Search anything:

Hierarchical Inheritance In Java

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article at OpenGenus, we are going to learn about Hierarchical Inheritance in Java along with a Java code example.


  1. Inheritance
  2. Types Of Inheritance
  3. Hierarchical Inheritance
  4. Code Sample
  5. Applications


Inheritance is one of the pillars of object-oriented programming. It allows a class to acquire the properties and behaviors of another class.
The main advantage of inheritance is code reusability and method overriding (runtime polymorphism).


class Superclass
// superclass data variables and member functions
class Subclass extends Superclass
// subclass data variables and member functions


In Java, there are several types of inheritance that can be used to establish relationships between classes. Some of the common types of inheritance include:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance


Hierarchical Inheritance, also known as tree-structured inheritance.
In Hierarchical inheritance multiple child classes inherit from a single parent class. Parent class serves as a common base class from which multiple child classes are derived. This type of inheritance is called "hierarchical" because it forms a hierarchy or a tree structure.

Hierarchical inheritance is useful in scenarios where you have a group of related classes that share common characteristics but also have distinct features. By defining those common characteristics in the superclass, you can avoid code duplication and ensure code reusability.




import java.util.*;

class School 
     void schoolName() 
         System.out.println("Nagarjuna High School");

class Teacher extends School 
      void teach()
        System.out.println("Teacher teaches science.");

class Student extends School
    void classStudying() 
     System.out.println("Student is in 8th standard");

public class Main 
    public static void main(String[] args)
     Teacher teacher = new Teacher();
     teacher.schoolName();   // Output: Nagarjuna High School
     teacher.teach();   // Output: Teacher teaches science

     Student student = new Student();
     student.schoolName();   //  Output:  Nagarjuna High School
     student.classStudying(); // Output: Student is in 8th standard

The above code demonstrates an example of hierarchical inheritance in Java.
Here, the School class acts as the superclass, which has a method schoolName() and The Teacher class is a subclass of School. It has a method teach() and The Student class is also a subclass of School which has a method classStudying().

Both the subclasses inherits the schoolName() method from the School class.
we create instances of both Teacher and Student. We can access the methods of the superclass (schoolName()) as well as the methods specific to each subclass (teach() for Teacher and classStudying() for Student).


  • File System Organization: Hierarchical inheritance is used in representing file systems, where directories and files have a hierarchical structure. Here, FileSystem is the super class and directories, files acts as subclasses.

  • User Interface Development: In UI development, hierarchical inheritance is used to define common properties and behavior in a superclass for UI components. Subclasses inherit from the superclass and add specific functionalities or visual elements.

  • Organizational Structures: Hierarchical inheritance helps in modelling organizational structures, where a "Employee" superclass defines common attributes. Subclasses like "Manager," "Engineer," and "Salesperson" inherit from it, adding specific attributes and behaviors.

  • Data Analysis and Visualization: In data analysis and visualization, hierarchical inheritance is used with a "Plot" superclass defining common functionalities. Subclasses like "BarPlot," "LinePlot," and "ScatterPlot" inherit from it, providing specialized functionalities for specific plot types.

  • Biological Classification:
    Hierarchical inheritance in biological classification categorizes organisms, with the superclass "Organism" and subclasses like "Mammal," "Bird," and "Fish."

With this article at OpenGenus, you must have the complete idea of Hierarchical Inheritance in Java.

Hierarchical Inheritance In Java
Share this