DEV Community

Akshay Sharma
Akshay Sharma

Posted on

MultiLevel Inheritance in Java

The concept of inheritance is straightforward but powerful: When you want to create a new class but there is an existing class that contains some of the code you need, you can derive your new class from it. This allows you to reuse the existing class's fields and methods without having to write (and debug!) them from scratch. Isn’t it cool? Now, there are several types of inheritance in OOPS which undoubtedly assist in writing readable and beautiful code.
Spoiler Alert: The lazy programmers love Inheritance.

MultiLevel Inheritance

When a superclass is inherited by an intermediate class, the intermediate class is inherited by a derived class( or child class), forming three or more levels of inheritance known as MultiLevel Inheritance.

Now, you must be wondering what is an intermediate class?
Well, as the name implies, it is the combination of a parent class and a child class. It means that at some point it is acting like a child class and parent class for the other subclass.

Let us understand it with an example:

In the above example, Grandfather is the SuperClass, Father is the Child Class for GrandFather Class and Parent Class for the Child Class.

Now, a question may arise whether this Son Class can inherit the properties of the Father as well as the GrandFather class?
The answer is Yes. Child Class has now access to the Super Class as well as its Parent Class i.e., Father Class.

Note: In multilevel inheritance we have only one SuperClass and multiple subclasses.

The levels are not restricted to three only, you can make as many levels you require and use the functionalities of parent classes as many times you want.
Interesting right?

Below is the given syntax to perform MultiLevel Inheritance:

Syntax

class SuperClass {
}
class sub1 extends SuperClass{
}
class sub2 extends sub1{
}
...
...
Enter fullscreen mode Exit fullscreen mode

Now, it's time to get our hands clean on writing the code to understand it in a more better way:

Time for an Example

class A{
    private int a; 
    private int b;
   public void add(int a , int b)
   {
       this.a = a;
       this.b = b;
    System.out.println("Addition results:" + (a+b));
   }
}
class B extends A{
    private int x;
    private int y;
    public void mul(int x , int y)
    {
       this.x = x;
       this.y = y;
    System.out.println("Multiplication results:" + (x*y));
    }
}
public class C extends B{
    private int p;
    private int q;
    public void div(int p , int q)
    {
       this.p = p;
       this.q = q;
    System.out.println("Division results:" + (p/q));
    }
   public static void main(String args[])
   {
     C obj=new C();
     // This object will have access to all the methods stated above
     obj.add(20,30);
     obj.mul(30,60);
     obj.div(50,5);
   }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Let’s us see one more example to understand its usage:
Another Example:

class A{
    void printName(String name){
        System.out.println("SuperClass => Name:" + name);
    }
}
class B extends A{
    void printName(String name){
        System.out.println("Intermediate Class => Name:" + name);
    }
}
public class C extends B{
    void printName(String name){
        System.out.println("Child Class => Name:" + name);
    }
   public static void main(String args[])
   {
     C obj=new C();
     obj.printName("Ninja");
   }
}
Enter fullscreen mode Exit fullscreen mode

Image description

We have invoked the method printName() which exists in all the three classes. Here, the method of class A is overridden in class B and the same method is overridden again in class C. As a result, the Class C's method got executed. The invocation could have been executed in Class B if that method had not been overridden in C.

Note: We can make a parent class feature (method or variable) private if we don't want it to be available outside the class. Such a feature will not be available to child classes in that case.

Now, let us look at some Faqs based on the above discussion:

Frequently Asked Questions

What is the difference between single inheritance and multilevel inheritance?

Inheriting properties from a grandparent in a child class is possible with Java Multilevel Hierarchy. A subclass or derived class inherits properties from its parent or superclass in simple inheritance. A subclass, on the other hand, is derived from a derived class in multilevel inheritance.

What are the advantages of using Inheritance?

  • Reusability is aided by inheritance. When a class inherits or derives from another class, it gains access to all of the inherited class's functionality.
  • Reusability increased the level of trustworthiness. The code for the base class will have already been tested and debugged.
  • Because existing code is reused, development and maintenance costs are reduced.
  • Inheritance reduces code redundancy while also allowing for code extensibility.

What are the disadvantages of using Inheritance?

  • Because of the indirection, inherited functions are slower than normal functions.
  • Improper inheritance use can lead to incorrect outcomes.
  • Data members in the base class are frequently left unused, resulting in memory waste.

What are the applications of Inheritance?

  • Reusability - A code once defined can be used multiple times which reduces the overall TAT and increases the efficiency.
  • Overriding - It helps replace the original implementation present in the base/parent class and instead use a newer version present in the derived/child class. We can increase features of class or method by overriding.
  • It minimises the length of code by placing the common code in the superclass
  • Redundancy gets reduced because we don't need to write the same code again and again.

Conclusion

Kudos that you have made it here as Inheritance is one of the most important features of an object-oriented programming language.

To wrap up the above discussion, we’ve extensively discussed one of the most important types of inheritance i.e., MultiLevel Inheritance with examples. Now, it’s your turn to write beautiful code using the same.
Happy learning!

Top comments (0)