Hierarchical Inheritance

In Hierarchical Inheritance, there is common Base class for more than one derived class. Below is an Example of Hierarchical Inheritance

class A8
{
	public A8(int i)
	{
		System.out.println("A8() constructor:"+i);
	}

	public void show()
	{
		System.out.println("show() in A8");
	}
}

class B8 extends A8
{
	public B8()
	{
		super(0);
		System.out.println("B8() constructor");
	}

	public void met1()
	{
		System.out.println("met1() in B8");
	}
}

class C8 extends A8
{
	public C8()
	{
		super(20);
		System.out.println("C8() constructor");
	}

	public void met2()
	{
		System.out.println("met2() in C8");
	}
}

public class HierarchicalInheritance {
public static void main(String args[])
{
	B8 obj = new B8();
	System.out.println();
	C8 obj1 = new C8();

	obj.show();
	obj.met1();

	obj1.show();
	obj1.met2();
}
}


Output:
A8() constructor:0
B8() constructor

A8() constructor:20
C8() constructor
show() in A8
met1() in B8
show() in A8
met2() in C8

You may also like to read:
Java Multilevel Inheritance
What is final class

3 thoughts on “Hierarchical Inheritance”

Leave a Reply