Multilevel Inheritance In Java
With Example Programs
Multilevel Inheritance
In single inheritance we have only one parent class and one child class but multilevel inheritance the child class again have subclasses.This technique is called multilevel inheritance
For example, if A, B and C are three classes, we can inherit class B to class A and class C to class B using extends keyword. Also, if we create a C class object, we can access the properties of all three classes. We can limit the object creation in inheritance.This is the advantage of inheritance.
Java multilevel inheritance example program
class A
{
int age1=50;
void display1()
{
System.out.println("This is parent class method disply1");
System.out.println("parent class also known as base class");
System.out.println("Another name super class ");
}
}
class B extends A
{
int age2=40;
void display2()
{
System.out.println("This is child class method disply2");
System.out.println("child class also known as derived class");
System.out.println("Another name subclass ");
System.out.println("This class is parent of class C ");
}
}
class C extends B
{
int age3=30;
void display3()
{
System.out.println("This is grandchild class method disply3");
System.out.println("clild class of class B");
}
}
class M1
{
public static void main(String a[])
{
C obj=new C();
System.out.println();
obj.display1();
System.out.println();
obj.display2();
System.out.println();
obj.display3();
System.out.println();
System.out.println("parent age variable :"+(obj.age1));
System.out.println("child age variable :"+obj.age2);
System.out.println("grand child age variable :"+obj.age3);
System.out.println();
}
}
observe the program and its output carefully
Comments
Post a Comment