Java : Multilevel Inheritance


 

Java : Multilevel Inheritance

This section contains concept of multilevel Inheritance in java.

This section contains concept of multilevel Inheritance in java.

Java : Multilevel Inheritance

This section contains concept of multilevel Inheritance in java.

Multilevel Inheritance :

In multilevel inheritance more than two classes take part in creation of application. Super class is inherited by one subclass and that subclass is again inherited by another subclass. You can say multilevel inheritance create one-to-one ladder.
Now you can access properties of all classes through lower most subclass. You can say it is indirect way to implement multiple inheritance.

Example :

class A {
void show() {
System.out.println("This is class A");
}
}

class B extends A {
void showB() {
System.out.println("This is class B and you are inheriting class A");
}
}

class C extends B {
void showC() {
System.out.println("This is class C and inheriting class B");
}
}

class MultilevelInheritance {
public static void main(String[] args) {
C c = new C();
c.show();
c.showB();
c.showC();
}
}

Description : In this example we are showing how to implement multilevel inheritance. class A is our super class having only one show() method. class B is subclass extending class A that is inheriting show() method and also having one showB() method. class C is another subclass extending subclass B. Now class C  has features of both class A and B. In main class MultilevelInheritance we are creating instance of class C to access all the methods show(), showB() and showC() .

Output :

This is class A
This is class B and you are inheriting class A
This is class C and inheriting class B

Ads