title: Why isn't the compiler able to restrict the return type in this case
body: <p>I have the following class:</p>
  
  <pre><code>Class Container&lt;E extends Supertype&gt;{
      ...
      public ArrayList&lt;E&gt; getList(){...}
      ...
  }
  </code></pre>
  
  <p>However when I try to do this:</p>
  
  <pre><code>public void someFunction(Container x){
      ....
      for(Supertype s : x.getList()){
          ...
      }
      ...
  }
  </code></pre>
  
  <p>It gives a compile error saying the elements of <code>x.getList()</code> is of <code>Object</code>. However, since the elements must be of type <code>E</code> which must be a subclass of <code>Supertype</code>, why can't the compiler "work this out"?</p>
  
  <p>My first thought was due to problems with raw types, but aren't those just the compiler being unsure whether something would be legal at run time?</p>
  
  <hr>
  
  <p>Yes, I can do</p>
  
  <pre><code>public void someFunction(Container&lt;?&gt; x)
  </code></pre>
  
  <p>And it won't even give an unchecked warning when I pass a raw <code>Container</code>. Just trying the learn the quirks of Java here.</p>