I was totally confused, when saw this snippet:
class Animal {}
class Dog extends Animal {}
public class Test {
public static void main(String[] args) {
List<? super Animal> list = new ArrayList<>();
list.add(new Dog()); //it's OK
list.add(new Animal()); //and this is OK too
}
}
Why such things are allowed? When i changed my list to List<? super Dog> list = new ArrayList<>();
compile-time error occurs in list.add(new Animal());
With extends
wildcard all combinations cause errors. Who can tell the exact reason of this behaviour? Thanks in advance.