0% found this document useful (0 votes)
94 views2 pages

Public Void PrintDescription

The document describes the creation of MountainBike and RoadBike subclasses that extend the Bicycle class. The MountainBike class adds a suspension field and overrides the printDescription method to print the suspension type. The RoadBike class adds a tireWidth field and overrides printDescription to print the tire width. A test program creates instances of each class and calls printDescription to output class-specific details.

Uploaded by

Awang Nurhadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views2 pages

Public Void PrintDescription

The document describes the creation of MountainBike and RoadBike subclasses that extend the Bicycle class. The MountainBike class adds a suspension field and overrides the printDescription method to print the suspension type. The RoadBike class adds a tireWidth field and overrides printDescription to print the tire width. A test program creates instances of each class and calls printDescription to output class-specific details.

Uploaded by

Awang Nurhadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

Here is the updated class:

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.");
}
}

Note the overridden printDescription method. In addition to the information provided


before, additional data about the suspension is included to the output.

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:

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.");
}
}

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.

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();
}
}

The following is the output from the test program:

Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10.

Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10.


The MountainBike has a Dual suspension.

Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20.


The RoadBike has 23 MM tires.

You might also like