Creating a Multilevel Inheritance Hierarchy in Java



Inheritance involves an object acquiring the properties and behaviour of another object. So basically, using inheritance can extend the functionality of the class by creating a new class that builds on the previous class by inheriting it.

Multilevel inheritance is when a class inherits a class which inherits another class. An example of this is class C inherits class B and class B in turn inherits class A.

A program that demonstrates a multilevel inheritance hierarchy in Java is given as follows:

Example

 Live Demo

class A {
   void funcA() {
      System.out.println("This is class A");
   }
}
class B extends A {
   void funcB() {
      System.out.println("This is class B");
   }
}
class C extends B {
   void funcC() {
      System.out.println("This is class C");
   }
}
public class Demo {
   public static void main(String args[]) {
      C obj = new C();
      obj.funcA();
      obj.funcB();
      obj.funcC();
   }
}

Output

This is class A
This is class B
This is class C

Advertisements