0% found this document useful (0 votes)
4 views2 pages

Vehicle Inheritance Worksheet Output

The document outlines a Java programming worksheet focused on inheritance, featuring a parent class 'Vehicle' and a child class 'Car'. It includes incomplete code snippets for constructors and methods, requiring the reader to fill in the blanks and predict the output of the main class. The expected output demonstrates the functionality of the drive and honk methods for two Car objects created in the main class.

Uploaded by

swayam.rraut
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Vehicle Inheritance Worksheet Output

The document outlines a Java programming worksheet focused on inheritance, featuring a parent class 'Vehicle' and a child class 'Car'. It includes incomplete code snippets for constructors and methods, requiring the reader to fill in the blanks and predict the output of the main class. The expected output demonstrates the functionality of the drive and honk methods for two Car objects created in the main class.

Uploaded by

swayam.rraut
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 -

You might also like