Multi-Level Inheritance in Java

Multi-level inheritance was introduced to enhance the concept of inheritance. When a subclass is derived from another subclass or derived class, it is known as the multi-level inheritance. In multilevel inheritance, the subclass is the child class for its super class is the subclass for another super class. Multi-level inheritance can go up to any number of levels.

 

class P

{

   int e;

   int f;

   int get(int a,int b)

   {

     e=a;

     f=b;

     return(0);

   }

void show()

   {

     System.out.println(e);

   }

}

class Q extends P

{

   void showq()

   {

     System.out.println("Q");

     }

}

class R extends Q

{

   void display()

   {

     System.out.println("R");

   }

public static void main(String aa[])

   {

     P p=new P();

     p.get(7,10);

     p.show();

   }

}

Output...

Multi level inheritance in java

Explanation…

In above example

1) First, a class P is created, wherein integer e and f are declared.

2) In the class P,

     a)The get() is called to get the values of integers a and b

     b)In the get()  

               i)The value of a and b are assigned to e and f respectively.

             ii)Then, return(0); statement is used to return the value of 0.

             iii)The show() is then called, wherein e is printed on the screen.

3)A new class Q is created as a subclass of the class P using the extends keyword.

4)In the class Q void showq() is called, wherein Q is printed on the screen.

5)A new class R is created as a subclass of the class Q using the extends keyword.

6)In the class R,

     a)The display() is called, wherein R is printed on the screen.

     b)Then, the main() of the class is called.

     c)In the main() method,

        i)A new object p of the class P is created using the new keyword.

       ii)Then, the int get() of the class P is called with the values for the variables in the parameter list,  that is 7 and 10.

     iii)At last, show() is called to show the value of p.

     iv)Finally, the output 7 is displayed on the screen.

      

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @