Create a class hierarchy for different types of vehicles. The base class should have a protected instance variable for the number of wheels. Derive classes for bicycle, motorcycle, and car from the base class. Each derived class should have a method to print the number of wheels for that vehicle type.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesBuilding class hierarchies
Referencing subclass objects
Implementing vehicle hierarchy with wheels
Report a typo
Sample Input 1:
bicycleSample Output 1:
A bicycle has 2 wheels.Sample Input 2:
motorcycleSample Output 2:
A motorcycle has 2 wheels.Write a program in Java 25
import java.util.Scanner;
// Base class for vehicles
class Vehicle {
protected int numWheels;
public Vehicle(int wheels) {
numWheels = wheels;
}
}
// TODO: Create the Bicycle class that extends Vehicle
// TODO: Create the Motorcycle class that extends Vehicle
// TODO: Create the Car class that extends Vehicle
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String vehicleType = scanner.nextLine();
// TODO: Create an instance of the appropriate vehicle class based on vehicleType
// TODO: Call the printWheels() method on the vehicle instance
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.