title: Java generics unchecked casts - wildcard bounds
body: <p>I'm trying to understand generics, but I block at wildcard bounds and casts. For instance:</p>
  
  <pre><code>List&lt;? extends Number&gt; l = new ArrayList&lt;&gt;();
  List&lt;PositiveInteger&gt; p = new ArrayList&lt;PositiveInteger&gt;();
  p.add(new PositiveInteger(10));
  l = p;
  NegativeInteger ni = (NegativeInteger) l.get(0);// runtime java.lang.ClassCastException:
  System.out.println(ni);
  </code></pre>
  
  <p>NegativeNumber and PositiveNumber both extend Number. So the question is why there is no warning at compile-time when casting the element from the list into a NegativeNumber ? Is there a way to prevent this type of exception ?</p>