Fields and Methods Labs
This is an extended drill that we will use for the rest of the chapter.
We will create a class hierarchy for Vehicles.
- Create the package
com.example.inheritance.labs.vehicles. - Create a
Vehicleclass - Fields
protected double purchasePrice
- Add a no-arg constructor.
- Add a one-arg constructor for the field.
- Add a method
public String toString()to return a String containing the class name, and field names and values. - Create a
Automobileclass which extendsVehicle - Fields
protected String makeprotected String modelprotected int yearprotected int numberOfWheelsprotected double speedInMph
- Add a no-arg constructor.
- Add a six-arg constructor for the fields of this class and
Vehicle. Set each field in the constructor. - Add a method
public String toString()to return a String containing the class name, and all six fields and values, starting with the field inVehicle. - Create a
Boatclass which extendsVehicle - Fields
protected String nameprotected double speedInKnotsprotected int lengthInFeet
- Add a no-arg constructor.
- Add a four-arg constructor.
- Add a method
public String toString()to return a String containing the class name, and all field names and values. - Create a
Truckclass which extendsAutomobile. - Fields
protected int bedSizeInCubicFeet
- Add a no-arg constructor.
- Add an seven-arg constructor for fields of this class,
Automobile, andVehicle. - Create a
VehicleTestAppclass that you can run to test Vehicles. - Create one
Automobile,Boat, andTruckusing the multi-arg constructors. - Call each object's
toString()method, and print theStringto the screen. Verify the classes work as expected.
Check your class structure against this UML diagram.

(Solution: Vehicle.java, Automobile.java, Truck.java, Boat.java, VehicleTestApp.java)