package inheritance; /** * This class represents a special kind of bicycle, a mountain bike. */ public class MountainBike extends Bicycle { // additional instance field private String suspension; /** * This constructor initializes the fields to the passed values. * * @param startCadence MountainBike's initial cadence. * @param startSpeed MountainBike's initial speed. * @param startGear MountainBike's initial gear. * @param suspensionType MountainBike's initial suspension. */ public MountainBike(int startCadence, int startSpeed, int startGear, String suspensionType) { // call the constructor in the super class super(startCadence, startSpeed, startGear); // set the fields in this class suspension = suspensionType; } /** * This is a copy constructor. It initializes the fields of the object being created to the same * values as the fields in the object passed as an argument. * * @param bikeObject The object being copied. */ public MountainBike(MountainBike bikeObject) { super(bikeObject); //super(bikeObject.getCadence(), bikeObject.getSpeed(), bikeObject.getGear()); if (bikeObject != null) { suspension = bikeObject.suspension; } } /** * The getSuspension method returns a MountainBike object's suspension. * * @return The value in the suspension field. */ public String getSuspension() { return suspension; } /** * The setSuspension method stores a value in the suspension field. * * @param suspensionType the value to store in suspension. */ public void setSuspension(String suspensionType) { suspension = suspensionType; } /** * The setSuspension method stores a default value in the suspension field. */ public void setSuspension() { suspension = "dual"; } /** * The toString method returns a string representing the state of a MountainBike object. * * @return A string containing the bicycle information: cadence, speed, gear, and suspension. */ @Override public String toString() { // Create a string representing the object. String output = super.toString() + "Suspension: " + suspension + "\n"; return output; } /** * The equals method compares two MountainBike objects. The result is true if the argument is not * null and is a MountainBike object with the same values for all fields as this object. * * @param obj The object with which to compare the calling object. * @return true if the given object has the same value for all fields as the calling object. */ @Override public boolean equals(Object obj) { // check that the type of the parameter is MountainBike if (!(obj instanceof MountainBike)) { return false; } // we already know that obj is of type MountainBike, so it's safe to cast MountainBike bike = (MountainBike) obj; // return true or false depending on whether cadence, speed, gear, and suspension have the same value return super.equals(bike) && suspension.equals(bike.suspension); } /** * The copy method creates a new MountainBike object and initializes it with the same data in the * calling object. * * @return a reference to the new object. */ @Override public MountainBike copy() { return new MountainBike(this); } /** * The printDescription method overrides the method in the parent class. It prints information * about this MountainBike object. */ @Override public void printDescription() { super.printDescription(); System.out.println("The MountainBike has a " + suspension + " suspension."); } }