Your problem here is at a more fundamental level: you built Vehicle
in such a way that Garage
needs to know more about its objects than the Vehicle
interface gives away. You should try and build the Vehicle
class from the Garage
perspective (and in general from the perspective of everything that's going to use Vehicle
): what kind of things do they need to do with their vehicles? How can I make those things possible with my methods?
For example, from your example:
bool carIsAutomatic = myGarage[0].auto;
Your garage want to know about a vehicle's engine for... reasons? Anyway, there is no need for this to be just exposed by Car
. You can still expose an unimplemented isAutomatic()
method in Vehicle
, then implement it as return True
in Boat
and return this.auto
in Car
.
It would be even better to have a three-valued EngineType
enum (HAS_NO_GEARS
, HAS_GEARS_AUTO_SHIFT
, HAS_GEARS_MANUAL_SHIFT
), which would let your code reason on the actual characteristics of a generic Vehicle
cleanly and accurately. (You'd need this distinction to handle motorbikes, anyway.)