Programs

What is Hierarchial Inheritance in Java? With Examples

Java is a popular programming language worldwide because of its versatility and compatibility. It finds crucial importance in various fields, including mobile applications, software development and the development of large systems. The mobile operating systems of 88% of the android phones were found to be written in Java in the year 2018. Learning and mastering this programming language opens a broader prospect of employment for the developers. 

Concept of Inheritance in Java:

Inheritance is a mechanism in Java in which an object gains all the properties and characteristics of a parent object. Object-oriented programming systems such as Java considers inheritance as one of the important concepts. The key principle behind the Java inheritance concept is that new classes can be built on the basis of the existing classes. The code blocks and methods of the parent class can be reused while inheritance is incorporated from an existing class. The addition of new fields and methods in a newly created class is also possible while a class is inherited. The inheritance in Java denotes the IS-A relationship that is commonly referred to as a parent-child relationship. Inheritance can be implemented in Java with the use of the keyword ‘extends’.

Note: Inheritance is used in Java to implement method overriding and hence ensure the accomplishment of runtime polymorphism. It can also be used to reuse the code.

Hierarchical Inheritance In Java: How Does It Work?

One superclass and a number of subclasses are included in Java’s hierarchy of inheritance. There must be at least two subclasses for inheritance to take place. When a superclass and numerous subclasses that will inherit from it exist, inheritance can function.

To allow for the flow of properties during inheritance, a superclass must be constructed first, followed by various subclasses. By doing this, the length of the code is drastically shortened, and inheritance improves the comprehension of the code. 

Hierarchical Inheritance in Java: Why Use It?

Code repetition is frowned upon in programming since it unnecessarily lengthens the code. It is required of a good programmer to produce code that is both reusable and clean. One technique that allows for extensive code reuse is hierarchical inheritance in Java.

In reality, one can go for a hierarchical inheritance program in Java to make it much easy and simpler to utilize a piece of code multiple times. So, let’s learn how hierarchical inheritance in Java works with an example. Let’s begin.

Check out our free technology courses to get an edge over the competition.

Explore our Popular Software Engineering Courses

Important Terminologies used in Hierarchical Inheritance in Java:

  1. Class:

A collection of objects with several properties that are common among them is referred to as a Class. It acts as a blueprint or a template for the creation of objects. 

2. Sub Class or Child Class:

A class that extends the other class is called a subclass. A subclass is also referred to as a child class or derived class or extended class. 

3. Super Class or Parent Class:

The class from which the features of the subclass are inherited is called a superclass. It is also called a parent class or a base class.

4. Reusability:

This is a property of the inherited class by virtue of which the fields and methods of a parent class can be reused in the newly created class. All the methods and fields used in the existing class can be used in the new class with this property.

Check out all the Trending Java Tutorial Topics in 2024.

Hierarchical Inheritance in Java

Inheritance is a mechanism in which a class or an object is dependent on another class or object to implement the same behavior of the class or object on which it depends. This permits independent extensions of the novel software via public interfaces and classes. It also helps in the mechanism of reusability of the code segments. If a hierarchical relationship exists between the classes or objects, it is referred to as the hierarchical inheritance. A single class serves as a parent class for several derived classes in the hierarchical inheritance.

Hierarchical inheritance in Java is a type of inheritance in which the same class is inherited by more than one class. In other words, when several classes inherit their features from the same class, the type of inheritance is said to be hierarchical. For example, consider 4 classes A, B, C and D, where A is the class from which B, C and D are inherited. 

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Learn Java Tutorials

Other kinds of Inheritance in Java:

Based on the class, there exist three inheritance types in Java. One of those is already discussed in the previous sections with sufficient example programs. Other than that, Java permits single and multilevel inheritances. In single inheritance, as indicated by the name, one class inherits only one class. i.e., there exists a single subclass for a parent class.  A class can inherit from an existing derived class in Multilevel inheritance. i.e., the base class of the newly formed class is a derived class. It is important to note that Java supports multilevel inheritance but not multiple inheritance. 

Read our Popular Articles related to Software Development

Limitations of Inheritance in Java:

With the use of inheritance, the parent class and the child class are closely coupled. If the code of the superclass is changed, the subclasses are also affected. So, the parent class and child class are dependent on each other. 

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Programme in Software Development – Specialisation in Full Stack Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Hierarchical Inheritance in Java Example: Real-World Example

Here is a Hierarchical Inheritance in Java example for a clearer understanding of how it works. Let’s say every employee in a company receives an average salary or payout of INR 30,000. Now, the interns are entitled to a 25% increase in this average payout and a 50% increase for the full-timers. Show the new salary structure upon the increment. 

Code: 

// parent class
class Employee {
 double salary = 30000;
 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();
 }
}

The parent class Employee is constructed with the variable salary and function displaySalary() to address the aforementioned issue. 

After that, child classes are made using the appropriate raise percentage. The salary value is inherited from the Employee class and increased appropriately in the incrementSalary() function. The displaySalary() method that the parent class inherited is then used to display the salary. 

Output:

  • Full-timers’ salary before increment: INR 30,000
  • Interns’ salary before increment: INR 30,000
  • Full-timers’ salary after increment: INR 45,000
  • Interns’ salary after increment: INR 37,500

Conclusion

  • Hierarchical inheritance in Java is among the four foundational principles of OOPs or Object-Oriented Programming. 
  • Hierarchical inheritance in Java is a type of inheritance allowing multiple child classes to share and distribute the identical parent class’s properties and methods. 
  • Besides shortening the code and making it reusable, hierarchical inheritance in Java makes it more modular. 
  • It takes two or more subclasses that extend (or inherit) the same superclass for there to be hierarchical inheritance. 
Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks