title: What is the advantage of an object type with a different reference type in Java?
body: <p>I know that when dealing with polymorphism in Java, you can have a different declared type for an object than the object type. For example: </p>
  
  <pre><code>    Parent foo = new Child(); 
  </code></pre>
  
  <p>I mostly see it when the declared type is an interface such as:</p>
  
  <pre><code>    Map&lt;Integer,BigInteger&gt; memo = new TreeMap&lt;Integer,BigInteger&gt;();    
  </code></pre>
  
  <p>I see this a lot in peoples Java code. What is the benefit of declaring foo as a Parent when it is a Child object? I also understand that if we do this, foo can no longer call any methods that are not declared abstract or defined in Parent. What puzzles me is I see this done when we can have both the declared and actual type Child and still pass as an argument to methods which require a Parent parameter object. For example:</p>
  
  <pre><code>    Child fooNew = new Child();
      public void showObj(Parent o){
          System.out.println(o); }
      showObj(fooNew);
  </code></pre>
  
  <p><em>So what gives?</em> In what circumstance would it be beneficial to have different declared and actual types for an object? My book says its important for generic programming but doesn't explain. I see it consistently used but with no clue why. And it even seems to have some drawbacks of its own.  </p>