Trying to get some generics to work, I ended up with the following simplified example:
Set<? extends Number> setOfNumbers;
Set<? extends Integer> setOfIntegers = new HashSet<>();
setOfNumbers = setOfIntegers; // compiles
List<Set<? extends Number>> listOfSetOfNumbers;
List<Set<? extends Integer>> listOfSetOfIntegers = new LinkedList<>();
listOfSetOfNumbers = listOfSetOfIntegers; // does not compile
I understand (clearly not completely) and use type bounds often, but this case confuses me. If anything, I would expect listOfSetOfIntegers = listOfSetOfNumber;
not to work (and indeed it does not either).
Why is that?