Java Polymorphism Programming - Vehicle Class with Car and Bicycle Subclasses for Speed Control
Java Polymorphism: Exercise-2 with Solution
Write a Java program to create a class Vehicle with a method called speedUp(). Create two subclasses Car and Bicycle. Override the speedUp() method in each subclass to increase the vehicle's speed differently.
In the given exercise, here is a simple diagram illustrating polymorphism implementation:
In the above diagram, Vehicle is the base class, and Car and Motorcycle are the subclasses. The arrow represents the inheritance relationship, indicating that Car and Motorcycle inherit from Vehicle.
Since both Car and Motorcycle are subclasses of Vehicle, they inherit the speed field and the speedUp() method. However, they override the speedUp() method to provide their own implementation.
This polymorphic relationship allows objects of type Car and Motorcycle to be treated as objects of type Vehicle. This means that you can use a Car or Motorcycle object where a Vehicle object is expected, which enables flexibility and code reuse.
Sample Solution:
Java Code:
// Vehicle.java
// Base class Vehicle
// Declare the Vehicle class
class Vehicle {
// Private field to store the speed of the vehicle
private int speed;
// Method to increase the speed of the vehicle by 10
public void speedUp() {
speed += 10;
}
// Method to get the current speed of the vehicle
public int getSpeed() {
return speed;
}
}
// Car.java
// Subclass Car
// Declare the Car class that extends the Vehicle class
class Car extends Vehicle {
// Override the speedUp method from the Vehicle class
@Override
public void speedUp() {
// Call the speedUp method of the parent class (Vehicle)
super.speedUp();
// Print a message indicating that the car's speed has increased
System.out.println("\nCar speed increased by 22 units.");
}
}
// Motorcycle.java
// Subclass Motorcycle
// Declare the Motorcycle class that extends the Vehicle class
class Motorcycle extends Vehicle {
// Override the speedUp method from the Vehicle class
@Override
public void speedUp() {
// Call the speedUp method of the parent class (Vehicle)
super.speedUp();
// Print a message indicating that the motorcycle's speed has increased
System.out.println("Motorcycle speed increased by 12 units");
}
}
// Main.java
// Main class
// Declare the Main class
public class Main {
// Main method which is the entry point of the program
public static void main(String[] args) {
// Create an instance of the Car class
Car car = new Car();
// Create an instance of the Motorcycle class
Motorcycle motorcycle = new Motorcycle();
// Print the initial speed of the car
System.out.println("Car initial speed: " + car.getSpeed());
// Print the initial speed of the motorcycle
System.out.println("Motorcycle initial speed: " + motorcycle.getSpeed());
// Speed up the car
car.speedUp();
// Speed up the motorcycle
motorcycle.speedUp();
// Print the speed of the car after speeding up
System.out.println("\nCar speed after speeding up: " + car.getSpeed());
// Print the speed of the motorcycle after speeding up
System.out.println("Motorcycle speed after speeding up: " + motorcycle.getSpeed());
}
}
Output:
Car initial speed: 0 Motorcycle initial speed: 0 Car speed increased by 22 units. Motorcycle speed increased by 12 units Car speed after speeding up: 10 Motorcycle speed after speeding up: 10
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Animal Class with Bird and Cat Subclasses for Specific Sounds.
Next: Shape Class with Circle, Rectangle, and Triangle Subclasses for Area Calculation.
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-polymorphism-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics