Why can't the parent class be stored in the next wildcard in this line of code?

The method add (capture-sharp2-of? Super Integer) in the type List < capture-sharp2-of? Super Integer > is not applicable for the arguments (Number)
    public static void down(List<? super Integer> numberList) {
        Number x = 4;
        numberList.add(x);
    }

add method error

Mar.14,2021

for generics, what is required is that once the generic parameters are determined, they cannot be changed. means that the generic parameter will be Integer or its parent class, so you can definitely use to consume objects of Integer or its subclasses.
but Number is the parent class of Integer -- We assume that the inheritance relationship is C inherits B, B inherits A, and A, B, C each implements some interfaces, so for generic parameters at run time-and here in your code (that is, List as a parameter), it is certainly impossible to determine this generic parameter at compile time. Therefore, the compiler simply forbids you to add the parent class of C to the generic container.

Menu