Multilevel-Inheritance-in-Java .

In this session i have discussed multilevel inheritance in Java .


In multilevel inheritance the derived class inherit from base class and it is also act as a parent for other class .


java multilevel inheritance image

In above image the class B is derived from the A class and act as a parent for the class C .

Multilevel Inheritance Demo :

Example : Write a program that demonstrate the multilevel inheritance in Java

    class A{
        
        public void sum(int x,int y){
        
            int result = x + y;
            System.out.println("Sum : " + result);
        }
    }
    class B extends A{
    
        public void sub(int x,int y){
        
            int result = x - y;
            System.out.println("Sub : " + result);
        }
    }
    class C extends B{
     
        public void mul(int x,int y){
        
            int result = x * y;
            System.out.println("Multi : " + result);
        }
    }
    class Test{
    
        public static void main(String args[]){
        
            C obj = new C();
            obj.sum(20, 10);
            obj.sub(20, 10);
            obj.mul(20, 10);
        }
    }
    OUTPUT : Sum  : 30
             Sub  : 10
             Multi: 200  
    

Welcome to AndroidATPoint.

back image                     next image