Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
How does Hierarchical Inheritance Work in Java?
2.
Why to use Hierarchical Inheritance in Java?
3.
Example of Hierarchical Inheritance in Java
3.1.
Java
4.
Real-World Example of Hierarchical Inheritance in Java
4.1.
Java
5.
Which Inheritance is Not Supported in Java?
6.
Advantages of Hierarchical Inheritance in Java
7.
Disadvantages of Hierarchical Inheritance in Java
8.
Frequently Asked Questions
8.1.
What is hierarchical inheritance with example?
8.2.
What is the difference between single and hierarchical inheritance in Java? 
8.3.
What is the difference between hierarchical and hybrid inheritance in Java?
9.
Conclusion
Last Updated: Jun 10, 2024
Medium

Hierarchical Inheritance in Java

Hierarchical inheritance in Java is a situation where multiple subclasses inherit from a single-parent class. 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 child 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 make the system less flexible.

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.

Code

  • Java

Java

// Parent class
class Parent {
void parentMethod() {
System.out.println("This is a method from the Parent class.");
}
}

// Child1 class, extends Parent
class Child1 extends Parent {
void child1Method() {
System.out.println("This is a method from Child1 class.");
}
}

// Child2 class, extends Parent
class Child2 extends Parent {
void child2Method() {
System.out.println("This is a method from Child2 class.");
}
}

// Child3 class, extends Parent
class Child3 extends Parent {
void child3Method() {
System.out.println("This is a method from Child3 class.");
}
}

public class Main {
public static void main(String[] args) {
Parent parentObj = new Parent();
Child1 child1Obj = new Child1();
Child2 child2Obj = new Child2();
Child3 child3Obj = new Child3();

// Calling methods from the Parent and Child classes
parentObj.parentMethod();
child1Obj.parentMethod();
child1Obj.child1Method();
child2Obj.parentMethod();
child2Obj.child2Method();
child3Obj.parentMethod();
child3Obj.child3Method();
}
}
You can also try this code with Online Java Compiler
Run Code

Output

output

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.

Code

  • Java

Java

// parent class
class Employee {
double salary = 50000;


void displaySalary() {
  System.out.println("Employee Salary: Rs."+salary);
}
}


// child class
class FullTimeEmployee extends Employee{
double hike = 0.50;


void incrementSalary() {
  salary = salary + (salary * hike);
}
}


// child class
class InternEmployee extends Employee{
double hike = 0.25;


void incrementSalary() {
  salary = salary + (salary * hike);
}
}


public class Main {
public static void main(String[] args) {
  // object created
  FullTimeEmployee emp1 = new FullTimeEmployee();
  InternEmployee emp2 = new InternEmployee();


  System.out.println("Salary of a full-time employee before incrementing:");
  emp1.displaySalary();


  System.out.println("Salary of an intern before incrementing:");
  emp2.displaySalary();


  // salary incremented
  emp1.incrementSalary();
  emp2.incrementSalary();


  System.out.println("Salary of a full-time employee after incrementing:");
  emp1.displaySalary();


  System.out.println("Salary of an intern after incrementing:");
  emp2.displaySalary();
}
}
You can also try this code with Online Java Compiler
Run Code


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.

Output

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

Which Inheritance is Not Supported in Java?

In Java, the type of inheritance that is not supported is called "Multiple Inheritance." This means a class cannot inherit (get features from) more than one class.

Let's use an example to understand this better. Imagine you have two classes:

Class A with a method called draw.
Class B with a method called paint.
 

If you try to create a new class, say Class C, that wants to inherit from both Class A and Class B to use both draw and paint methods, Java won't allow it. This is because if both Class A and Class B had methods with the same name and parameters, Java wouldn't know which one to use in Class C.

However, Java provides an alternative called "Interfaces" to get around this. An interface is like a contract that classes can agree to follow, and a class can implement (agree to follow) multiple interfaces. This way, Java ensures a class can still use features from multiple sources without the confusion of multiple inheritance.

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.

Conclusion

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!

Recommended Readings:


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. 

Live masterclass