News

Tutorial: Creating Multilevel Hierarchy in Java

multilevel_inheritance

In our previous tutorial we have discussed all about Inheritance. What is it? Why it is required? and its 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.

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 lower most class will have the properties of all the super classes’.

So it will be like this:

  Person

      ↓

Employee

      ↓

Manager

N.B: 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

multilevel_inheritance

Explanation of Code & Output

Here Person, Employee and Manager are three classes. Manager subclass is derived from Employee subclass which is again derived from Person class. All these classes has one to one relation. So Manager class is the most specialized class among the three which will have the access right to all the members of both Employee and Person. On the other hand, Person super class won’t have access to any of the members of its subclasses, same is true for Employee subclass which won’t have access to the any members of Manager subclass.

All these three classes are written in single java file for your understanding but generally these are put in three different java files.

Next we will checkout how to do method overloading in Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.

Click to comment

Leave a Reply

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

To Top