Multilevel Inheritance in Java

vaishnavi pandey
Last Updated: May 24, 2022
Difficulty Level :
EASY

Introduction

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. 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.

Inheritance in Java

Inheritance is a crucial component of OOP. Inheritance is the ability of one class to inherit the properties and methods of another. Some standard terms frequently used in this article are: 

  1. CLASS: It is a template or blueprint with some common properties from which an object can be created.
  2. SUBCLASS/ CHILD CLASS: It is the class that inherits features from the parent class. It is also known as Derived Class.
  3. SUPER CLASS/PARENT CLASS: It is the class from which subclasses inherit traits. It is also known as Base Class.

(See OOPS in Java)

Let's get to know about multilevel Inheritance now.

Multilevel Inheritance

Multilevel Inheritance involves inheriting a class, which already inherited some other class. Correlating it with a real-life scenario, we’ve often seen some of our habits and thoughts match precisely with our parents. And similarly, their habits match with their parents, i.e., our grandparents. 

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.

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 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.


Now, you might be wondering, why do we use multilevel Inheritance in Java?

The answer is the readability and reusability of the code. We don't have to write the same code in the child class because it inherits the properties of the parent class. This makes it easier to reuse code, reduces the amount of code, and makes it more understandable.

Let’s understand the above lines with the help of an example.

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

//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

Frequently Asked Questions

What is Multiple Inheritance in Java? Give one example.

Multiple Inheritance occurs when one class extends more than one other class. For example, Class C extends Classes A and B. This is referred to as multiple Inheritance. Multiple Inheritance is not allowed in Java.


 

What is multilevel Inheritance? Explain with an example.

Multilevel Inheritance occurs when a class extends a class that extends another class. 
For example, class C extends class B, and class B extends class A. This is referred to as multilevel Inheritance.

How is multilevel Inheritance implemented in Java?

Multilevel Inheritance in Java is implemented using extends keyword. Refer to the example above to understand in a better way.

Is multilevel Inheritance allowed in Java?

Yes, multilevel Inheritance is allowed in Java.

What are the types of Inheritance in Java?

Java supports three main types of Inheritance: multilevel, hierarchical, and single. Multiple Inheritance in Java is supported using interfaces. 
 

Conclusion

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

Recommended Reading

Also, check out some Guided Paths on topics like Data Structure and Algorithms, Competitive Programming as well as some Contests and very helpful Interview Experiences and Interview Bundles only on CodeStudio brought to you by Industry Experts.

 

Was this article helpful ?
1 upvote

Comments

No comments yet

Be the first to share what you think