Java Program 4 – Inheritance

Program

class Car{

    public Car(){
        System.out.println("Inside car..Constructor");
    }

    public Car(String color){
        System.out.println("Inside car.." + color);
    }
    
    public void acceleration(){
        System.out.println("Car Acceleration");
    }

}

class Benz extends Car{

    public Benz(){
        super("Green");
        System.out.println("Inside Benz..Constructor");
    }
    public void breaking(){
        System.out.println("Inside Benz breaking..");
    }

}

class Merz extends Benz{

    public void acceleration(){
        System.out.println("Merz Acceleration");
    }
}

class TestInheritance{
    
    public static void main(String args[]){
        Benz b1=new Benz();
        b1.acceleration();
        b1.breaking();
        
        System.out.println("");
        
        Merz m1=new Merz();
        m1.acceleration();
        m1.breaking();
    }
}


Output

D:\a5518\day1>javac Inheritance.java

D:\a5518\day1>java TestInheritance
Inside car..Green
Inside Benz..Constructor
Car Acceleration
Inside Benz breaking..

Inside car..Green
Inside Benz..Constructor
Merz Acceleration
Inside Benz breaking..

D:\a5518\day1>


 

You can leave a response, or trackback from your own site.

Leave a Reply