Multilevel Inheritance in Java


Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). When a class extends a class, which extends anther class then this is called multilevel inheritance.

Example :
     class Son extends class Father and class Father extends class Grandfather then this type of inheritance is known as multilevel inheritance.

This Java program demonstrates the concept of multilevel inheritance, where a derived class extends another derived class which itself extends a base class.

  • In this example, there are three classes GrandFather, father and son. The class father extends the GrandFather class, while the class son extends the father class.
  • The GrandFather class has a method house() which prints the message "3 BHK House.". The father class has a method land() which prints the message "5 Arcs of Land..". The son class has a method car() which prints the message "Own Audi Car..".
  • In the main() method, an object o of the son class is created. The car(), house() and land() methods are called using this object to print their respective messages. Since son class extends father class which in turn extends GrandFather class, the son object can access all the methods of these classes.

Source Code

//Multilevel Inheritance in Java
class GrandFather {
    public void house() {
        System.out.println("3 BHK House.");
    }
}
class father  extends GrandFather{
    public void land() {
        System.out.println("5 Arcs of Land..");
    }
}
 
class son extends father {
    public void car() {
        System.out.println("Own Audi Car..");
    }
}
 
public class multilevel {
    public static void main(String args[]) {
        son o = new son();
        o.car();
        o.house();
        o.land();
    }
}
 

Output

Own Audi Car..
3 BHK House.
5 Arcs of Land..
To download raw file Click Here

Basic Programs