I have an abstract class called Vehicle, and two child abstract classes called Car, and the other called Truck.
In the Vehicle class, I have a virtual method called TowTrailer(int weight). In this method in the Vehicle class, am I allowed to say:
if ( this is Car )
return "Cars can't tow trailers."
else if ( this is Truck )
return "Trucks can tow trailers."
I'm confused because on the one hand, since I'm creating the TowTrailer in the base class, it doesn't feel right to say if "this is Car". The answer is no. Vehicle is NOT a car, but a car is a vehicle.
However, since the TowTrailer method will be inherited by a child of either a Car or a Truck, this is perfectly fine, but I'm not sure.
Please let me know when it's OK to use the "is" keyword in a hierarchy. Thanks.