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:
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
prabhu says
Good Example Dude
swetha says
Have you really understood this? if so explain..plz
gokul says
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..
Vikki says
But what is the use of placing Methodb() in myclass when it is not invoked.
Abdulkarim says
its clear and simple. thanks for the explanatory example.
pooja says
can u please explain why my class is used here?
namrata says
myclass is the name of the class. u can give any name to the class .
eg: myprogram , abc, alpha etc.
Anitha says
you have used only a…you are not at all inherited methods of b,c ……then how hierarchical exists..
lovi says
yes , by interface we use ‘a’
ashwin says
Can u explain me why u method b is used in myclass…plzzz
eduardo says
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