Multilevel Inheritance in Java

The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class.

When a class extends a class, which extends anther class then this is called multilevel inheritance. For example class C extends class B and class B extends class A then this type of inheritance is known as multilevel inheritance.

Multilevel Inheritance

It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of this diagram, then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and methods of class A along with class B that’s what is called multilevel inheritance.

Example.

class Shape {
   void display() {
      System.out.println("Inside display");
   }
}
class Rectangle extends Shape {
 void area() {
      System.out.println("Inside area");
   }
}
class Cube extends Rectangle {
   void volume() {
      System.out.println("Inside volume");
   }
}
   public static void main(String[] arguments) {
      Cube cube = new Cube();
      cube.display();
      cube.area();
      cube.volume();
   }
}

Reference.

https://www.tutorialspoint.com/Multilevel-inheritance-in-Java

https://beginnersbook.com/2013/12/multilevel-inheritance-in-java-with-example/

https://www.codingninjas.com/codestudio/library/multilevel-inheritance-in-java

Leave a comment

Design a site like this with WordPress.com
Get started