BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Hierarchical Inheritance in java with example program

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

When more than one classes inherit a same class then this is called hierarchical inheritance. For example class B, C and D extends a same class A. Lets see the diagram representation of this:
Hierarchical Inheritance

As you can see in the above diagram that when a class has more than one child classes (sub classes) or in other words more than one child classes have the same parent class then this type of inheritance is known as hierarchical inheritance.

If you find any difficulty in understanding the following example then refer this guide:
Java – Inheritance

Example of Hierarchical Inheritance

We are writing the program where class B, C and D extends class A.

class A
{
   public void methodA()
   {
      System.out.println("method of Class A");
   }
}
class B extends A
{
   public void methodB()
   {
      System.out.println("method of Class B");
   }
}
class C extends A
{
  public void methodC()
  {
     System.out.println("method of Class C");
  }
}
class D extends A
{
  public void methodD()
  {
     System.out.println("method of Class D");
  }
}
class JavaExample
{
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     D obj3 = new D();
     //All classes can access the method of class A
     obj1.methodA();
     obj2.methodA();
     obj3.methodA();
  }
}

Output:

method of Class A
method of Class A
method of Class A
❮ PreviousNext ❯

Top Related Articles:

  1. Multithreading in java with examples
  2. Java – Static Class, Block, Methods and Variables
  3. Java date difference
  4. How to write to file in Java using BufferedWriter
  5. Final Keyword In Java – Final variable, Method and Class

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. prabhu says

    July 14, 2014 at 8:45 AM

    Good Example Dude

    Reply
    • swetha says

      August 7, 2015 at 11:23 AM

      Have you really understood this? if so explain..plz

      Reply
      • gokul says

        October 22, 2015 at 8:31 AM

        Yes..

        The methodB() is defined in both class B and Myclass but there is no object created for Myclass so method inside this class will never be invoked at all.. Where as method of class A is invoked through objects of class B C and D respectively… Don’t get confused with method overriding..

        Reply
        • Vikki says

          June 4, 2016 at 12:20 PM

          But what is the use of placing Methodb() in myclass when it is not invoked.

          Reply
  2. Abdulkarim says

    July 22, 2014 at 1:09 PM

    its clear and simple. thanks for the explanatory example.

    Reply
  3. pooja says

    November 28, 2014 at 11:06 AM

    can u please explain why my class is used here?

    Reply
    • namrata says

      September 1, 2015 at 2:59 AM

      myclass is the name of the class. u can give any name to the class .
      eg: myprogram , abc, alpha etc.

      Reply
  4. Anitha says

    August 7, 2015 at 11:20 AM

    you have used only a…you are not at all inherited methods of b,c ……then how hierarchical exists..

    Reply
    • lovi says

      February 24, 2016 at 12:11 PM

      yes , by interface we use ‘a’

      Reply
  5. ashwin says

    November 18, 2015 at 2:00 PM

    Can u explain me why u method b is used in myclass…plzzz

    Reply
    • eduardo says

      January 3, 2016 at 11:17 PM

      There is no specific reason…. If you call methodA() on myclass object it wont work since it does not have have access to that method but if we extend method B and have the same method within myclass then this would be method overloading…

      class A
      {
      public void methodA()
      {
      System.out.println(“method of Class A”);
      }
      }
      class B extends A
      {
      public void methodB()
      {
      System.out.println(“method of Class B”);
      }
      }
      class C extends A
      {
      public void methodC()
      {
      System.out.println(“method of Class C”);
      }
      }
      class D extends A
      {
      public void methodD()
      {
      System.out.println(“method of Class D”);
      }
      }
      class MyClass extends B
      {
      public void methodB()
      {
      System.out.println(“method of Class B”);
      }
      public static void main(String args[])
      {
      B obj1 = new B();
      C obj2 = new C();
      D obj3 = new D();
      obj1.methodA();
      obj2.methodA();
      obj3.methodA();

      MyClass a = new MyClass();
      a.methodA();
      a.methodB();
      }
      }

      Output:
      method of Class A
      method of Class A
      method of Class A
      method of Class A
      method of Class B

      Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap