title: Inheritance in Java with generics
body: <p>Some days ago I was writing a simple Java program, and I discovered that this piece of code doesn't work (it gives a compile-time error):</p>
  
  <pre><code>ArrayList&lt;Document&gt; docs = new ArrayList&lt;Book&gt;();
  </code></pre>
  
  <p>where Document is an interface and Book implements Document.</p>
  
  <p>Why inheritance in Java doesn't work with generics? If I want that code to work, I must use wildcards, like this:</p>
  
  <pre><code>ArrayList&lt;? extends Document&gt; docs = new ArrayList&lt;Book&gt;();
  </code></pre>
  
  <p>I was wondering what is the reason of this? If I need an ArrayList of Documents, and I know that a Book is also a Document, why can't I use an ArrayList of Books as an ArrayList of Documents?</p>