Hierarchical Inheritance in Java
Hierarchical Inheritance is one of the types of inheritance where multiple child classes inherit the methods and properties of the same parent class.
As child classes get access to the methods and properties of the parent class using Hierarchical inheritance, they don't have to bother about creating those methods and properties separately, they just need to create the class-specific methods or properties.
This also reduces code redundancy as code hardly gets repeated. Hence, Hierarchical inheritance ultimately makes the code reusable, less redundant, modular, and more readable.
Hierarchical Inheritance in Java
OOPs (Object Oriented Programming System) is one of the most important concepts in programming and it's one of those features that make Java a very powerful programming language. OOPs has four pillars:
So Inheritance is one of the four pillars of OOPs and it has 5 types:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
In this article, we will be discussing Hierarchical Inheritance only. So without further ado, let's start.
Why to use Hierarchical Inheritance in Java?
In programming, code repetition is considered bad practice as it unnecessarily increases the length of the code. A good programmer is expected to deliver code that is both clean and reusable. You may read Clean Code in Java article if you want to learn more about clean code in Java. Inheritance is one of the ways using which code can be reused to a great extent. In fact, Hierarchical inheritance is very helpful in reusing a piece of code. Now it's time to discuss how does hierarchical inheritance work in Java? Here we go.
How does Hierarchical Inheritance Work in Java?
We already know that "Hierarchical inheritance" occurs when multiple child classes inherit the methods and properties of the same parent class. This simply means we have only one superclass and multiple sub-classes in hierarchical inheritance in Java.
The idea is simple: To use the methods and properties of the parent class instead of creating them again in the sub-class. This not only reduces the code length but also increases the code modularity.
Note: For Hierarchical inheritance to occur, there must be at least two sub-classes that extend (or inherits) the same superclass.
The working of hierarchical inheritance in Java is very straightforward. We need a base class and some subclasses that inherit the base class. Hence, first, we create a base class (also called as Superclass), then we create some sub-classes which inherit that same base class. This is how we can use the code of the base class in sub-classes of that base class.
Theoretically, this might sound a bit complex but it's not. Let us understand this with the help of some examples.
Examples of Hierarchical Inheritance in Java
For better understanding, we will start with explanation of a basic example of Hierarchical inheritance in Java then we will move towards a real-world example of Hierarchical inheritance in Java.
Basic Example of Hierarchical Inheritance in Java
- In the above-given example, we have one parent class(base class) called BaseClass and three subclasses that inherit the BaseClass.
- We created the object of each subclass. Then we multiplied the parentNum variable which belongs to the parent class with the variable of each child class.
- Even though the parentNum variable is not present in the child classes, we are able to access it very easily. This is just because of Hierarchical Inheritance in Java.
Remember: There is no limit on the sub-classes of one parent class. This simply means that any number of child classes can inherit the methods and properties of the same parent class.
Real-World Example of Hierarchical Inheritance in Java
- In the above-given example, we created a parent class(base class) called Vehicle and two subclasses TwoWheeler and FourWheeler which extends the base class Vehicle.
- Hence apart from accessing their own methods and properties, these classes can also access the methods and properties of the "Vehicle" class.
- Here, the subclasses got access to the variable basePrice and the method showPrice() of the base class.
- We created two objects bike and car from the sub classes TwoWheeler and FourWheeler respectively.
- We modified the price of the bike and car by referring to the basePrice of the vehicle in the base class and the increasePriceBy variable of each subclass respectively. This is how we utilized the power of Hierarchical inheritance in Java.
Which Inheritance is Not Supported in Java?
In Java, multiple inheritance is not supported. Multiple inheritance refers to the ability of a class to inherit from more than one superclass.
The reason for not supporting multiple inheritance in Java is to avoid the complications and ambiguities that can arise from inheriting characteristics and behaviors from multiple sources. Multiple inheritance can lead to conflicts when two superclasses have conflicting definitions for the same method or attribute.
Instead of multiple inheritance, Java supports single inheritance, where a class can inherit from only one superclass. However, Java provides an alternative mechanism called interfaces, which allow a class to implement multiple interfaces. Interfaces define a contract of methods that a class must implement, enabling the achievement of similar functionality as multiple inheritance through interface implementation.
To learn more about inheritance in Java and the reasons behind the lack of support for multiple inheritance, you can refer to this article on which inheritance is not supported in Java.
- Inheritance is one of the four pillars of OOPs(Object Oriented Programming System).
- Hierarchical inheritance is one of the types of inheritance where multiple child classes inherit the methods and properties of the same parent class.
- Hierarchical inheritance not only reduces the code length but also increases the code modularity.
- For Hierarchical inheritance to occur, there must be at least two or more sub-classes that extend (or inherits) the same superclass.
- The base class is also called superclass or parent class while the sub-classes are also called child classes.
- There is no limit on the sub-classes of one parent class. This simply means that any number of child classes can inherit the methods and properties of the same parent class.