This may be a beginner question but is there a standard way to refactor the duplication of the Wheel property into the abstract class yet still maintain the explicit cast to the Part type. Let’s assume we have to prevent a FastCarWheel from being put on a SlowCar, and that there are many properties just like this one.
abstract class Car {}
class FastCar : Car
{
public FastCarWheel Wheel { get; set; }
}
class SlowCar : Car
{
public SlowCarWheel Wheel { get; set; }
}
abstract class WheelPart {}
class FastCarWheel: WheelPart {}
class SlowCarWheel: WheelPart {}
In this type of scenario is it common to just allow this type of duplication? I was thinking of making use of Generics but it just seems like I’m moving the issue around, and it gets worse for each additional property that behaves this way.
abstract class Car <P>
where P : Part
{
protected abstract P Wheel { get; set; }
}
Thanks