0

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?

2
  • When posting to Stack Overflow, strip down your code to the absolute minimum needed to show your issue. MCVE Commented May 27, 2020 at 0:42
  • this is polymorphism , you can use car instance if you want to access property belongs to car Commented May 27, 2020 at 0:51

1 Answer 1

1

Your for loop variable is a vehicle type. Since this is a method for the car class, make it a car object:

for (vehicle obj : gbm) {
    if (obj.vehicleId == vid) {
        if (obj instanceof car) {
            ((car)obj).setWheels(wheels);
        }
    }
    ... rest of loop ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

"type mismatch: cannot convert from element type vehicle to car"
Please see my edit. Actually, the loop variable should still be vehicle since that is the wrapped type of the ArrayList gmb. But you can check if the obj is a car with instanceof and if so, cast it to type car before calling setWheels
I suppose one solution would be to use a different linked list for each different type of vehicle instead of all together in one.
True, but you may want to just look through all vehicles and only care about the subclass sometimes. It depends on what you want to do with the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.