You are on page 1of 6

1. Extend the Vehicle class by Car class and Car class by Duster class.

Inherit the properties

From Vehicle and add new properties in Car and Duster.

2. Show is use of super keyword in Car and Duster.

3. Replace some existing properties of Vehicle in Car and Replace the some existing properties

Of Car in Duster.

4. Show the use of Dynamic Method Dispatch in above inheritance.

5. Restrict the class Duster to extend further.

6. Restrict any of the method of Car to override.

7. Implement the Has-A relationship in between Car class and Engine class

Public void

Class Vehicle{

Int maxspeed=0;

Int noOfWheels;

Int noOfSeats;

Public void brake() {

System.out.println(“Apply Brake”);

Class Car extends Vehicle{

Int maxspeed=100;

Int currentSpeed;

Int noOfWheels=4;

Int noOfSeats=4;
Car(int a){

This.currentSpeed=a;

Public void carStartEngine() { // implementing Has-A relationship between car and


Engine.

Engine carEngine=new Engine();

carEngine.startEngine();

carEngine.stopEngine();

Public void steerRight() {

System.out.println(“turn right”);

Public void steerLeft() {

System.out.println(“turn left”);

Final void horn() { // A final method can be inherited but not overriden . if we try to override
final method in child we will get compiletime error.

System.out.println(“press Horn”);
}

Public void speed() {

System.out.println(currentSpeed);

@Override

Public void brake() {

// TODO Auto-generated method stub

Super.brake(); // super is used in brake method of car to give


same impolementation as parent vehicle

Final class Duster extends Car{

String type=”SUV”;

Int maxSpeed=120;

Duster(int a){

Super(a); // using super in subclass constructor to call parent constructor. It


will inherit property from car.

// using this super to initialize currentspeed of Duater

@Override
Public void steerRight() {

// TODO Auto-generated method stub

System.out.println(“Duster turns right”);

@Override

Public void steerLeft() {

// TODO Auto-generated method stub

System.out.println(“duster turns left”);

@Override

Public void brake() {

// TODO Auto-generated method stub

System.out.println(“duster apply brakes”);

@Override

Public void speed() {

// TODO Auto-generated method stub

System.out.println(“Speed of duster:-“);

Super.speed();
}

// public void horn() { // getting compile time error as its declared final in parent car class

//

// System.out.println(“press Horn”);

// }

//

Class Engine{

Public void startEngine() {

System.out.println(“Start ENgineS”);

Public void stopEngine() {

System.out.println(“Stop EngineS”);

Public class Question1 {

Public static void main(String[] args) {

// TODO Auto-generated method stub

Duster a =new Duster(50);


System.out.println(a.maxSpeed);

a.brake();

a.speed();

Car c=new Duster(10);

c.steerLeft();

System.out.println(c.maxspeed);

You might also like