in java
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:
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:
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.
A better approach would be to write an Automobile base/Parent class to hold all the general data about an automobile, and then write derived classes (Car, PickUpTruck, SUV which extends the Parent Class automobile) for each specific type of automobile.
Create an Automobile class with above attributes, Provide getter, setters, constructors and toString method.
Write a class Car which extends Automobile class with attribute noOfDoors, provide default and parameterized constructor (with super keyword set values of parent’s attributes), getters, setters, and toString method.
Write a class PickUpTruck which extends Automobile class with attribute driveType, provide default and parameterized constructor (with super keyword set values of parent’s attributes), getters, setters, and toString method.
Write a class SUV which extends Automobile class with attribute passengerCapacity, provide default and parameterized constructor (with super keyword set values of parent’s attributes), getters, setters, and toString method.
Now in the main you have to create objects of three types of automobiles using parametrized constructor and print their values.