Multilevel Inheritance in Java

Multilevel Inheritance in Java
Multilevel Inheritance in Java

Introduction

Object-Oriented Programming or OOPs refers to a programming paradigm that organises software design around real-world objects.

Inheritance, abstraction, polymorphism, 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.

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.

Let’s get to know about multilevel Inheritance now.

Multilevel Inheritance in Java

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 utilise a subclass as a superclass. In this case, each subclass inherits all of the traits shared by all of its superclasses.

Example: 

blog banner 1

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.

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 to Illustrate Multilevel Inheritance in Java

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.

The implementation of the above problem is given below:

//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);
}
}
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 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.

Key Takeaways

In object-oriented programming, Inheritance is a crucial concept. And we’ve gone over Multilevel Inheritance in Java in-depth in this article.

For technical interviews, you can refer to some of the frequently asked OOPs Interview Questions to refresh your concepts.

By: Vaishnavi Pandey