Suppose we are developing a program that a car dealership can use to manage its inventory of
Question:
Suppose we are developing a program that a car dealership can use to manage its inventory of used cars. The dealership’s inventory includes three types of automobiles: cars, pickup trucks, and sport-utility vehicles (SUVs). Regardless of the type, the dealership keeps the following data about each automobile:
• Make
• Year model
• Mileage
• Price
Each type of vehicle that is kept in inventory has these general characteristics, plus its own specialized characteristics. For cars, the dealership keeps the following additional data:
• Number of doors (2 or 4)
For pickup trucks, the dealership keeps the following additional data:
• Drive type (two-wheel drive or four-wheel drive)
And, for SUVs, the dealership keeps the following additional data:
• Passenger capacity
In designing this program, one approach would be to write the following three classes:
• A Car class with attributes for the make, year model, mileage, price, and number of doors.
• A Truck class with attributes for the make, year model, mileage, price, and drive type.
• An SUV class with attributes for the make, year model, mileage, price, and passenger
capacity.
This would be an inefficient approach, however, because all three classes have a large number of common data attributes. As a result, the classes would contain a lot of duplicated code. In addition, if we discover later that we need to add more common attributes, we would have to modify all three classes.
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang