DEV Community

nadirbasalamah
nadirbasalamah

Posted on

Java Tutorial - 9 Inheritance

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

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

Car class

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;
    }
}

Enter fullscreen mode Exit fullscreen mode

RaceCar class

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;
    }
}

Enter fullscreen mode Exit fullscreen mode

Main Class

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

    }
}

Enter fullscreen mode Exit fullscreen mode

Output

Running with racing spec

Enter fullscreen mode Exit fullscreen mode

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.

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

Enter fullscreen mode Exit fullscreen mode

Output

Running with racing spec

Enter fullscreen mode Exit fullscreen mode

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

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

Multilevel Inheritance Illustration

Sources

  • Learn more about inheritance in this link.

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.

Top comments (0)