beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Hierarchical Inheritance in java with example program

By Chaitanya Singh | Filed Under: OOPs Concept

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 ❯

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 Index
  • Java Introduction
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

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

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • 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 Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap