MULTILEVEL INHERITANCE

In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. ClassBinherits the property of ClassA and again ClassB act as a parent for ClassC. In Short ClassA parent for ClassB andClassB parent for ClassC.
Multilevel_Inheritance_in_Java
MultiLevel Inheritance Example
public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassB
{
    public void dispC()
    {
        System.out.println("disp() method of ClassC");
    }
    public static void main(String args[])
    {
        //Assigning ClassC object to ClassC reference
        ClassC c = new ClassC();
        //call dispA() method of ClassA
        c.dispA();
        //call dispB() method of ClassB
        c.dispB();
        //call dispC() method of ClassC
        c.dispC();
    }
}
Output :
disp() method of ClassA
disp() method of ClassB
disp() method of ClassC

No comments:

Post a Comment