I understand the PECS rule with regards to extend and super from other posts, but I am a bit confused about the restrictions below code
List<Integer> integerList = new ArrayList<>();
// integerList.add(3.14); => understandably this will not compile,
// as we cannot add double to a list of Integer
List<? extends Number> numberList = integerList;
// numberList.add(3); => will not compile!
// Why the compiler cannot infer from the above line that
// adding an Integer to a list of Integer is OK, the same
// way it inferred that line 3 was not OK?
You are effectively saying this is a List of Something that is a subclass of Number.
So that could be anything, what if it is a List of Doubles? List of BigDecimals? This is why adding Integers is not allowed.
If you want a List that can hold any Number, that is:
List<Number> numberList = ...
In Java, there is no type-inference, except with Lambdas, so the information that the List is acutally a List of Integers is lost as soon as you cast it.