How to create multilevel hierarchy in Java (Tutorial)

In our previous tutorial, we have discussed all Inheritance. What is it? Why is it required? and it’s different types! Moreover, we have already come across Simple Inheritance; here, we will discuss Multilevel Inheritance. It is nothing but the enhancement of the Simple Inheritance. From the type name, it is pretty much clear that Inheritance is done at ‘n’ number of levels, where n>1. I will show in this guide how you can create a multilevel hierarchy in Java. Let’s learn it!

How to create a multilevel hierarchy in Java (Tutorial)

In simple inheritance, a subclass or derived class derives the properties from its parent class, but in multilevel inheritance, a subclass is derived from a derived class. One class inherits the only single class. Therefore, in multilevel inheritance, every time ladder increases by one. The lowermost class will have the properties of all the superclass.

So it will be like this:

  Person

      ↓

Employee

      ↓

Manager

Note: Multilevel inheritance is not multiple inheritances where one class can inherit more than one class at a time. Java does not support multiple inheritances.

We will understand Multilevel Inheritance using the following example:

Java Program to create a multilevel hierarchy

class Person{
  String personName;
  String address;
  Person(String personName,String address){
    this.personName = personName;
    this.address = address;
  }
  void showPersonalDetails(){
    System.out.println("Name is: "+personName);
  }
}

class Employee extends Person{
  String employeeID;
  double salary;
  Employee(String personName,String address,String employeeID,double salary){
    super(personName,address);
    this.employeeID = employeeID;
    this.salary = salary;
  }
}

class Manager extends Employee{
  int numberOfSubordinates;
    Manager(String personName,String address,String employeeID,double salary,int numberOfSubordinates){
    super(personName,address,employeeID,salary);
    this.numberOfSubordinates = numberOfSubordinates;
  }
}

class MultileveleInheritance{
  public static void main(String args[]){
    Person p =  new Person();
    Employee e = new Employee();
    Manager m = new Manager();
  }
}

The output of the multilevel hierarchy program

Creating Multilevel Hierarchy in Java

Explanation of Code & Output of multilevel hierarchy Java Programme

Here, Person, Employee, and Manager are three classes. Manager subclass derives from the Employee subclass, which derives from the Person class. All these classes have one to one relation. So the Manager class is the unique class among the three, which will have the access right to all the members of both Employee and Person.

On the other hand, Person superclass won’t have access to any of the members of its subclasses; the same is true for Employee subclass, which won’t have access to any members of the Manager subclass.

To make sure you understand well, all these three classes are written in a single java file. Generally, these are put in three different java files.

Next, we will check out how to do method overloading in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.