Hierarchical Inheritance in Java

Hierarchical Inheritance in Java

07 Mar 2024
Beginner
426 Views
11 min read
Learn via Video Course & by Doing Hands-on Labs

Java Programming For Beginners Free Course

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.

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.

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);	
}
}     
Now implement the whole program:

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

Output:

   PAge * CAge1 = 10
   PAge * CAge2 = 20
   PAge * CAge3 = 30

Program Explanation:

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 hierarchy inheritance in Java?

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. 

Q2. What is hierarchical inheritance for example?

For example, Physics, Chemistry, and Biology are derived from Science class.

Q3. What is the hierarchy of a Java program?

In Java, the class hierarchy is tree-like. 

Q4. What is a hierarchy in OOP?

A family of classes is known as a class hierarchy.

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

Hierarchy describes a system that organizes or ranks things, often according to power or importance
Share Article
Batches Schedule
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.
Self-paced Membership
  • 22+ Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A
  • 10+ Real-world Projects
  • Career Coaching
  • Email Support
Upto 66% OFF
KNOW MORE..

To get full access to all courses

Accept cookies & close this