Getters Setters Labs

In this lab we will improve the encapsulation of our com.example.inheritance.labs.vehicles.Vehicle hierarchy.

  1. Change Vehicle, Automobile, Truck, and Boat so that all fields are private.

  2. There will be compilation errors. Do not accept any Eclipse suggestions to fix them.

  3. Add getters and setters in each class for all fields that do not have them.

  4. You can do this in Eclipse by choosing a class and selecting Source->Generate Getters and Setters.

    Generate getters and setters

  5. Then select fields, and eclipse will generate the methods.

    Generate getters and setters

  6. Generating getters and setters will only show fields that are visible and do not already have getters and setters defined.

  7. Fix compilation errors by using getters and setters instead of directly accessing fields.

  8. Run VehicleTestApp and make sure it still works.

(Solution: Vehicle2.java, Automobile2.java, Truck2.java, Boat2.java, VehicleTestApp2.java)


Prev -- Up