Hierarchical Inheritance in Java

Dec 16, 2022

4 min read

Write your own content on FeedingTrends
Write

Object-oriented programming concepts have made programming more dynamic and easier to implement and java is no exception. It is touted among the most popular and powerful programming languages. 

Now that we are talking about OOPS concepts and related programming languages, the discussion is incomplete without mentioning inheritance. It is defined as the ability to inherit methods and properties from the base class to the superclass. 

Inheritance is mostly used to decrease the length of the code and to promote code reusability. This itself is a wide concept and is of different types based on class structures. One such type of inheritance is hierarchical inheritance in java. 

To give you a better understanding of this hierarchical inheritance, we have come up with this complete guide. Here, learn everything you need to know about hierarchical inheritance in detail. But before that, let's understand some basic terms of inheritance that you must be aware of. 

Basic Terms Used In Inheritance

  • Class: It is a collection of objects which have the same characteristics and behaviour. One thing here you need to note is that a class is simply a blueprint according to which an object is created. 

  • Child Class: A child class is a class that inherits properties from another class. You can add more methods and variables to the child class along with inheriting the methods of properties of the parent class. 

  • Parent Class: A parent class is the one class whose methods and variables are inherited by other classes. 

  • Reusability: With the help of inheritance, code reusability is promoted. Here, you can use the code that is already written. 

What Is Hierarchical Inheritance In Java? 

When it comes to hierarchical inheritance in Java, there is only one superclass whereas there are multiple subclasses. In simpler terms, multiple subclasses inherit the properties of one superclass. 

Now that the child can access the methods and properties of the parent class, there is no need to create the same methods again and again. This whole inheritance process reduces code redundancy and enhances code readability. 

Why Hierarchical Inheritance Is Used? 

Hierarchical inheritance in Java is used crucially for the following reasons:

  • Method overriding: You can only implement the method overriding concept with the help of inheritance. The overriding concept allows the child class to specifically implement a method that is already defined by one of the superclasses. With the help of method overriding, java can achieve run-time polymorphism.

  • Code Reusability: The code that you will write in the superclass will be available for all the other classes. Therefore, there is no need to write the same code in child classes as it only needs to inherit them from the parent class. 

  • Abstraction: Abstraction implies that only the required functionality is shown to the user. This abstraction can be achieved only with the help of inheritance. 

Syntax of Hierarchical Inheritance In Java

Class A{

Methods and variables

}

Class B extends A{

Methods and variables

}

Class C extends A{

Methods and variables

}

Here, in the syntax, class A is the base class whose properties are being inherited by other classes B and C. However, one thing that you need to note here is that there is no relation between class B and C. 

Real-Life Example Of Hierarchical Inheritance

Imagine that each employee in the firm has a base salary of 50000. When it comes to a full-time employee, the salary is incremented by 50% whereas, for interns, the salary is incremented by 25%. You are required to write a code to display the salary of an employee after an increment. 

To resolve this problem, you will first have to create a superclass Employee in which you have to mention a variable salary and a function displaysalary. 

Now, you will have to create child classes and inherit salary value from the superclass to increment the salary as per the decided percentage. In the end, you will have to inherit the display salary function from the base class. 

Important Points To Remember About Inheritance

Before implementing hierarchical inheritance in Java, you must keep the  following points in mind. 

  • Private member inheritance: The foremost thing that you need to keep in mind is that you can not inherit private members of the superclass. A subclass can only inherit protected and public members of the class to indirectly use the private members of the superclass. 

  • Constructor Inheritance: While implementing constructors, you will have to keep in mind that you can not inherit constructors of a class. This is because a constructor is not a member of the class. However, you can invoke the constructor of a superclass from a subclass. 

  • Default superclass: In inheritance, each class can have only one superclass. However, if there is no explicit superclass present, the class will implicitly have an object class as its superclass. Here, you must know that there is no superclass for the object class. 

  • Only One Superclass Is Allowed: In inheritance, there is only one superclass but there are multiple subclasses. But you must know that there can be only one superclass. 

Now that we are discussing a type of inheritance, another very important type of inheritance is multilevel inheritance in Java. 

When it comes to multilevel inheritance in Java, a subclass will inherit a base class and that subclass will act as a base class for another subclass. 

To understand it, consider the following structure:

Class A{

Methods and variables

}

Class B extends A{

Methods and variables

}

Class C extends B{

Methods and variables

}

In this way, B is a derived class for class A whereas it is a superclass for class C. 

Conclusion

Hierarchical inheritance in Java allows multiple subclasses to inherit properties of a single superclass. This type of inheritance allows you to reduce the length of the code and at the same time increases code reusability. 

The concept of inheritance is also used to design real-life applications. Hence every developer and programmer must be aware of the same. 

Write your own content on FeedingTrends
Write