Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Hierarchical Inheritance in Java

Hierarchical Inheritance in Java

12 Jul 2024
Beginner
2.09K Views
11 min read
Learn via Video Course & by Doing Hands-on Labs

Java Online Course Free with Certificate (2024)

Inheritance in Java: An Overview

Inheritance is one of the powerful Object-Oriented Programming concepts in Java. In which a class can inherit attributes and behaviors from superclasses. This allows systematic designing and structuring of classes, enabling access to properties of different methods or classes. In this Java Tutorial, we are going to discuss specifically Hierarchical Inheritance in Java, which will include What is Hierarchical Inheritance in Java? and Why Use Hierarchical Inheritance? We will also explore Hierarchical inheritance in Java with examples. So, Let's first discuss a little bit about "What is Hierarchical Inheritance ?".

What is Hierarchical Inheritance in Java?

  • Hierarchical inheritance is one of the crucial types of inheritance in Object-Oriented Programming.
  • Where more than one class is derived from the parent class.
  • There is a single base class and multiple derived classes.
  • It allows users to 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.

Now Let's see Hierarchical Inheritance with an example in Java compiler.

Read More

Example of Hierarchical Inheritance in Java

First Let's implement Step by step so It will be easy for you

Step 1: Create the Superclass Called ParentClass

class ParentClass
{
  int PAge = 10;
}   

Step 2: Create Subclasses called ChildClass1, ChildClass2, ChildClass3.


class ChildClass1 extends ParentClass
{
  int CAge1 = 1;
}
class ChildClass2 extends ParentClass
{
  int CAge2 = 2;
}
class ChildClass3 extends ParentClass
{
  int CAge3 = 3;
}   

Step 3: Utilize the Subclasses using objects.

classMainpublicstaticvoidmain(String args[])ChildClass1C1=newChildClass1ChildClass2C2=newChildClass2ChildClass3C3=newChildClass3"PAge * CAge1 = ""PAge * CAge2 = ""PAge * CAge3 = "

class ParentClass
{
  int PAge = 10;
}
class ChildClass1 extends ParentClass
{
  int CAge1 = 1;
}
class ChildClass2 extends ParentClass
{
  int CAge2 = 2;
}
class ChildClass3 extends ParentClass
{
  int CAge3 = 3;
}
public class Main
{
  public static void main(String args[])
  {
    ChildClass1 C1 = new ChildClass1();
    ChildClass2 C2 = new ChildClass2();
    ChildClass3 C3 = new ChildClass3();

    System.out.println("PAge * CAge1 = " + C1.PAge * C1.CAge1);	
    System.out.println("PAge * CAge2 = " + C2.PAge * C2.CAge2);	
    System.out.println("PAge * CAge3 = " + C3.PAge * C3.CAge3);	
}
}   
102030
Example of Hierarchical Inheritance in Java

First We created One superclass called ParentClass And Then we Created more three classes called ChildClass1, ChildClass2, and ChildClass3 which inherit the Parent Class. These 3 classes are nothing but derived classes. As shown in the program We created objects C1, C2, and C3 of Child classes and fetched properties of Parent Class through it. That is how the hierarchy works throughout the program. Now let's take a Real-World Example of Hierarchical Inheritance in Java to understand more about it.

Real-World Example of Hierarchical Inheritance in Java

// Superclass
class Company {
  void CompanyName() {
    System.out.println("Company name is Dotnettricks.");
  }
}
// Subclass 1
class Employee1 extends Company {
  void EmployeeName1() {
    System.out.println("EmployeeName1 is Sakshi.");
  }
}
// Subclass 2
class Employee2 extends Company {
  void EmployeeName2() {
    System.out.println("EmployeeName2 is Sourav.");
  }
}
// Main class
public class Main {
  public static void main(String[] args) {
    Employee1 E1 = new Employee1();
    E1.CompanyName(); 
    E1.EmployeeName1(); 
    Employee2 E2 = new Employee2();
    E2.CompanyName(); 
    E2.EmployeeName2(); 
  }
}    

Output:

   Company name is Dotnettricks.
   EmployeeName1 is Sakshi.
   Company name is Dotnettricks.
   EmployeeName2 is Sourav. 

Program Explanation:

Real-World Example of Hierarchical Inheritance in Java

First We created One superclass called Company And Then we Created more two classes called Employee1, and Employee2 which inherit the Parent Class. Employee1 and Employee2 are derived classes. As shown in the program We created objects E1, and E2 of Derived classes and fetched properties of Parent Class "Company" through it As shown in Output.

Use of Hierarchical Inheritance in Java

  • The use of Hierarchical Inheritance in Java plays an important role. Method Overriding is one of Use of it.
  • Method Overriding occurs when the child class has the same method as declared in the parent class.
  • This is mainly used for runtime polymorphism, also known as Dynamic binding.
  • It is implemented by virtual functions and pointers.
  • Hierarchical Inheritance is also used to Increase code reusability.

Which Inheritance Is Not Supported in Java?

  • The Multiple inheritances using classes in Java are not supported.
  • Java only allows for single inheritance, where a class can inherit from only one superclass to avoid the complexities that arise from multiple inheritances.
  • There are very few situations where multiple inheritances are truly necessary.
  • So it is recommended to avoid keeping the codebase simple and manageable.
  • One of the challenges with multiple inheritances is the Diamond problem.

Advantages & Disadvantages of Hierarchical Inheritance in Java

AdvantagesDisadvantages
It allows the developer to reuse existing code in many situations. A class can be created once and it can be reused again and again to create many child classes.In Inheritance base class and child classes are tightly coupled. Hence If you change the code of the parent class, it will affect to all the child classes.
It saves a lot of time and effort to write the same classes again.In a class hierarchy, many data members remain unused and the memory allocated to them is not utilized.
A base class is already compiled and tested properly. This class can be used in a new application without compiling it again. The use of existing classes increases program reliability.It affects the performance of your program if you have not implemented inheritance correctly.

Conclusion

Hierarchical Inheritance in Java is important for managing class hierarchies. By creating a Base class that several Child classes can inherit, developers can save time and make their code more organized and efficient. This makes it easier to function. We explored all concepts related to hierarchical inheritance with its real-time example. I would like to listen to your feedback on this. Also, consider our Java Programming Course to become a master in Java.

FAQs

Q1. What is the advantage of hierarchical inheritance in Java?

In Java, hierarchical inheritance allows many classes to inherit from the same base class, increasing code reuse and consistency. It makes maintenance easier by centralizing shared functions and providing a clear organizational structure.

Q2. What is the highest level of inheritance hierarchy in Java?

The Object class represents the highest level of inheritance hierarchy in Java. Every Java class implicitly derives from Object, which provides fundamental methods such as equals(), hashCode(), and toString() that are accessible to all Java objects.

Q3. What is a real time example of hierarchical inheritance?

A real-world example of hierarchical inheritance is a company's organizational structure. For example, a base class Employee can contain subclasses such as Manager and Developer, which inherit common traits from Employee while adding capabilities specific to their responsibilities.

Q4. What is a hierarchy in OOP?

In Object-Oriented Programming (OOP), a hierarchy is a systematic organization of classes in which subclasses inherit from parent classes, resulting in a treelike structure. This hierarchy enables code reuse and the establishment of a more manageable and logical class structure.

Q5. What is the hierarchy and why is it used?

In OOP, hierarchy refers to the ordering of classes in a tree-like structure, with subclasses inheriting from parent classes. It promotes code reuse, improves organization, and allows for polymorphism by treating objects as instances of their parent classes.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this