fork download
  1.  
  2. // source http://stackoverflow.com/q/12894335/348975
  3.  
  4. import java.util.List;
  5. import java.util.ArrayList;
  6.  
  7. class Main
  8. {
  9. public static void main ( String [ ] args )
  10. {
  11. }
  12. }
  13.  
  14. interface Canine {}
  15. class Dog implements Canine {}
  16. class Collie extends Dog {
  17. public static void main(String[] args){
  18. List<Dog> d = new ArrayList<Dog>();
  19. List<Collie> c = new ArrayList<Collie>();
  20. d.add(new Collie());
  21. c.add(new Collie());
  22. do1(d); do1(c);
  23. do2(d); do2(c);
  24. }
  25. static void do1(List<? extends Dog> d2){
  26. // d2.add(new Collie());
  27. System.out.print(d2.size());
  28. }
  29. static void do2(List<? super Collie> c2){
  30. c2.add(new Collie());
  31. System.out.print(c2.size());
  32. System . out . println ( c2 . get ( 0 ) ) ; // works ok
  33. Collie collie = c2 . get ( 0 ) ; // compiler error ... no access
  34. System . out . println ( collie ) ;
  35. }
  36. }
  37.  
Compilation error #stdin compilation error #stdout 0.06s 215488KB
stdin
Standard input is empty
compilation info
Main.java:33: error: incompatible types
        Collie collie = c2 . get ( 0 ) ; // compiler error ... no access
                                 ^
  required: Collie
  found:    CAP#1
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object super: Collie from capture of ? super Collie
1 error
stdout

Standard output is empty