title: Passing subclass or superclass object to method (expecting a superclass object)
body: <p>I'm working with some code that needs to send either a superclass or subclass object to a method.</p>
  
  <p>The method <code>public void repair(Vehicle vehicle)</code> will ONLY access methods in the super class object.  </p>
  
  <pre><code>public class Vehicle {
      //class stuff
  }
  
  public class Car extends Vehicle {
      //class stuff
  }
  
  public static void main(String[] args) 
  {
      // do stuff to determine whether working with a vehicle or car
      if (someCondition)
      {
          Car = new Car();
          // do some stuff...
          repair(Car);
      }
      else
      {
          Vehicle = new Vehicle();
          // do some stuff...
          repair(Vehicle);
      }
  }   
  </code></pre>
  
  <p>I figure I have three options:</p>
  
  <ol>
  <li>Leave the code as it is, it seems to be working. - I don't like this option, it feels like I'm making assumptions and I suspect car only methods could be accidentally called doing this, leading to unexpected behaviour.  </li>
  <li>Create a getVehicle() method in car, to return a Vehicle. Then use <code>repair(Car.getVehicle());</code> - this feels a little better</li>
  <li>Change <code>Car = new Car();</code> to <code>Vehicle = new Car();</code> which I believe would create an object (vehicle) that can only perform methods of type vehicle.  - This feels the safest, as I'm now restricting what can be done, to prevent unexpected behaviour.</li>
  </ol>
  
  <p>Is 3, the best approach, given that the repair method is only ever expecting vehicles?</p>
  
  <p>Also, is there anything I could/should to to the: <code>public void repair(Vehicle vehicle)</code> method declaration?</p>
  
  <p>EDIT: It seems I should be using:</p>
  
  <blockquote>
    <p>Leave the code as it is</p>
  </blockquote>
  
  <p>since the <code>repair()</code> method casts the subclass object to a superclass object anyway.  </p>