[Practice] Java Inheritance
Problem Statement
Write a Java Class named Vehicle with functions start and stop. This Class has a variable run_status. This is modified by the start and stop functions.
Write two other classes named Car and Bike which inherit the Vehicle Class.
Define the constructor for the Car and Bike Classes. Car Constructor takes the Model Name, Year, and Num of Wheels. Bike Constructor takes the Brand Name, Year, and Num of Gears.
The Car class has a function called drive, which takes a parameter gear position.
The Bike class has a function called pedal, which takes a parameter pedal speed.
In the main function, Instantiate Car and Bike classes. Then start, drive and stop the Car. Similarly start, pedal and stop the Bike.
If the Vehicle is not running (run_status is false), then drive and pedal should return error.
Test Data
Sample Input 1
No Input
Sample Output 1
Car Instantiated with Parameter Jaguar XF, 2022, 4
Cannot drive. Start the car first.
[Vehicle] stopped.
Bike Instantiated with Parameter Giant, 2021, 18
Cannot pedal. Start the bike first.
[Vehicle] stopped.
Comments