Worksheet Inheritance-1
Worksheet Inheritance-1
Fill in the blanks and predict the output of the main class.
// Parent class
class Vehicle {
// Constructor
G truly nuke, Shay model intyear
public Vehicle(…………………………………………………) { ,
this
nuke
……………………………………………………………………….
mvhej =
-
this yen =
……………………………………………………………………….
year;
.
// Method
public void drive() {
System.out.println("Driving a " + year + " " + make + " " + model);
}
}
// Child class
// Constructor
public Car(String make, String model, int year, int numDoors) {
spear ( motel
model
year ) ;
………………………………………………………………………. ,
// Method
public void honk() {
System.out.println(make + " " + model + " honks");
}
}
// Main class
Car myCar1 = new Car("Toyota", "Corolla", 2022, 4); // Create Car object
Car myCar2 = new Car("BMW", "BMW s6", 2023, 5); // Create Car object
}
}
OUTPUT:
Driving a
Toyota corolla 2022