Types of inheritance in Java: Single, Multiple, Multilevel, Hybrid Tutorial

In this section, we will learn the types of inheritance in Java and how they work.

Note: we’re assuming you’re already familiar with the general concepts of inheritance in Java.

1- Java Single inheritance

The first and perhaps the simplest method of inheritance is when two classes are involved and one inherits from the other.

In cases like this, one of the classes becomes the parent class, and the other is the child class.

Example: single inheritance in Java

package com.example.demo;

public class Animal {
    protected void saySomething(){
        System.out.println("The animal is making noise...");
    }
}


package com.example.demo;

public class Cat extends Animal {
    public void makeNoise(){
        saySomething();
    }
}

In this example, the class `Cat` is the child and the class `Animal` is the parent class.

2- Java multiple inheritance

Multiple inheritance means one class (for example, class A) inherits from two or more classes using the extends keyword!

You should know that Java does not support multiple inheritances between classes. But we can create a class that implements multiple interfaces!

Note: please check the Java interface section to learn more about this.

Example: multiple inheritance in Java

package com.example.demo;

public class ParentOne {
}


package com.example.demo;

public class ParentTwo {
}


package com.example.demo;

public class Child extends ParentOne, ParentTwo {
}


package com.example.demo;

public class Main {

    public static void main(String[] args) {
        Child child = new Child();
    }
}

Output:

Error…

We get error because the `Child` class is trying to inherit from more than just one class!

3- Java multilevel inheritance

Multilevel inheritance is the idea that a parent class can itself become the child class of another class!

For example, consider three classes: GrandParent, Parent, and Child.

Here the Child class can inherit from the Parent class and also the Parent class can inherit from the GrandParent class!

In a situation like this, the `Parent` class has access to the public and protected members of the GrandParent class and the `Child` class has the access to the members of the Parent as well as the GrandParent class!

Basically, using the multilevel inheritance, we can create a chain of classes that inherit from each other and each class in the chain can access the entire members (default, protected, public) of those ancestor classes in the chain.

Example: multilevel inheritance in Java

package com.example.demo;

public class GrandParent {
    public void sayHiGrandParent(){
        System.out.println("Grandpa is saying Hi :)");
    }
}


package com.example.demo;

public class Parent extends GrandParent{
    public void sayHiParent(){
        System.out.println("My Parents are saying Hi :)");
    }
}


package com.example.demo;

public class Child extends Parent{
    public void sayHiChild(){
        System.out.println("The child is saying Hi :)");
    }
}


package com.example.demo;

public class Main {

    public static void main(String[] args) {
        Child child = new Child();
        child.sayHiChild();
        child.sayHiParent();
        child.sayHiGrandParent();
    }
}

 

Output:

The child is saying Hi 🙂

My Parents are saying Hi 🙂

Grandpa is saying Hi 🙂

As you can see, the instance of the `Child` class has the access to the public members of the `Parent` as well as the `GrandParent` class because these two classes are the ancestors in the chain that the `Child` class is.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies