Types Of Inheritance

Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.

Below are the different types of inheritance which is supported by Java.
  • Single Inheritance
  • Multiple Inheritance (Through Interface)
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance (Through Interface) 

Single Inheritance in Java

Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we  call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class.
https://www.javainterviewpoint.com/wp-content/uploads/2015/07/Single_Inheritance_in_Java.png 
public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
    public static void main(String args[])
    {
        //Assigning ClassB object to ClassB reference
        ClassB b = new ClassB();
        //call dispA() method of ClassA
        b.dispA();
        //call dispB() method of ClassB
        b.dispB();
    }
}
Output :
disp() method of ClassA
disp() method of ClassB

Multiple Inheritance in Java

Multiple Inheritance is nothing but one class extending more than one class. Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has to manage the dependency of more than one Parent class. But you can achieve multiple inheritance in Java using Interfaces.
Multiple_Inheritance_in_Java

Multilevel Inheritance in Java

In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.
Multilevel_Inheritance_in_Java
MultiLevel Inheritance Example
public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassB
{
    public void dispC()
    {
        System.out.println("disp() method of ClassC");
    }
    public static void main(String args[])
    {
        //Assigning ClassC object to ClassC reference
        ClassC c = new ClassC();
        //call dispA() method of ClassA
        c.dispA();
        //call dispB() method of ClassB
        c.dispB();
        //call dispC() method of ClassC
        c.dispC();
    }
}
 
        Output :
         disp() method of ClassA
         disp() method of ClassB
         disp() method of ClassC

Hierarchical Inheritance in Java

In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example ClassA will be inherited by ClassB, ClassC and ClassD. ClassA will be acting as a parent class for ClassB, ClassC and ClassD.

Hierarchical_Inheritance_in_Java


public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassA
{
    public void dispC()
    {
        System.out.println("disp() method of ClassC");
    }
}
public class ClassD extends ClassA
{
    public void dispD()
    {
        System.out.println("disp() method of ClassD");
    }
}
public class HierarchicalInheritanceTest 
{
    public static void main(String args[])
    {
        //Assigning ClassB object to ClassB reference
        ClassB b = new ClassB();
        //call dispB() method of ClassB
        b.dispB();
        //call dispA() method of ClassA
        b.dispA();
        
        
        //Assigning ClassC object to ClassC reference
        ClassC c = new ClassC();
        //call dispC() method of ClassC
        c.dispC();
        //call dispA() method of ClassA
        c.dispA();
        
        //Assigning ClassD object to ClassD reference
        ClassD d = new ClassD();
        //call dispD() method of ClassD
        d.dispD();
        //call dispA() method of ClassA
        d.dispA();
    }
}
Output :
          disp() method of ClassB 
          disp() method of ClassA 
          disp() method of ClassC 
          disp() method of ClassA 
          disp() method of ClassD 
          disp() method of ClassA

Hybrid Inheritance in Java

Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be acting as the Parent class for ClassB & ClassC and ClassB & ClassC will be acting as Parent for ClassD.
Hybrid_Inheritance_in_Java