Inheritance is one of the most important concepts of java programming, and it affects the way in which we design and write our java classes. It is through inheritance that a class can immediately inherit the properties of another class. Java Multilevel Hierarchy allows you to inherit properties of a grandparent in  a child class.

In simple inheritance, a sub class or derived class derives its properties from its parent or super class. But in multilevel inheritance a sub class is derived from a derived class. Each class inherits only a single class. Therefore, in multilevel inheritance a ladder is formed which at each step increases by one and the lowermost class will have the properties of all the super classes.

It looks like this,

Java Multilevel Hierarchy

Java multilevel hierarchy is highly effective in implementing code reuse. It reduces programming efforts by using the predefined and pre-compiled classes. The readability of programs increases giving opportunity to a programmer to extend his code with additional features and methods on those offered by parent and grand parent classes.

For Example

Java Multilevel Hierarchy Example

Java Multilevel Hierarchy-Example

Let us understand it in the form of a program. The following program contains the above shown hierarchy.

 
 
class stu
{
      int rollno;
      String name;
      stu(int r, String n)
      {
            rollno = r;
            name = n;
      }
      void show()
      {
            System.out.println("Student Roll no - " + rollno);
            System.out.println("Student Name - " + name);
      }
}
class marks extends stu
{
      int s1, s2, s3, s4, s5, sum1;
      marks(int r, String n, int m1, int m2, int m3, int m4, int m5)
      {
            super(r,n);
            s1 = m1;
            s2 = m2;
            s3 = m3;
            s4 = m4;
            s5 = m5;
      }
      void showmarks()
      {
            show();
            sum1 = s1 + s2 + s3 + s4 + s5;
            System.out.println("Total marks = " + sum1);
      }
}
class percent extends marks
{
      float per;
      percent(int r, String n, int m1, int m2, int m3, int m4, int m5, float p)
      {
            super(r,n,m1,m2,m3,m4,m5);
            per=p;
      }
      void showper()
      {
            showmarks();
            per = sum1 / 5;
            System.out.println("Percentage = " + per);
      }
}
class multiEg
{
      public static void main(String args[])
      {
            percent p = new percent(101, "Nancy", 90, 75, 85, 90, 80, 0);
            p.showper();
      }
}

Output

java multilevel hierarchy Program

Explanation of the above code

In the above program code, there are three classes forming Java Multilevel Hierarchy: student, marks and percent. The class student serves as the parent class for the derived class marks, which in turn serves as a parent class for the derived class percent. All the classes have a one to one relation. Marks class serves as the intermediates base class as it serves as a link between the classes’ student and percent. The chain so formed is known as the inheritance path.

In the chain so formed, each sub class inherits all the features of the parent class. So in the above example, percent class inherits all the properties of both the classes’ marks and student.