Do you think IIT Guwahati certified course can help you in your career?
No
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."
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.
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.
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.
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); } }
You can also try this code with Online Java Compiler
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 of Multilevel Inheritance
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.
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 multiple inheritance and multilevel inheritance in Java?
Multiple inheritance in Java refers to a scenario where a class inherits properties and behavior from more than one parent class. Multilevel inheritance involves a class inheriting from another derived class, creating a hierarchy.
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 single vs multi level inheritance?
Single inheritance involves inheriting from only one parent class, while multilevel inheritance involves inheriting from multiple levels of classes.
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
In this article, we have thoroughly discussed the concept of Multilevel Inheritance in Java and also discussed its implementation in Java. Multilevel inheritance in Java provides a powerful mechanism for creating complex class hierarchies. By allowing classes to inherit properties and behavior from other derived classes, it promotes code reusability and maintains a clear hierarchical structure.