Reputation: 37034
I am reading book for scjp preparation. I have read that extends
in wild card is inclusive operator but super
is exclusive.But I cannot find related example. I tryed to write some code.
List<? super Number> list1 = new ArrayList<Number>();
List<? extends Number> list2= new ArrayList<Number>();
Both strings compiles good.
I am suppose that I knew something was wrong. Or maybe it is a book bug.
Please help to understand what was it in the book?
Upvotes: 1
Views: 581
Reputation: 203
It's a bug in the book, both super and extends operators are inclusive - they accept the type defined in the wildcard. You can see this in both your example and in the following example
List<? super Number> list1;
list1 = new ArrayList<Number>(); // ok - inclusive
list1 = new ArrayList<Object>(); // ok - wildcard accepts the super classes
list1 = new ArrayList<Integer>(); // compilation error
List<? extends Number> list2;
list2 = new ArrayList<Number>(); // ok - inclusive
list2 = new ArrayList<Object>(); // compilation error
list2 = new ArrayList<Integer>(); // ok - wildcard accepts the subclasses
Upvotes: 2