title: Using superclass type for subclass instance
body: <p>I know this question has been asked a lot, but the usual answers are far from satisfying in my view.</p>
  
  <p>given the following class hierarchy:</p>
  
  <pre><code>class SuperClass{}
  class SubClass extends SuperClass{}
  </code></pre>
  
  <p>why does people use this pattern to instantiate SubClass:</p>
  
  <pre><code>SuperClass instance = new SubClass();
  </code></pre>
  
  <p>instead of this one:</p>
  
  <pre><code>SubClass instance = new SubClass();
  </code></pre>
  
  <p>Now, the usual answer I see is that this is in order to send <code>instance</code> as an argument to a method that requires an instance of SuperClass like here:</p>
  
  <pre><code>void aFunction(SuperClass param){}
  
  //somewhere else in the code...
  ...
  aFunction(instance);
  ...
  </code></pre>
  
  <p>But I can send an instance of SubClass to aFunction regardless of the type of variable that held it!  meaning the following code will compile and run with no errors (assuming the previously provided definition of aFunction):</p>
  
  <pre><code>SubClass instance = new SubClass();
  aFunction(instance);
  </code></pre>
  
  <p>In fact, AFAIK variable types are meaningless at runtime. They are used only by the compiler!</p>
  
  <p>Another possible reason to define a variable as SuperClass would be if it had several different subclasses and the variable is supposed to switch it's reference to several of them at runtime, but I for example only saw this happen in class (not super, not sub. just class). Definitly not sufficient to require a general pattern...</p>