Question
PLEASE ADD THIS TO CODE: for code reuse, in the overridden display() you may invoke common features of the vehicles by using super, followed by
PLEASE ADD THIS TO CODE: for code reuse, in the overridden display() you may invoke common features of the vehicles by using super, followed by specific features. PLEASE provide an explanation and insert the code where needed.
TESTER:
public class TestVehicle {
public static void main(String[] args) { //Create instances of subclasses Car car1 = new Car("Red", "Sedan", "Electric"); Truck truck1 = new Truck ("Blue", "Extended Cab", "Diesel"); //Display values car1.display(); truck1.display(); }
}
VEHICLE:
public class Vehicle { //attributes private String color; //constructor public Vehicle(String color) { this.color = color; } //get method public String getColor() { return color; } //set method public void setColor(String color) { this.color = color; } //Overloading methods public static void odometer() { System.out.println("There is no odometer reading for this vehicle."); } public static void odometer(int odometer) { System.out.println("Mileage: " + odometer); } //Overriding method public void display() { System.out.println("Vehicle color:"); System.out.println("Such a beautiful vehicle!"); } }
CAR:
public class Car extends Vehicle { // Car class attributes private String bodyStyle; private String typeOfCar; //Constructor public Car(String color, String bodyStyle, String typeOfCar) { super(color); this.bodyStyle = bodyStyle; this.typeOfCar = typeOfCar; } //Getter methods public String getBodyStyle() { return bodyStyle; } public String getTypeOfCar() { return typeOfCar; } //Setter methods public void setBodyStyle(String bodyStyle) { this.bodyStyle = bodyStyle; } public void setTypeOfCar(String typeOfCar) { this.typeOfCar = typeOfCar; } //Display method @Override public void display() { System.out.println("Car color: " + this.getColor() + " Body Style: " + bodyStyle + " Type of Car: " + typeOfCar); //Overloading Vehicle.odometer(); System.out.println("Such a beautiful car!"); } }
TRUCK:
public class Truck extends Vehicle { // attributes private String cabStyle; private String fuelType; //Constructor public Truck(String color, String cabStyle, String fuelType) { super(color); this.cabStyle = cabStyle; this.fuelType = fuelType; } //Getter methods public String getCabStyle() { return cabStyle; } public String getFuelType() { return fuelType; } //Setter methods public void setCabStyle(String cabStyle) { this.cabStyle = cabStyle; } public void setFuelType(String fuelType) { this.fuelType = fuelType; } //Display method @Override public void display() { System.out.println("Truck color: " + this.getColor() + " Cab Style: " + cabStyle + " Type of Fuel: " + fuelType); //Overloading Vehicle.odometer(256000); System.out.println("Such a beautiful truck!"); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started