Topic : INHERITANCE | WORKSHEET-1
Fill in the blanks and predict the output of the main class.
// Parent class
class Vehicle {
private String make;
private String model;
private int year;
// Constructor
G truly nuke, Shay model intyear
public Vehicle(…………………………………………………) { ,
this
nuke
……………………………………………………………………….
mvhej =
-
this nadel model ;
……………………………………………………………………….
.
=
this yen =
……………………………………………………………………….
year;
.
// Method
public void drive() {
[Link]("Driving a " + year + " " + make + " " + model);
}
}
// Child class
class Car extends Vehicle {
private int numDoors;
// Constructor
public Car(String make, String model, int year, int numDoors) {
spear ( motel
model
year ) ;
………………………………………………………………………. ,
this now nvm DoorDo
……………………………………………………………………….
is j
• =
.
// Method
public void honk() {
[Link](make + " " + model + " honks");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Car myCar1 = new Car("Toyota", "Corolla", 2022, 4); // Create Car object
Car myCar2 = new Car("BMW", "BMW s6", 2023, 5); // Create Car object
[Link](); // Call inherited method
[Link](); // Call child method
[Link](); // Call inherited method
[Link](); // Call child method
}
}
OUTPUT:
Driving a
Toyota corolla 2022
Toyota corolla honks
Dunn 6
a BMW BMWs 2 02}
Bmw Must honky -