Last Updated on January 31, 2023 by Sumit Kumar
In this blog, we will learn about hierarchical inheritance in java followed by a basic introduction about oops and inheritance then types of inheritance, and then a proper explanation of hierarchical inheritance, and reasons to use hierarchical inheritance in java, working of hierarchical inheritance in java, at last, we will discuss the example of hierarchical inheritance.
Inheritance in Java
OOPs (Object-oriented programming ) is one the main or important concepts of any programming language like Java, Python, and C++. And because of this Java is one of the most useful languages in today’s world. The oops has four pillars which are used everywhere in the oops and they are
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
So in this part of the article, we will discuss inheritance. Inheritance is a property of mechanism in which an object acquires all the properties of its corresponding parent object. We use inheritance in order to avoid repeating the code if one object needs the properties of another class we can form a parent-child relationship between them and with the help of inheritance we can use the properties of the parent class. The class that is using the property of one class is known as the child class and the class whose properties are being accessed is known as the parent class.
Types of Inheritance in Java
In java inheritance is mainly of five types, we will name all the types below and will study only hierarchical inheritance in java.
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Keywords used in Inheritance
Class: It is a set of objects that have the same properties or it can also be referred to as the blueprint from which we will create the object.
Child Class: The class which inherits from other classes.
Parent Class: It is the class from which the child class inherits.
What is Hierarchical Inheritance in Java
When Multiple classes i.e, two or more than two classes inherit the same class it is known as a hierarchical inheritance in Java.
Why use Hierarchical inheritance in Java?
Code repetition is frowned upon in programming since it needlessly lengthens the code. It is required of a skilled programmer provides code that is both reusable and clean. One technique that allows for extensive code reuse is inheritance. In reality, utilizing a piece of code many times is made much easier by hierarchical inheritance. It’s time to talk about how hierarchical inheritance in Java functions right now.
How does Hierarchical Inheritance Work in Java?
We already know that in hierarchical inheritance in java multiple classes inherit from a single class or we can simplify it by saying there is one parent class and multiple child classes in the case of hierarchical inheritance in java. And you must remember that to use hierarchical inheritance in java there must be atleast two child classes.
The working of hierarchical inheritance in java is not very difficult as we just need a parent class and many sub-classes that will inherit the parent class first we need to create a parent class and then different child classes so that they can inherit from the parent class. It might sound a bit complex but it is not so let’s now look at the examples of hierarchical inheritance in java to make the concept clear.
Examples of Hierarchical Inheritance in Java
In this section, we will discuss examples of hierarchical inheritance in java We will discuss example of hierarchical inheritance in java and will explain the result properly with output and detailed explanation.
Code Implementation:
class Parent { int parentVariable = 15; } // child class class Child1 extends Parent { int childVariable = 10; } // child class class Child2 extends Parent { int childVariable = 20; } // child class class Child3 extends Parent { int childVariable = 30; } public class Main { public static void main(String[] args) { // object created Child1 child1 = new Child1(); Child2 child2 = new Child2(); Child3 child3 = new Child3(); System.out.println("Parent variable + Child variable of child1 = " + (child1.parentVariable + child1.childVariable)); System.out.println("Parent variable + Child variable of child2 = " + (child2.parentVariable + child2.childVariable)); System.out.println("Parent variable + Child variable of child3 = " + (child3.parentVariable + child3.childVariable)); } }
Output
Parent variable + Child variable of child1 = 25
Parent variable + Child variable of child2 = 35
Parent variable + Child variable of child3 = 45
Explanation of the above code
The above code is an example of hierarchical inheritance in java where we have a parent class with the name parent and we have three child classes with the name Child1, Child2, and Child3 which are inheriting from the same parent class we have declared above. In the output, we are printing the summation value of each of the child class variables with the value of the parent class variable and in the code, you can see we are accessing the value of the parent variable from the object of the derived child classes, Hence we are using inheritance in this and hierarchical inheritance in java to be more specific.
Advantages of Hierarchical Inheritance in Java
- Code reusability: Classes that are derived from a common parent class can inherit its properties and methods, reducing the amount of code that needs to be written.
- Improved organization: By grouping related classes together in a hierarchy, it can make the codebase easier to understand and maintain.
- Easier to add new classes: By adding a new class to the hierarchy, it can easily inherit the properties and methods of the parent class without having to write the same code again.
- Provides a way to represent the "is-a" relationship between objects in the code: Hierarchical inheritance allows you to model the relationship of a subclass being a more specific version of its parent class, which can make the code more intuitive to understand.
Disadvantages of Hierarchical Inheritance in java
- Increased complexity: As the number of classes in the hierarchy increases, it can make the codebase more complex and harder to understand.
- Limited flexibility: If a subclass needs to inherit from multiple classes, hierarchical inheritance is not suitable and other inheritance models such as multiple inheritances should be used.
- Tight Coupling: Subclasses are tightly coupled with their parent classes, thus any changes in the parent class will affect the child classes.
Conclusion
In the above article, we have studied hierarchical inheritance in java, we have discussed briefly inheritance followed by the type of inheritance then we have a detailed explanation of why to use hierarchical inheritance and some of the keywords used in hierarchical inheritance, and then how to use hierarchical inheritance in java followed by an example with proper code and output followed by the explanation of the same.
And at last, we have discussed the advantages and disadvantages of hierarchical inheritance in java.