Do I really have a car in my garage? [duplicate]

If you need to make the difference between Car and Boat in your garage, then you should store them in distinct structures.

For instance:

public class Garage {
    private List<Car> cars;
    private List<Boat> boats;
}

Then you can define methods that are specific on boats or specific on cars.

Why have polymorphism then?

Let’s say Vehicle is like:

public abstract class Vehicle {
   protected int price;
   public getPrice() { return price; }
   public abstract int getPriceAfterYears(int years);
}

Every Vehicle has a price so it can be put inside the Vehicle abstract class.

Yet, the formula determining the price after n years depends on the vehicle, so it left to the implementing class to define it. For instance:

public Car extends Vehicle {
    // car specific
    private boolean automatic;
    @Override
    public getPriceAfterYears(int years) {
        // losing 1000$ every year
        return Math.max(0, this.price - (years * 1000));  
    }
}

The Boat class may have an other definition for getPriceAfterYears and specific attributes and methods.

So now back in the Garage class, you can define:

// car specific
public int numberOfAutomaticCars() {
    int s = 0;
    for(Car car : cars) {
        if(car.isAutomatic()) {
            s++;
        }
    }
    return s;
}
public List<Vehicle> getVehicles() {
    List<Vehicle> v = new ArrayList<>(); // init with sum
    v.addAll(cars);
    v.addAll(boats);
    return v;
}
// all vehicles method
public getAveragePriceAfterYears(int years) {
    List<Vehicle> vehicules = getVehicles();
    int s = 0;
    for(Vehicle v : vehicules) {
        // call the implementation of the actual type!
        s += v.getPriceAfterYears(years);  
    }
    return s / vehicules.size();
}

The interest of polymorphism is to be able to call getPriceAfterYears on a Vehicle without caring about the implementation.

Usually, downcasting is a sign of a flawed design: do not store your vehicles all together if you need to differenciate their actual type.

Note: of course the design here can be easily improved. It is just an example to demonstrate the points.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)