1. Write a Java program to implement the following inheritance hierarchy. (10 po
ID: 3549777 • Letter: 1
Question
1. Write a Java program to implement the following inheritance hierarchy. (10 points)
a. Super class Vehicle.java
- It includes data members representing the registration number and owners name.
- Add constructors, accessors, and mutators.
- Vehicle should provide an abstract public method description(). It is up to the subclasses how to implement this method.
b. Subclass Truck.java
- It inherits the functionalities of super Vehicle, but also includes an additional data member that represents the number of axles.
- Add constructors, accessors, and mutators.
- It must be a concrete class (not an abstract class).
c. Subclass Car.java
- It inherits the functionalities of super Vehicle, but also includes an additional data member that represents the number of doors.
- Add constructors, accessors, and mutators.
- It must be a concrete class (not an abstract class).
d. Write a client program to test these classes.
- The program should contain an array of Vehicle that holds variety of objects of Car and Truck.
- It loops through the array to output the vehicles descriptions polymorphically.
Super class Vehicle.java It includes data members representing the registration number and owner's name. Add constructors, accessors, and mutators. Vehicle should provide an abstract public method description(). It is up to the subclasses how to implement this method. Subclass Truck.java It inherits the functionalities of super Vehicle, but also includes an additional data member that represents the number of axles. Add constructors, accessors, and mutators. It must be a concrete class (not an abstract class). Subclass Car.java It inherits the functionalities of super Vehicle, but also includes an additional data member that represents the number of doors. Add constructors, accessors, and mutators. It must be a concrete class (not an abstract class). Write a client program to test these classes. The program should contain an array of Vehicle that holds variety of objects of Car and Truck. It loops through the array to output the vehicles' descriptions polymorphically.Explanation / Answer
package com.chegg.truckjavaprogram;
public abstract class Vehicle {
private String regisatrationNo;
private String ownerName;
public Vehicle(String regisatrationNo, String ownerName) {
super();
this.regisatrationNo = regisatrationNo;
this.ownerName = ownerName;
}
public String getRegisatrationNo() {
return regisatrationNo;
}
public void setRegisatrationNo(String regisatrationNo) {
this.regisatrationNo = regisatrationNo;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Vehicle [regisatrationNo=");
builder.append(regisatrationNo);
builder.append(", ownerName=");
builder.append(ownerName);
builder.append("]");
return builder.toString();
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public abstract String description();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.