0% found this document useful (0 votes)
6 views

Worksheet Inheritance-1

Uploaded by

swayam.rraut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Worksheet Inheritance-1

Uploaded by

swayam.rraut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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() {
System.out.println("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() {
System.out.println(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

myCar1.drive(); // Call inherited method

myCar1.honk(); // Call child method

myCar2.drive(); // Call inherited method

myCar2.honk(); // Call child method

}
}

OUTPUT:
Driving a
Toyota corolla 2022

Toyota corolla honks


Dunn 6
a BMW BMWs 2 02}
Bmw Must honky -

You might also like