Multilevel inheritance is a type of inheritance where a subclass acts as a superclass of another class. In other words, when a class having a parent class, is extended by another class and forms a sequential chain, then it’s termed Multilevel inheritance. For instance, class A is extended by class B, and further class B is inherited by class C which acquires features of class A and class B.
In multilevel inheritance, a parent can have a single child only and at least require three classes arranged sequentially, forming a chain of parent-child relations.
Syntax:
class A { } class B extends A { } class C extends B { }
Below are some programs provided to implement multilevel inheritance in Java.
Program:
class Person { String getName(){ return "ProgrammerBay"; } } class Programmer extends Person{ String getCodingLanguage(){ return "Java"; } } class Program extends Programmer{ int getLineOfCode(){ return 20; } public static void main(String[] args) { Program program = new Program(); System.out.println(" I am "+program.getName()+" and I code in "+ program.getCodingLanguage()+ " . This program has "+program.getLineOfCode()+" lines"); } }
Output:
I am ProgrammerBay and I code in Java . This program has 20 lines
Explanation:
In the above code, we are having 3 classes named Person, Programmer and Program where class Program is inheriting class Programmer, further, class Programmer is extending class Person. As a result, the Program class’s got the functionalities of class Programmer and also class Person.
Program:
class iPhone6 { void makeCalls(){ System.out.println("Calling functionality......."); } } // Getting feature of previous iPhone model class iPhone10 extends iPhone6{ void unlockPhoneByFaceId(){ System.out.println("Unlocking phone by face Id......."); } } // Getting feature of previous iPhone10 class iPhone12 extends iPhone10{ void supportFor5GNetwork(){ System.out.println("5G network support......."); } public static void main(String[] args) { iPhone12 iPhone = new iPhone12(); iPhone.makeCalls(); // feature reused of iPhone 6 for iPhone 12 iPhone.unlockPhoneByFaceId(); // feature reused of iPhone 10 for iPhone 12 iPhone.supportFor5GNetwork(); // additional feature } }
Output:
Calling functionality....... Unlocking phone by face Id....... 5G network support.......
Explanation:
In the above code, we’ve tried to explain multilevel inheritance using the iPhone Models example. Just similar to the previous example, iPhone12 class is having the functionality of iPhone10 and iPhone6 classes. Because iPhone12 class is inheriting iPhone 10 class, further, iPhone10 is extending iPhone 6 class.
Program:
class Person { Person(){ System.out.println("This is Person's class constructor"); } void printName(){ System.out.println("ProgrammerBay"); } } class Programmer extends Person{ Programmer(){ System.out.println("This is Programmer's class constructor"); } void printCodingLanguage(){ System.out.println("Java"); } } class Program extends Programmer{ Program(){ System.out.println("This is Program's class constructor"); } void printLineOfCode(){ System.out.println(20); } public static void main(String[] args) { Program program = new Program(); program.printName(); program.printCodingLanguage(); program.printLineOfCode(); } }
Output:
This is Person's class constructor This is Programmer's class constructor This is Program's class constructor ProgrammerBay Java 20
Explanation:
What is multilevel Inheritance in java?
Multilevel Inheritance in java can be defined as an inheritance where a class extends another class, and further, that subclass is become a parent of another class by extending it
How is multilevel Inheritance implemented in Java?
It can be implemented using extends keyword where class A is extended by B, and further, C sequentially extends B.
Is multilevel Inheritance allowed in Java?
Yes, multilevel Inheritance is supported in Java.
What are the types of Inheritance in Java?
There are various types of inheritance supported by Java.
In the case of interfaces, Java supports 5 types of inheritance
which are Single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance.
In the case of classes, it limits to 3 types which are single, multilevel and hierarchical inheritance as hybrid and multiple inheritance is not supported in this.
This post was last modified on March 10, 2024