Multi-Level Inheritance in Java with Program Example

In this java tutorial, we will understand the working of multi-level inheritance in java with a program example. Multi-level inheritance can be considered as a addon to single inheritance as in this type we have more than one level of inheritance (shown in the diagram below). We have a complete explanation of Inheritance in Java so if you don’t know what Inheritance in Java is then check this article out.

In multi-level Inheritance, we have a single Super Class and a subclass1(level1) which inherits the properties directly from the Super class & then we have one more subclass2(level2) which  inherits the properties directly from the subclass 1 class.

multilevel inheritance in java

Java Multi-Level Inheritance Program Example –
Class SuperClass
{
   public void methodA()
   {
     System.out.println("SuperClass");
   }
}
Class SubClass1 extends SuperClass
{
   public void methodB()
   {
     System.out.println("SubClass1 ");
   }
}

Class SubClass2 extends SubClass1
{
   public void methodC()
   {
     System.out.println("SubClass2");
   }
   public static void main(String args[])
   {
     SubClass2 obj = new SubClass2();
     SubClass2.methodA(); //calling super class method
     SubClass2.methodB(); //calling subclass 1 method
     SubClass2.methodC(); //calling own method
  }
}
Output
SuperClass
SubClass1
SubClass2

Leave a Reply

Your email address will not be published. Required fields are marked *