Login    New User    Forgot Password    Help      

Multilevel Inheritance

Java Tutorials

It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class.  Multilevel inheritance can go up to any number of level.

Example

// Multilevel hierarchy
 class base
{
int a;
             
        base()
        {
        a=10;
        }

        void showbase()
        {
        System.out.println("Base class");
        System.out.println("Value of b is "+a);
        }

}

class drive extends base
{
int b;

     drive()
     {
     b=20;
     }

     void showdrive()
     {
     System.out.println("\ndrive class");
     System.out.println("Value of a is "+a);
     System.out.println("Value of b is "+b);
     }

}
class drive1 extends drive
{
int c;

     drive1()
     {
     c=30;
     }

     void showdrive1()
     {
     System.out.println("\nDrive one class");
     System.out.println("Value of a is "+a);
     System.out.println("Value of b is "+b);
     System.out.println("Value of c is "+c);
     }
}

class herit4
{
public static void main(String args[])
{
base b1=new base();
b1.showbase();

drive ob=new drive();
ob.showdrive();

drive1 ob1=new drive1();
ob1.showdrive1();

}
}

Output






Java Tutorials