title: Java Generics Interface casting
body: <p>I stumbled across a Java casting situation involving Generics and Interfaces that I do not understand.</p>
  
  <p>Please consider the following code where I create a <code>List&lt;Interface1&gt;</code>. And then <code>get()</code> an element and cast it to <code>Interface2</code> without compiler error although these two interfaces are completely unrelated.</p>
  
  <pre><code>import java.util.*;
  
  public class Main {
    public static void main(String ... args) {
      List&lt;Interface1&gt; list = new ArrayList&lt;&gt;();
      list.add(new Interface1() {});
  
      Interface1 ok = list.get(0);
      Interface2 why = (Interface2)list.get(0);
    }
  }
  
  interface Interface1 {
  }
  
  interface Interface2 {
  }
  </code></pre>
  
  <p>Can anyone explain why there is not compiler error for the cast at the second <code>get(0)</code>?</p>
  
  <p>Two side notes: Executing the class throws a <code>ClassCastException</code> (as expected). And using two classes instead of interfaces does actually generate compile errors.</p>