Hierarchical Inheritance in Java

Hierarchical Inheritance in Java
Hierarchical Inheritance in Java

Introduction 

Object-Oriented Programming (OOP) is one of the main concepts in the programming world. The concept of OOP is tested in interviews, and hence it becomes essential to know the concepts of OOPs like Inheritance, Abstraction, Encapsulation, and Polymorphism thoroughly. 

Check out the blog Commonly Asked OOPs Interview Questions before you go to your next interview.

This blog will focus on the concept of hierarchical inheritance in Java in detail, along with some real-world examples.

Inheritance

Inheritance is the process of inheriting the properties and behavior 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

Base class and 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:-

  1. Single inheritance
  2. Multilevel inheritance 
  3. Hierarchical inheritance
  4. Multiple inheritance via interfaces
  5. Hybrid inheritance (via interfaces if multiple inheritance 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. 

Hierarchical Inheritance in Java

The type of inheritance in which more than one derived class inherits the properties of the same base class is called hierarchical inheritance. There are multiple child classes and a single parent class. All the child classes will inherit the methods and fields present in the parent class.

Hierarchical inheritance

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.

The Syntax for the above example:

Class P {
 // fields and methods 
}
 
class C1 extends P {
 // fields and methods
}
 
class C2 extends P {
 // fields and methods
}
 
class C3 extends P {
 // fields and methods
}

Let us see an example of hierarchical inheritance in Java.

Code:

// parent class
class P {
 int parentVariable = 5;
}
 
// child class
class C1 extends P {
 int childVariable = 1;
}
 
// child class
class C2 extends P {
 int childVariable = 2;
}
 
// child class
class C3 extends P {
 int childVariable = 3;
}
 
public class Main {
 public static void main(String[] args) {
   // object created
   C1 child1 = new C1();
   C2 child2 = new C2();
   C3 child3 = new C3();
 
   System.out.println("Parent variable + Child variable of child1 = " + (child1.parentVariable + child1.childVariable));
 
   System.out.println("Parent variable + Child variable of child2 = " + (child2.parentVariable + child2.childVariable));
 
   System.out.println("Parent variable + Child variable of child3 = " + (child3.parentVariable + child3.childVariable));
 }
}

Output:

Parent variable + Child variable of child1 = 6
Parent variable + Child variable of child2 = 7
Parent variable + Child variable of child3 = 8

In the above example, the parentVariable of the parent class P is inherited by the child classes C1, C2, and C3.

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:

// 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();
 }
}

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

Frequently Asked Questions

How many classes are required to implement hierarchical inheritance in Java?

At least three classes: two derived classes and one base class must be present to implement hierarchical inheritance. In this case, two child classes can inherit the properties of the common parent class.

How many child classes can be derived from the base class using hierarchical inheritance in Java?

There is no restriction on the number of child classes that can be derived from the common base class. This feature helps programmers write flexible and reusable code.

Hierarchical inheritance is a subset of which type of inheritance?

A mix of two or more types of inheritance is called hybrid inheritance. As hybrid inheritance can contain hierarchical inheritance, hierarchical inheritance is a subset of hybrid inheritance.

Which keyword is used to implement inheritance in Java?

Extends keyword is used to achieve inheritance in Java.

Why is inheritance used in programming?

Inheritance provides code reusability. A child class has to write only the unique features, and the rest of the common variables and methods can be extended from the parent class.

Key Takeaways

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 CodeStudio Platform!

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. 

By: Hari Sapna Nair