<p>I'm trying to set up an inheritance hierarchy similar to the following:</p>

<pre><code>abstract class Vehicle
{
  public string Name;
  public List&lt;Axle&gt; Axles;
}

class Motorcycle : Vehicle
{
}

class Car : Vehicle
{
}

abstract class Axle
{
  public int Length;
  public void Turn(int numTurns) { ... }
}

class MotorcycleAxle : Axle
{
  public bool WheelAttached;
}

class CarAxle : Axle
{
  public bool LeftWheelAttached;
  public bool RightWheelAttached;
}
</code></pre>

<p>I would like to only store MotorcycleAxle objects in a Motorcycle object's Axles array, and CarAxle objects in a Car object's Axles array.  The problem is there is no way to override the array in the subclass to force one or the other.  Ideally something like the following would be valid for the Motorcycle class:</p>

<pre><code>class Motorcycle : Vehicle
{
  public override List&lt;MotorcycleAxle&gt; Axles;
}
</code></pre>

<p>but the types have to match when overriding.  How can I support this architecture?  Will I just have to do a lot of run-time type checking and casting wherever the Axles member is accessed?  I don't like adding run-time type checks because you start to lose the benefits of strong typing and polymorphism.  There have to be at least some run-time checks in this scenario since the WheelAttached and Left/RightWheelAttached properties depend on the type, but I would like to minimize them.</p>

[{'answer_id': 31110, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 4, 'selected': True, 'text': '<p>Use more generics</p>\n\n<pre><code>abstract class Vehicle&lt;T&gt; where T : Axle\n{\n  public string Name;\n  public List&lt;T&gt; Axles;\n}\n\nclass Motorcycle : Vehicle&lt;MotorcycleAxle&gt;\n{\n}\n\nclass Car : Vehicle&lt;CarAxle&gt;\n{\n}\n\nabstract class Axle\n{\n  public int Length;\n  public void Turn(int numTurns) { ... }\n}\n\nclass MotorcycleAxle : Axle\n{\n  public bool WheelAttached;\n}\n\nclass CarAxle : Axle\n{\n  public bool LeftWheelAttached;\n  public bool RightWheelAttached;\n}\n</code></pre>\n'}, {'answer_id': 31273, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 0, 'selected': False, 'text': "<p>2 options spring to mind. 1 is using generics:</p>\n\n<pre><code>abstract class Vehicle&lt;TAxle&gt; where TAxle : Axle {\n   public List&lt;TAxle&gt; Axles;\n}\n</code></pre>\n\n<p>The second uses shadowing - and this assumes you have properties:</p>\n\n<pre><code>abstract class Vehicle {\n   public IList&lt;Axle&gt; Axles { get; set; }\n}\n\nclass Motorcyle : Vehicle {\n   public new IList&lt;MotorcycleAxle&gt; Axles { get; set; }\n}\n\nclass Car : Vehicle {\n   public new IList&lt;CarAxle&gt; Axles { get; set; }\n}\n\nvoid Main() {\n   Vehicle v = new Car();\n   // v.Axles is IList&lt;Axle&gt;\n\n   Car c = (Car) v;\n   // c.Axles is IList&lt;CarAxle&gt;\n   // ((Vehicle)c).Axles is IList&lt;Axle&gt;\n</code></pre>\n\n<p>The problem with shadowing is that you have a generic List. Unfortunately, you can't constrain the list to only contain CarAxle. Also, you can't cast a List&lt;Axle&gt; into List&lt;CarAxle&gt; - even though there's an inheritance chain there. You have to cast each object into a new List (though that becomes much easier with LINQ).</p>\n\n<p>I'd go for generics myself.</p>\n"}, {'answer_id': 46994, 'author': 'Luke', 'author_id': 327, 'author_profile': 'https://Stackoverflow.com/users/327', 'pm_score': 0, 'selected': False, 'text': '<p>I asked a <a href="https://stackoverflow.com/questions/46933/why-doesnt-inheritance-work-the-way-i-think-it-should-work">similar question</a> and got a better answer, the problem is related to C#\'s support for covariance and contravariance.  See that discussion for a little more information.</p>\n'}]
['https://Stackoverflow.com/questions/31088', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/327/']