Question: Program 5 : In the below example of inheritance, class Bicycle is a base class, class MountainBike is a derived class that extends the Bicycle
Program : In the below example of inheritance, class Bicycle is a base class, class MountainBike is a derived class that extends the Bicycle class and class Test is a driver class to run the program.
class Bicycle
the Bicycle class has two fields
public int gear;
public int speed;
the Bicycle class has one constructor
public Bicycleint gear, int speed
this.gear gear;
this.speed speed;
the Bicycle class has three methods
public void applyBrakeint decrement
speed decrement;
public void speedUpint increment
speed increment;
toString method to print info of Bicycle
public String toString
return No of gears are gear
"speed of bicycle is speed;
derived class
class MountainBike extends Bicycle
the MountainBike subclass adds one more field
public int seatHeight;
the MountainBike subclass has one constructor
public MountainBikeint gear, int speed,
int startHeight
invoking baseclassBicycle constructor
supergear speed;
seatHeight startHeight;
the MountainBike subclass adds one more method
public void setHeightint newValue
seatHeight newValue;
driver class
public class Test
public static void mainString args
MountainBike mb new MountainBike;
System.out.printlnmbtoString;
Note: We will add this later to the above program
overriding toString method of Bicycle to print more info
@Override public String toString
return supertoString
seat height is
seatHeight;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
