title: Passing in a sub-class to a method but having the super class as the parameter?
body: <p>I have an abstract class <code>Vehicle</code> with 2 implemented subclasses <code>RedVehicle</code> and <code>YellowVehicle</code>. </p>
  
  <p>In another class I have a <code>List&lt;Vehicle&gt;</code> containing instances of both subclasses.
  I want to be able to pass into a method a class type and then use that type to decide which set of objects I want to do something to in the <code>List</code>.</p>
  
  <p>Since <code>Class</code> is generic I should parameterise it with something, however putting the parameter as the parent class <code>Vehicle</code> stops the calling code working since <code>exampleMethod</code> is now expecting a type of Vehicle, not a subclass of <code>RedVehicle</code> or <code>YellowVehicle</code>.</p>
  
  <p>I feel there should be a clean way to do this so what would be the correct way to implement the functionality? </p>
  
  <p>n.b. I don't necessarily have to pass in the <code>Class</code> type, if there are better suggestions I'd be happy to try those.</p>
  
  <p>Calling code:</p>
  
  <pre><code>service.exampleMethod(RedVehicle.class);
  service.exampleMethod(YellowVehicle.class);
  </code></pre>
  
  <p>Fields/Method:</p>
  
  <pre><code>//List of vehicles
  //Vehicle has 2 subclasses, RedVehicle and YellowVehicle
  private List&lt;Vehicle&gt; vehicles;
  
  //Having &lt;Vehicle&gt; as the Class parameter stops the calling code working
  public void exampleMethod(Class&lt;Vehicle&gt; type) 
  {
      for(Vehicle v : vehicles)
      {
          if(v.getClass().equals(type))
          {
              //do something
          }
      }
  }
  </code></pre>