title: Raw types and Generics -- Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicit">Is List&lt;Dog&gt; a subclass of List&lt;Animal&gt;? Why aren't Java's generics implicitly polymorphic?</a><br>
    <a href="https://stackoverflow.com/questions/6260841/java-generics-assigning-a-list-of-subclass-to-a-list-of-superclass">Java Generics &mdash; Assigning a list of subclass to a list of superclass</a>  </p>
  </blockquote>
  
  
  
  <p>With raw types you can easily say something like this.</p>
  
  <pre><code>[...] // MyClass is generic with upper bound of Object
  
  MyClass c = new MyClass&lt;Double&gt;();
  
  [...]
  </code></pre>
  
  <p>But this isn't allowed</p>
  
  <pre><code>MyClass&lt;Number&gt; c = new MyClass&lt;Double&gt;() 
  </code></pre>
  
  <p>I don't understand why this is.  My book tells me why the second doesn't work, because you can't add a Integer to a <code>MyClass&lt;Double&gt;</code>.  What it doesn't explains is why <code>MyClass&lt;Double&gt;</code> is a subclass of <code>MyClass&lt;Object&gt;</code> (or the equivalent raw type form),  since double is subclass of object.</p>
  
  <p>So why is the first allowed if the second form isn't.  Please realize I am new to this.</p>
  
  <p><strong>Edit: Taking it a step further what would happen in the first example if Number was the upper bound?</strong></p>
  
  <p>You can see here the effects of type erasure</p>
  
  <pre><code>class Untitled {
  public static void main(String[] args) {
  
  }
  public static&lt;T&gt; void c(T t)
  {
      t.SubClassMethod();// this won't work because class Object has no such method. But if I change the upperbound to SubClass it will.
              t.toString() // this will because class Object has such a method
  }
  }
  </code></pre>
  
  <p><strong>My point is that why should it matter what the Generic is declared as if it ultimately becomes treated as the upper bound anyway?</strong></p>