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, 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.
This article is a part of the Inheritance blog series in Java. This series includes all four types of Inheritance in Java.
- Hierarchical Inheritance in Java
- Single Inheritance in Java
- Multiple Inheritance in Java
- 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:
- CLASS: It is a template or blueprint with some common properties from which an object can be created.
- SUBCLASS/ CHILD CLASS: It is the class that inherits features from the parent class. It is also known as Derived Class.
- SUPER CLASS/PARENT CLASS: It is the class from which subclasses inherit traits. It is also known as Base Class.
Why use inheritance in java
- Inheritance is a fundamental concept in Java that allows for code reuse, modularity, and flexibility.
- Creating a new class based on an existing class saves time and effort by inheriting all of its attributes and methods.
- Inheritance enables the use of polymorphism, where a subclass object can be used in place of a parent class object.
- Inheritance provides a more modular and manageable code structure.
- Large, complex classes can be broken down into smaller, more manageable ones using inheritance.
- New classes with unique features can be created using inheritance.
- Inheritance is a powerful tool that helps to create efficient, flexible, and reusable code in Java.
Terms used in Inheritance
Some terms used in inheritance in Java include:
- Superclass: A class that is extended by a subclass. Also known as a parent class or base class.
- Subclass: A class that extends a superclass. Also known as a child class or derived class.
- Inheriting: The process of a subclass obtaining attributes and methods from its superclass.
- Overriding: The process of a subclass providing its own implementation for a method that is already defined in its superclass.
- Polymorphism: The ability of a subclass object to be used in place of a superclass object, providing more flexibility in the code structure.
- Final class: A class that cannot be extended by any subclasses.
- Final method: A method that cannot be overridden by any subclasses.
- Abstract class: A class that cannot be instantiated and serves as a base class for other classes to extend from.
- Interface: A collection of abstract methods that a class can implement, allowing for multiple inheritance in Java.
Syntax of Java Inheritance
class Subclass extends Superclass {
public Subclass() {
super(); // calling constructor of Superclass
}
}
Subclass obj = new Subclass(); // Creating object of Subclass
(See OOPS in Java)
Let's get to know about multilevel Inheritance now and practice it on online java editor.
Multilevel Inheritance in Java
Multilevel Inheritance in java 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 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.
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 multilevel Inheritance in java?
Multilevel Inheritance in java occurs when a class extends a class that extends another class. This is called multilevel Inheritance in java. For example, class C extends class B, and class B extends class A.
How is multilevel Inheritance implemented in Java?
Multilevel Inheritance in Java is implemented using extends keyword. For example, class C extends class B, and class B extends class A.
Why multiple inheritance is not supported in java?
Java does not support multiple inheritances due to the potential for ambiguity and complexity in the code. Diamond inheritance problems can arise, and multiple interface implementation provides similar functionality without these issues.
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 in Java and have seen its implementation in Java.
Recommended Reading
- Multiple Inheritance in Java.
- Abstraction
- Basics of Java
- Encapsulation in Java
- OOPs Interview Questions
- Hybrid Inheritance in Java
- Features of Object Oriented Programming
- Super Keyword In Java
Also, check out some Guided Paths on topics like Data Structure, 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.