Java Tutorial - 9 Inheritance
Tags: java, tutorial, beginners
Published: 2021-06-10 00:10:44

## Introduction

Inheritance is a mechanism to create a sub class that inherits the attributes and behaviours from the parent class. Basically, the class that created in Java is automatically inherits from the `Object` class.

Usually, the parent class is a general abstraction of an entity. The sub class is a specific abstraction of an entity. The inheritance mechanism is illustrated in this picture.

![Inheritance Illustration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2z2iz249pqf6r7itt1wr.png)

## Creating Inheritance

To create an inheritance, use the `extends` keyword followed with the parent class. In this example, the two classes is created called `Car` and `RaceCar`. The `Car` acts as a parent class and the `RaceCar` acts as a sub class. The relation between `Car` and `RaceCar` is illustrated in this picture below.

![Inheritance Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/817h4cuicv6x18rzfr2e.png)

Car class

```java
public class Car {
    private String manufacturer;
    private String model;

    public Car(String manufacturer, String model) {
        this.manufacturer = manufacturer;
        this.model = model;
    }

    public void run() {
        System.out.println("Running...");
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

```

RaceCar class

```java
public class RaceCar extends Car{
    // additional field for race car class
    private String teamName;

    public RaceCar(String manufacturer, String model, String teamName) {
        // call the parent's constructor using super()
        super(manufacturer, model);
        this.teamName = teamName;
    }

    // implement run() for race car class
    @Override
    public void run() {
        System.out.println("Running with racing spec");
    }

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }
}

```

Main Class

```java
public class MyApp {
    public static void main(String[] args) {
        RaceCar car = new RaceCar("Porsche","911 GT3","Manthey Racing");
        car.run();

    }
}

```

Output

```
Running with racing spec

```

Based on the code above, the `RaceCar` is extends all the fields and methods in `Car` class so the fields and methods inside `Car` class is also available in `RaceCar` class. Notice that the `super()` is used inside the `RaceCar` constructor to call the parent's constructor.

The `RaceCar` class overrides the method from `Car` class called `run()` with `@Override` annotation. `@Override` basically means define the specific implementation in the parent's method in this case the `run()` method.

> The simple way to understand the inheritance code is replace the `extends` with `is a` word.

In this case, the `RaceCar` is a `Car` so the object from `RaceCar` class can be stored in a variable with the `Car` type.

```java
public class MyApp {
    public static void main(String[] args) {
        Car raceCar = new RaceCar("Toyota","GT-One","Toyota Racing");
        raceCar.run();
    }
}

```

Output

```
Running with racing spec

```

Based on the cove above, the `run()` method is called from `RaceCar` class.

## Notes

The multiple inheritance is not supported in Java. The multiple inheritance means a class can inherits many parent classes. This is the illustration of multiple inheritance.

![Multiple Inheritance Illustration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv9w7wgqjmdqdcqco71p.png)

The multilevel inheritance is supported in Java. This is the illustration of multilevel inheritance.

![Multilevel Inheritance Illustration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y02k0el681a9p980qg2.png)

## Sources

- Learn more about inheritance in this [link](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html).

I hope this article is helpful for learning the Java programming language. If you have any thoughts or comments you can write in the discussion section below.