Multilevel inheritance is the form of inheritance in which there is a base class and the derived class. The derived class is itself the base class .Derived class itself acts as the base class of some other derived class.
Following is the code of a multilevel inheritance.The code below has four classes Fclass ,Sclass, Tclass,Mlevel. Fclass is the base class for Sclass, Sclass is the derived lass for Fclass and acts as the base class for Tclass, Tclass is the derived class of Sclass.
Mlevel class creates object of the Tclass and using objects functions f1,f2 and f3 are called.
{
void f1()
{
System.out.println("First Class");
}
}
class Sclass extends Fclass
{
void f2()
{
System.out.println("Second Class");
}
}
class Tclass extends Sclass
{
void f3()
{
System.out.println("Class three");
}
}
class Mlevel
{
public static void main(String arg[])
{
Tclass obj=new Tclass();
obj.f1();
obj.f2();
obj.f3();
}
}
Following is the output of the above code for better understanding:
Following is the code of a multilevel inheritance.The code below has four classes Fclass ,Sclass, Tclass,Mlevel. Fclass is the base class for Sclass, Sclass is the derived lass for Fclass and acts as the base class for Tclass, Tclass is the derived class of Sclass.
Mlevel class creates object of the Tclass and using objects functions f1,f2 and f3 are called.
Program for Multilevel Inheritance
class Fclass{
void f1()
{
System.out.println("First Class");
}
}
class Sclass extends Fclass
{
void f2()
{
System.out.println("Second Class");
}
}
class Tclass extends Sclass
{
void f3()
{
System.out.println("Class three");
}
}
class Mlevel
{
public static void main(String arg[])
{
Tclass obj=new Tclass();
obj.f1();
obj.f2();
obj.f3();
}
}
Following is the output of the above code for better understanding: