I have 3 simple classes as follows:
public class ElementA {}
public class ElementB extends ElementA {}
public class ElementC extends ElementB {}
Then if I want to create, for example, generic List which takes only subclasses of ElementA class I can declare it as:
List<? super ElementA> list = new ArrayList<>();
and then use it as follows:
list.add(new ElementA());
list.add(new ElementB());
list.add(new ElementC());
which is fine and can be compiled without errors. But I became confused if I want to store anything but not ElementC or ElementB or ElementA. I declare such List as follows:
List<? extends ElementC> list = new ArrayList<>();
and I can't use it at all because it can store only null values. Same thing happen when I declare List as (notice that I'm using class which is 'in the middle of family'):
List<? extends ElementB>
Why so?