I have the following java superclass with constructor and getters and setters for each attribute:
public class vehicle {
int vehicleId;
public vehicle(int vehicleId) {
super();
this.vehicleId = vehicleId;
}
public int getVehicleId() {
return vehicleId;
}
public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;
}
And a car subclass that extends the vehicle super class and has a few unique attributes:
public class car extends vehicle{
private String wheels;
//constructor
public car( int vehicleId, String wheels) {
super(vehicleId);
this.wheels = wheels;
}
//getters and setters
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
I create a new linked list of vehicle objects and add a new car object to it:
LinkedList<vehicle> gbm = new LinkedList<vehicle>();
car car = new car(0, "");
car.setVehicleId(1);
car.setWheels("alloy");
gbm.add(car);
I can change any of the attributes that belong to the vehicle super class with a function like this:
public static void editVehicleId(int vid, int vehicleId) {
for (vehicle obj : gbm) {
if (obj.vehicleId == vid) {
obj.setVehicleId(vehicleId);
} else {
System.out.println("No matching vehicle Id found - please check input");
}
}
}
But when I try to create a similar function to change one of the subclass attributes, i get this error: "the method setWheels(int) is undefined for the type vehicle".
public static void editWheels(int vid, int wheels) {
for (vehicle obj : gbm) {
if (obj.vehicleId == vid) {
obj.setWheels(wheels);
} else {
System.out.println("No matching vehicle Id found - please check input");
}
}
}
Can somebody tell me how to edit one of the subclass specific attributes?