Public Void PrintDescription
Public Void PrintDescription
To demonstrate polymorphic features in the Java language, extend the Bicycle class with a
MountainBike and a RoadBike class. For MountainBike, add a field for suspension, which
is a String value that indicates if the bike has a front shock absorber, Front. Or, the bike has
a front and back shock absorber, Dual.
public MountainBike(
int startCadence,
int startSpeed,
int startGear,
String suspensionType){
super(startCadence,
startSpeed,
startGear);
this.setSuspension(suspensionType);
}
Next, create the RoadBike class. Because road or racing bikes have skinny tires, add an
attribute to track the tire width. Here is the RoadBike class:
Note that once again, the printDescription method has been overridden. This time,
information about the tire width is displayed.
To summarize, there are three classes: Bicycle, MountainBike, and RoadBike. The two
subclasses override the printDescription method and print unique information.
Here is a test program that creates three Bicycle variables. Each variable is assigned to one
of the three bicycle classes. Each variable is then printed.
bike01.printDescription();
bike02.printDescription();
bike03.printDescription();
}
}