Java4teachus

Monday, July 08, 2019

Multilevel Inheritance In Java

Multi-Level Inheritance


Definition

If a class is derived from the class which is derived from the another class then it is also known as multi-level inheritance.  (or)  In this inheritance their exists single base class, single derived class and multiple intermediate base classes. which is shown in the below example.

Example
Mutlilevel-Inheritance-In-Java.jpeg
Inheritance Path : Class1→Class2→Class3→Class4
 
Note:- In the above example Base class is one, Derived class is also one but Intermediate class are  multiple.

Syntax:

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

➧  In the above syntax  we have one Base class called class A.
➧ An Another class B called as Intermediate Base class and we call the all features of class A by using extends keyword.
➧  A  class C is an derived class calling from the features called Class B by using extends keyword.

Program

class Alpha
{
    void show()
    {
        System.out.println("This is Alpha");
    }
}
class Beta extends Alpha
{
    void show1()
    {
        System.out.println("This is Beta");
    }
}
class Gamma extends Beta
{
    void show2()
    {
        System.out.println("This is gamma");
    }
}
class AlphaBetaGamma
{
    public static void main(String[] args)
    {
        System.out.println("==================");
        Gamma g=new Gamma();
        g.show();
        g.show1();
        g.show2();
        System.out.println("==================");
    }
}
OutPut:

AlphaBetaGamma-on-multilple-inheritance-in-java.jpeg

Program:2 

class Tenth
{
    void schoolName()
    {
        System.out.println("Brilliant E.M School");
    }
    void marksGain()
    {
        System.out.println("Marks Secured in Tenth 490");
    }
}
class Inter extends Tenth
{
    void interClgName()
    {
        System.out.println("Sri sai krupa Jr College");
    }
    void marksGain1()
    {
        System.out.println("Marks Secured in Inter 586");
    }
}
class Degree extends Inter
{
    void degreeClgName()
    {
        System.out.println("Sri Sai Krupa Degree Collge");
    }
    void marksGain2()
    {
        System.out.println("Marks Secured in Degree 80.1");
    }
}
class Education
{
    public static void main(String[] args)
    {
        Degree d=new Degree();
        d.schoolName();
        d.marksGain();
        d.interClgName();
        d.marksGain1();
        d.degreeClgName();
        d.marksGain2();
    }
}

OutPut:

Multiple-Inheritance-Program-in-java.jpeg
 

No comments:

Post a Comment

Thanks for visiting my site..