mercredi 4 novembre 2020

Creating object in an if statement

I am trying to do the following:

if (vehicleType == 1){
    Car vehicle = new Car();
} else if (vehicleType == 2){
    Van vehicle = new Van();
} else  {
    Bike vehicle = new Bike();
}
vehicle.setBrand("something");

The classes Car, Bike and Van all extend the class Vehicle, which contains setBrand. Therefore vehicle.setBrand() is always possible. So why is IntelliJ telling me "Can't find symbol."?

Note: I am doing much more with the generated object than vehicle.setBrand(), which is why I don't want it inside the if statements.

Aucun commentaire:

Enregistrer un commentaire