Inheritence

//Parent Class

public class CarClass {
    public void carNeedTiers(){
        System.out.println("Car Needs Tiers");
    }
    public void carNeedsEngine(){
        System.out.println("Car Needs Engine");
    }
    public void carNeedsSeats(){
        System.out.println("Car Needs Seats");
    }
    public void carNeedsBody(){
        System.out.println("Car Needs Body");
    }
    private void carNeedsTrunk(){
        System.out.println("Car Needs Trunk");
    }
}

 

 

//Inherited Class

 

public class HondaCar extends CarClass{

    //constructor
    public HondaCar(){
        super.carNeedTiers();
    }

    //Overriding Method in the parent class
    public void carNeedTiers(){
        System.out.println("Honda Car Needs Different Model Tiers");
    }

    public void CarColor(){
        System.out.print("Red");
    }

    public void CarModel(){
        System.out.print("Sedan");
    }
}

 

//Main Method

 

public class MainClass {

    public static void main(String[] args) {

        //object CarClass
        CarClass car  = new CarClass();
        car.carNeedTiers();

        //Object CarClass with inherited Class
        CarClass hCarOver = new HondaCar();
        hCarOver.carNeedTiers();

        //Honda Car Object
        HondaCar hondaCar = new HondaCar();
        hondaCar.carNeedTiers();
        hondaCar.CarColor();
    }

}

 

Print Print | Sitemap
© 2016 Automation Learn. All rights reserved.