I dont really catch why "ArrayList<? extends Integer> is not a subtype of ArrayList<Number>"
any type that extends from Integer, would be also a subtype of Number, because of its hierarchy relation. isn't it?
thanks in advance.
Im sorry, I'll asnwer myself.
Already have studied it in the official docs, but its a bit counterintuitive. Integer, extends Number, but an Array of Integers doesnt extends an Array of Numbers, because simply, these types Array<Integer> and Array<Number> are not related on the Java hierarchy
List<Number> means, it is a list of Numbers. Every element in this list will be always a Number. For example, an element in this list could be an Integer or a Short or some other type of Number. We know for sure that whatever element is present in this list will be a Number. So, you can add an Integer or Short to it.
List<? extends Number> is a list of some class that extends Number but we don't know which type. For example, this list could be a list of Integers, in which case, it cannot contain a Short or some other type of Number. This list could also be a list of Shorts, in which case, it cannot contain Integers.
The same logic applies to List<? extends Integer>.
You can see that both types of list have different conditions on what they may contain. Therefore, List<? extends Number> or List<? extends Integer> is not a subtype of List<Number>.
If you like our products and services, please help us by posting your review here.