Java Inheritance Programming - Create a class called Vehicle with a method called drive(). Create a subclass called Car that overrides the drive() method to print "Repairing a car"
Java Inheritance: Exercise-2 with Solution
Write a Java program to create a class called Vehicle with a method called drive(). Create a subclass called Car that overrides the drive() method to print "Repairing a car".
This program creates a class called 'Vehicle' with a method called drive() and a subclass called Car that overrides the drive() method to print "Repairing a car".
Sample Solution:
Java Code:
// Define the parent class Vehicle
class Vehicle {
// Define a public method named drive
public void drive() {
// Print "Repairing a vehicle" to the console
System.out.println("Repairing a vehicle");
}
}
The above code defines a Java class named Vehicle. Inside this class:
There is a method called drive(), which prints "Repairing a vehicle" to the console.
// Define the child class Car that extends Vehicle
class Car extends Vehicle {
// Use the @Override annotation to indicate that this method overrides a method in the superclass
@Override
// Define the drive method
public void drive() {
// Print "Repairing a car" to the console
System.out.println("Repairing a car");
}
}
The above code defines a Java class named Car, which is a child class that extends the parent class Vehicle. Inside this class:
There is a method named drive(), which overrides the drive() method from the parent class Vehicle. The overridden method prints "Repairing a car" to the console.
// Define the main class
public class Main {
// Define the main method
public static void main(String[] args) {
// Create an instance of Vehicle
Vehicle vehicle = new Vehicle();
// Create an instance of Car
Car car = new Car();
// Call the drive method on the vehicle instance
vehicle.drive(); // Output: Repairing a vehicle
// Call the drive method on the car instance
car.drive(); // Output: Repairing a car
}
}
In the exercise above -
An instance of the Vehicle class is created and stored in the variable vehicle.
An instance of the Car class is created and stored in the variable car.
The drive() method is called on the vehicle object, resulting in the output "Repairing a vehicle."
The drive() method is called on the car object, resulting in the output "Repairing a car."
Output:
Repairing a vehicle Repairing a car
Explanation:
In this program, we first define a parent class called Vehicle with a method called drive() which simply prints " Repairing a vehicle" to the console.
Then, we create a subclass called Car that extends Vehicle and overrides the drive() method to print " Repairing a car" instead.
In the main() method, we create an instance of both the Vehicle and Car classes, and call the drive() method on each object. The output of the first call to drive() will be "Repairing a vehicle", while the output of the second call to drive() will be "Repairing a car", as defined in the Car class.
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Animal with a method called makeSound.
Next: Create a class called Shape with a method called getArea and a subclass called Rectangle.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/java-inheritance-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics