Update appNew update is available. Click here to update.
Last Updated: Sep 26, 2023
Medium

Multilevel Inheritance in Java

Introduction

Multilevel Inheritance in Java is a type of inheritance in which a class that is already inherited by another class, inherits another class. Before we delve into the details of this topic, let's get our basics cleared. Object-Oriented Programming or OOPs refers to a programming paradigm that organizes software design around real-world objects. Inheritance, abstractionpolymorphism, and other real-world concepts are all part of OOP.

multilevel inheritance in java

The basic goal of OOP is to connect data and functions. Functions operate on data so that no other part of the code may access it. 

This article is a part of the Inheritance blog series in Java. This series includes all four types of Inheritance in Java. 

  1. Hierarchical Inheritance in Java.
     
  2. Single Inheritance in Java.
     
  3. Multiple Inheritance in Java.
     
  4. Multilevel Inheritance in Java
     

In this article, we'll be discussing Multilevel Inheritance in Java. Let’s first get acquainted with the concept of Inheritance.

What is Inheritance in Java?

In Java, inheritance is a fundamental concept of Object-Oriented Programming (OOP) where one class (the subclass or child class) can inherit attributes and behaviors from another class (the superclass or parent class). It allows for the creation of a new class based on an existing class, reusing its fields and methods. 

This promotes code reusability and hierarchical organization. The subclass inherits the properties and methods of the superclass, enabling the extension and specialization of classes in a structured manner. Inheritance facilitates the building of complex software systems by promoting the sharing and modification of code.

Syntax of Java Inheritance

class Subclass extends Superclass {
   public Subclass() {
      super(); // calling constructor of Superclass
   }
}

Subclass obj = new Subclass(); // Creating object of Subclass

 

Let's get to know about multilevel Inheritance now.

What is Multilevel Inheritance in Java?

Multilevel Inheritance in java involves a class extending to another class that is already extended from another class. In simple words it includes inheriting a class, which already inherited some other class. We can create hierarchies in Java with as many layers of Inheritance as we want. This means we can utilize a subclass as a superclass. In this case, each subclass inherits all of the traits shared by all of its superclasses. Multilevel inheritance in Java occurs when a class inherits from a class, and then another class inherits from that derived class. For example, consider a class "Vehicle," then a class "Car" inheriting from "Vehicle," and finally a class "SportsCar" inheriting from "Car."

Why do we use Multilevel Inheritance in Java? 

In Java, inheritance enhances code readability and reusability by allowing child classes to inherit properties and methods from parent classes. This avoids redundant code, reducing complexity, and making the codebase more understandable. Developers can efficiently build on existing functionality, fostering a streamlined development process and clearer class relationships.

Multilevel Inheritance in Java Example

Let's assume you work in a software company. And your manager told you about a project to find out the volume of a box. You, the competent employee, completed this task by creating a simple Box class(as shown in the below given example).

The next day, he told you to rewrite the program, adding weight as well. But, you had clear inheritance concepts, so you have created a child class, BoxWeight, and inherited the Box class.

But, the manager was not impressed and told you again to rewrite the code and add Shipment details. This made you a little disturbed, but then you recalled your multilevel inheritance concept. You created another class, Shipment, and inherited the BoxWeight class.

And you tackled this situation quickly. This is how Inheritance makes life easier.

Implementation

  • Java

Java

//Superclass box...
class  Box {
  private  double width;
  private  double height;
  private  double depth;
  // Constructor of the superclass
  Box(double w,  double h,  double d)
  {
      width = w;
      height = h;
      depth = d;
  }
  // Volume calculation...
  double  volume() {
      return width * height * depth;
  }
}
// Sub class - 1
// BoxWeight class extending the box class...
class  BoxWeight  extends  Box
{
  double weight;  // weight of box
  // Sub class -1 constructor
  BoxWeight(double w,  double h,  double d,  double m) {
      super(w, h, d);  // calling superclass constructor
      weight = m;
  }
}

// Sub class - 2
// Shipment class extending BoxWeight class...
class  Shipment  extends  BoxWeight {
  double cost;

  // Sub class - 2 constructor
  Shipment(double w,  double h,  double d,  double m,  double c) {
      super(w, h, d, m);  // calling superclass constructor
      cost = c;
  }
}

public class  TestMultilevel
{
  public  static  void  main(String args[])
  {
      Shipment shipment1 =  new Shipment(1,  2,  3,  5,  3.41);
      Shipment shipment2 =  new Shipment(2,  4,  6,  10,  1.28);

      double vol;
      vol = shipment1.volume();

      System.out.println("The volume of shipment1 is " + vol);
      System.out.println("The weight of shipment1 is " + shipment1.weight);
      System.out.println("Shipping cost: Rs." + shipment1.cost);
      System.out.println();

      vol = shipment2.volume();
      System.out.println("The volume of shipment2 is " + vol);
      System.out.println("The weight of shipment2 is " + shipment2.weight);
      System.out.println("Shipping cost: Rs." + shipment2.cost);
  }
}

Output

The volume of shipment1 is 6.0
The weight of shipment1 is 5.0
Shipping cost: Rs.3.41
The volume of shipment2 is 48.0
The weight of shipment2 is 10.0
Shipping cost: Rs.1.28

Real Life Example

Multilevel Inheritance Real Life Example

In the above diagram, multilevel Inheritance is shown. A is the grandparent class, B is the parent, and C is the child class. B is a subclass of A, so it inherits the methods of A in it. Similarly, C is the subclass of B, so it inherits all the methods associated with B and its parent.

Let's understand multilevel inheritance in java with the help of a real-life example.

There's a family hierarchy:

Sachin → Raj(Sachin's Father) → Shyam(Raj's Father) → Ramesh(Shyam's Father). 

Here, Sachin is the child of Raj. 

Raj is the child of Shyam.

Shyam is the child of Ramesh.

For consideration in this example, Ramesh has his own traits.

Shyam has his own traits, along with all the traits of Ramesh.

Raj has his own traits, along with all the traits of Shyam. 

Sachin has his own traits, along with all the traits of Raj.

This is one example of multilevel inheritance that we can visualize in our day-to-day life.

Frequently Asked Questions

What is multilevel Inheritance with example? 

Multilevel inheritance is when a derived class inherits properties from a base class, and then another class inherits from that derived class. An example would be a Grandfather ( base class) who passes traits to his Son (first-derived class), who then passes those traits to his Grandson( second-derived class).

Is multilevel inheritance allowed in Java?

Yes, Java allows multilevel inheritance. This means a class can inherit from another class, which in turn can inherit from another class. It forms a chain of inheritance.

Why do we need multilevel inheritance in Java?

We use multilevel inheritance in Java to create a hierarchy of classes where a child class inherits from another child class, enabling code reuse and organizing classes in a structured way.

What is the problem with multilevel inheritance in Java?

The issue with multilevel inheritance in Java is that it can lead to complex and hard-to-maintain code. It may cause difficulties in understanding and debugging, affecting code readability and reliability.

Conclusion

Inheritance is a crucial concept in Object-Oriented Programming. In the above article, we have thoroughly discussed the concept of Multilevel Inheritance in Java and have seen its implementation in Java.

Recommended Reading-

Happy Learning Ninjas!

Previous article
Difference Between Inheritance and Polymorphism
Next article
Multiple Inheritance in Java