Hierarchical inheritance is the type of inheritance in object-oriented programming where more than one class is derived from the parent class, i.e., there is a single base class and multiple derived classes. It allows for code reusability and promotes modularity. It enables the customization of classes. However, changes to the base class can impact all derived classes and makes the system less flexible.
This blog will focus on the concept of hierarchical inheritance in Java in detail, along with some real-world examples.
Understanding Inheritance in Java
Inheritance is the process of inheriting the properties and behaviour of an existing class into a new class. The existing class is called the parent class or the base class, and the new class is called the child class or the derived class.
Inheritance is just like a parent and a child in the real world. As the child inherits the properties of their parents in the real world, similarly in programming, the child class inherits the parent class's method and fields.
The different types of inheritance supported by Java are:-
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritances via interfaces
- Hybrid Inheritance in Java (via interfaces if multiple inheritances is used)
For a better understanding of inheritance, check out the blog Inheritance in Java. In this blog, we will focus on hierarchical inheritance in Java.
What is Hierarchical Inheritance in Java?
Hierarchical Inheritance in Java is a type of class inheritance where a parent class can have multiple child classes. In this form of inheritance, one class can serve as the parent class or base for several child classes. In turn, each of them can have their own child classes. In simple words, Hierarchical inheritance is a type of inheritance in Java where multiple derived classes inherit the properties of a parent class. It allows all the child classes to inherit methods and fields from their parent class.
In the above example, the child classes: Class C1, Class C2, and Class C3 inherit the same parent class, Class P. The methods and fields available in Class P can be used by the child classes.
How does Hierarchical Inheritance Work in Java?
Hierarchical Inheritance in Java includes one superclass and various subclasses. For the inheritance to occur, there must be at least two subclasses.
The working of inheritance happens when there is a superclass and many subclasses which will inherit from the superclass. First, the superclass is to be created, and then different subclasses are to be made such that the flow of properties in inheritance occurs. By using this, the code length gets significantly reduced, and inheritance also increases the readability of the code.
Why to use Hierarchical Inheritance in Java?
The use of Hierarchical Inheritance in Java plays important roles. Method Overriding is one of them.
Method Overriding occurs when the subclass has the same method as declared in the parent class. This is mainly used for runtime polymorphism, also known as Dynamic binding or Late binding. It is achieved by virtual functions and pointers. Some of the rules to achieve polymorphism are
- Child and parent class methods must have the same name.
- The methods of child and parent classes must have the same parameter.
- Inheritance is mandatory.
In addition to that, Hierarchical Inheritance is also used to enhance code reusability. Code reusability in Java is the ability to repurpose already existing code when implementing new ones.
Example of Hierarchical Inheritance in Java
In this example, a single parent class called Parent is inherited by three child classes. In the main function, objects are created from all the classes, and methods are called to demonstrate the inheritance.
Real-World Example of Hierarchical Inheritance in Java
Let's now see a real-world example to understand the concept of hierarchical inheritance in Java.
Problem Statement: Every employee has a standard salary of Rs.50000. For a full-time employee, increment the salary by 50%, and increment the salary by 25% for an intern. After increasing the salary, display the incremented salary.
To solve the above problem, a parent class Employee is created with the variable salary and function displaySalary(). Then child classes are created with the respective hike percentage. In the incrementSalary() function, the salary value is inherited from the Employee class and incremented accordingly. Finally, the salary is displayed by using the displaySalary() inherited by the parent class.
Salary of a full-time employee before incrementing: Rs.50000.0 Salary of an intern before incrementing: Rs.50000.0 Salary of a full-time employee after incrementing: Rs.75000.0 Salary of an intern after incrementing: Rs.62500.0
Advantages of Hierarchical Inheritance in Java
The following are some advantages of Hierarchical Inheritance in Java:-
- Code Reusability: Hierarchical inheritance promotes code reusability by allowing multiple subclasses to inherit common properties and methods from a single superclass.
- Organization and Structure: It provides a clear and organized structure to your codebase. Related classes can be grouped under a common superclass.
- Extensibility: It allows you to easily extend functionality in your applications by adding subclasses with common functionalities.
- Polymorphism: Hierarchical inheritance supports polymorphism, which enables you to write code that can work with objects of different classes in a consistent manner.
Disadvantages of Hierarchical Inheritance in Java
The following are some disadvantages of Hierarchical Inheritance in Java:-
- Rigidity: Hierarchical inheritance enforces a strict parent-child relationship between classes which can make it difficult to adapt to changes or accommodate new requirements without modifying the entire hierarchy.
- Increased Complexity: As the hierarchy gets deeper, it can become complex to maintain the code. For other developers, understanding the relationships can be challenging as well.
- Risk of Tight Coupling: Changes to the superclasses can add unintended side effects to the subclasses due to the tight coupling.
- Limited Code Reusability: Hierarchical inheritance promotes code reusability for closely related classes, but it may not help with code reusability for classes that do not fit into the existing hierarchy.
Frequently Asked Questions
What is hierarchical inheritance with example?
Inheritance is the process of inheriting the properties and behaviour of an existing class into a new class. The existing class is called the parent class, and the new class is called the child class. This goes well with the example of a parent and a child. The child inherits properties from the parent, like hair colour, eye colour and much more.
What is the difference between single and hierarchical inheritance in Java?
In the case of Single Level inheritance, a class inherits properties from a single class. For example, Class 2 inherits Class 1. Whereas, in the case of Hierarchical Inheritance, multiple classes inherit properties from a single class. For example, Class 2 inherits Class 1, and Class 3 inherits Class 1.
What is the difference between hierarchical and hybrid inheritance in Java?
Inheritance where many subclasses inherit from one single class, is known as Hierarchical Inheritance. Whereas, Hybrid inheritance is the combination of two or more types of inheritance. For example, the Father inherited the properties of the Grandfather and the children inherit properties from the Father. In a way, Father and the Grandfather are the Superclas and the child is the subclass.
In this blog, we covered hierarchical inheritance in Java in detail, along with some examples. Now that you know the various types of inheritance, try out some questions based on inheritance on our Coding Ninjas Studio Platform!
- C++ vs Java
- Introduction to C#
- Super Keyword In Java
- Fibonacci Series Program in Python
- Access modifiers in java
Don't stop here. Check out our Java-guided path to learn Java from scratch. We hope you found this blog useful. Feel free to let us know your thoughts in the comments section.