Tutorial: Creating Multilevel Hierarchy in Java

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. This guide is on Creating Multilevel Hierarchy in Java. Let’s learn it!

Creating Multilevel Hierarchy in Java

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 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 inheritance where one class can inherit more than one class at a time. Java does not support multiple inheritance.

We will understand Multilevel Inheritance using the following example:

Java Program

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();
  }
}

Output

Creating Multilevel Hierarchy in Java

Explanation of Code & Output

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 Manager class is the most special 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, same is true for Employee subclass which won’t have access to any members of Manager subclass.

To make sure you understand well, all these three classes are written in 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.