w3resource

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:

// Vehicle.java
// Parent class Vehicle
class Vehicle {
    public void drive() {
        System.out.println("Repairing a vehicle");
    }
}

// Car.java
// Child class Car
class Car extends Vehicle {
    @Override
    public void drive() {
        System.out.println("Repairing a car");
    }
}
// Main.java
// Main class
public class Main {
    public static void main(String[] args) {
        Vehicle vehicle = new Vehicle();
        Car car = new Car();
        vehicle.drive(); // Output: Repairing a vehicle
        car.drive(); // Output: Repairing a car
    }
}

Sample 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:

Flowchart: 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'.
Flowchart: 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'.
Flowchart: 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 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?



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

How do I remove repeated elements from ArrayList?

If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);

Of course, this destroys the ordering of the elements in the ArrayList.

Ref: https://bit.ly/3bYIjNC

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook