When we use bounded wildcards with super, it means that the type parameter can be the specified type or any of its superclass. For example, List super Number> indicates that the list can hold objects of type Number or any of its parent classes.
In your first example, the list list declared as List super Number> cannot take an Object because Object is not a subclass of Number. On the other hand, Integer is a subclass of Number, so it can be added to the list.
In your second example, you attempted to pass a List
Unlike lower bounded wildcards, upper bounded wildcards cannot be used as producers. This means that you cannot obtain individual elements from a List super Number>. This restriction exists to prevent type safety violations.
Bounded wildcards with super allow us to create collections that can consume objects of multiple types that inherit from a common superclass. However, we should be careful when adding elements to these collections, as we cannot add objects that are not subclasses of the designated superclass.
The above is the detailed content of How Do Java Generics\' `? super` Wildcards Work and What Are Their Limitations?. For more information, please follow other related articles on the PHP Chinese website!