Bicycle
class. For example, a printDescription
method could be added to the class that displays all the data currently stored in an instance. public void printDescription(){ System.out.println("\nBike is " + "in gear " + this.gear + " with a cadence of " + this.cadence + " and travelling at a speed of " + this.speed + ". "); }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 class MountainBike extends Bicycle { private String suspension; public MountainBike( int startCadence, int startSpeed, int startGear, String suspensionType){ super(startCadence, startSpeed, startGear); this.setSuspension(suspensionType); } public String getSuspension(){ return this.suspension; } public void setSuspension(String suspensionType) { this.suspension = suspensionType; } public void printDescription() { super.printDescription(); System.out.println("The " + "MountainBike has a" + getSuspension() + " suspension."); } }
printDescription
method. In addition to the information provided before, additional data about the suspension is included to the output.RoadBike
class. Because road or racing bikes have skinny tires, add an attribute to track the tire width. Here is the RoadBike
class:public class RoadBike extends Bicycle{ // In millimeters (mm) private int tireWidth; public RoadBike(int startCadence, int startSpeed, int startGear, int newTireWidth){ super(startCadence, startSpeed, startGear); this.setTireWidth(newTireWidth); } public int getTireWidth(){ return this.tireWidth; } public void setTireWidth(int newTireWidth){ this.tireWidth = newTireWidth; } public void printDescription(){ super.printDescription(); System.out.println("The RoadBike" + " has " + getTireWidth() + " MM tires."); } }
printDescription
method has been overridden. This time, information about the tire width is displayed.Bicycle
, MountainBike
, and RoadBike
. The two subclasses override the printDescription
method and print unique information.Bicycle
variables. Each variable is assigned to one of the three bicycle classes. Each variable is then printed.public class TestBikes { public static void main(String[] args){ Bicycle bike01, bike02, bike03; bike01 = new Bicycle(20, 10, 1); bike02 = new MountainBike(20, 10, 5, "Dual"); bike03 = new RoadBike(40, 20, 8, 23); bike01.printDescription(); bike02.printDescription(); bike03.printDescription(); } }
Labels: Core JAVA