Hierarchical Inheritance in Java With Example Program
Hierarchical Inheritance in Java With Example Program
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.
4
2.Hierarchical Inheritance[Content:Program, Necessary assumption]
}
}
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