Java4teachus

Friday, July 12, 2019

Hierarchical Inheritance In Java

Hierarchical Inheritance 


Definition

If two or more classes are derived from a class then it is called as hierarchical inheritance. (OR) In this inheritance their exist single base class and multiple derived class is called as Hierarchical Inheritance.

Hierarchical-Inheritance-In-Java.jpeg
Class Path
 
 Base class → Derived Class1
 Base class → Derived Class2
 Base class → Derived Class3

Note:- Single Inheritance, Multilevel Inheritance, Hierarchical inheritance are supported both classes and interfaces.

Syntax

class  A
{
-------
-------
}
class B  extends class A
{
-------
-------
}
class C  extends class A
{
-------
-------
}

➤  In the above example We have create one base class called A.
➤  Another derived class B calls the features from base class A by using extends keyword. which is shown below program.

Program

class Hero
{
    void show()
    {
        System.out.println("Hero");
    }
}
class Honda extends Hero
{
    void show1()
    {
        System.out.println("Honda");
    }
}
class Suziki extends Hero
{
    void show2()
    {
        System.out.println("Suziki");
    }
}
class HeroHonda
{
    public static void main(String[] args)
    {
        System.out.println("================");
        Honda h=new Honda();
        h.show();
        h.show1();
        Suziki s=new Suziki();
        s.show();
        s.show2();
        System.out.println("================");
    }
}

Output:-
Hierarchical-Inheritance-Programs-In-Java.jpeg

Program

class Lakshmi
{
    void show()
    {
        System.out.println("Lakshmi");
    }
}
class Lucky extends Lakshmi
{
    void show1()
    {
        System.out.println("Lucky");
    }
}
class Bujji extends Lakshmi
{
    void show2()
    {
        System.out.println("Bujji");
    }
}
class LakshmiLucky
{
    public static void main(String[] args)
    {
        System.out.println("================");
        Lucky l=new Lucky();
        l.show();
        l.show1();
        Bujji b=new Bujji();
        b.show();
        b.show2();
        System.out.println("================");
    }
}
Output:-
Hierarchical-Inheritance-Program-In-Java.jpeg

No comments:

Post a Comment

Thanks for visiting my site..