fork download
  1. import java.util.*;
  2.  
  3. /* Name of the class has to be "Main" only if the class is public. */
  4. class Ideone {
  5. static class A {}
  6.  
  7. static class SubclassOfA extends A {}
  8.  
  9. static class B {}
  10.  
  11. static class SubclassOfB extends B {}
  12.  
  13. public static void main(String[] args) throws java.lang.Exception {
  14. try {
  15. List<Map<A, B>> list = new ArrayList<>();
  16.  
  17. // Would be compiler error, but use raw types to pretend it's OK.
  18. List<Map<? extends A, ? extends B>> list2 = (List) list;
  19.  
  20. Map<SubclassOfA, SubclassOfB> map = new HashMap<>();
  21. list2.add(map);
  22.  
  23. list.get(0).put(new A(), new B());
  24.  
  25. SubclassOfA soa = map.keySet().iterator().next();
  26. } catch (Exception e) {
  27. e.printStackTrace(System.out);
  28. }
  29. }
  30. }
  31.  
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
{Ideone$A@14ae5a5=Ideone$B@7f31245a}
java.lang.ClassCastException: Ideone$A cannot be cast to Ideone$SubclassOfA
	at Ideone.main(Main.java:26)