consider :
public class Parent {
}
public class Child extends Parent {
}
ArrayList<Parent> ps= new ArrayList<Child>(); \\wont compile
ArrayList<? extends Parent> ps2= new ArrayList<Child>(); \\works
Why isnt <? extends Parent>
assumed by default when using <Parent>
? I mean i cant think of a use case in which assuming every Child item is a Parent will cause any unexpected behaviour can you think of any?
EDIT :
a more usefull example :
public static final void main(String[] args) {
ArrayList<Child> children=new ArrayList<Child>();
children.add(new Child());
children.add(new Child());
computeSomething1(children); \\doesnt compile
computeSomething2(children); \\compiles
}
public static int computeSomething1(ArrayList<Parent> ps) {
return 1;
}
public static int computeSomething2(ArrayList<? extends Parent> ps) {
return 1;
}