Hierarchical inheritance – JAVA

The form of inheritance in which more than one classes are derived from single base class is known as hierarchical inheritance. We can implement the hierarchical inheritance by defining at least three classes in which one is base class and the remaining two classes is derived from the same one base class.

SYNTAX:-

class base_class_name

{

…………….

…………….

}

class derived_class_name1 extends base_class_name

……………

……………

}

class derived_class_name2 extends base_class_name

…………..

…………..

}

//PROGRAM TO SHOW HIERARCHICAL INHERITANCE  – JAVA

class Base

{

void showBase()

{

System.out.println(“Base class method”);

}

}

class Derived1 extends Base

{

void showDerived1()

{

System.out.println(“method of Derived1”);

}

}

class Derived2 extends Base

{

void showDerived2()

{

System.out.println(“method of Derived2”);

}

}

class TestHierarchical

{

public static void main(String args[])

{

Derived1 d1=new Derived1();

Derived2 d2=new Derived2();

d1.showDerived1();

d1.showBase();

d2.showDerived2();

d2.showBase();

}

}

Leave a Reply

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