Unlike arrays, different instantiations of a generic type are not compatible with each other, not even explicitly: With the declaration Generic superGeneric; Generic subGeneric; the compiler would report a conversion error for both castingssuperGeneric and subGeneric. This incompatibility may be softened by the wildcard if ? is used as an actual type parameter: Generic> is the abstract supertype for all parameterizarions of the generic type. This allows objects of type Generic and Generic to be safely assigned to a variable or method parameter of type Generic>. Using Generic extends Supertype> allows the same, restricting compatibility to Supertype and its children. Another possibility is Generic super Subtype>, which also accepts both objects and restricts compatibility to Subtype and all its parents.
Wildcard as parameter type
In the body of a generic unit, the type parameter is handled like its upper bound. If the return type of a method is the type parameter, the result can be referenced by a variable of the type of the upper bound. In the other direction, the wildcard fits no other type, not even Object: If ? has been applied as the formal type parameter of a method, no actual parameters can be passed to it. However, objects of the unknown type can be read from the generic object and assigned to a variable of a supertype of the upperbound. class Generic ... Generic concreteTypeReference = new Generic; Generic> wildcardReference = concreteTypeReference; UpperBound ub = wildcardReference.read; // Object would also be OK wildcardReference.write); // type error wildcardReference.write); // type error concreteTypeReference.write); // OK
Bounded wildcards
A bounded wildcard is one with either an upper or a lower inheritance constraint. The bounds can be both class and interface types. Upper bounds are expressed using the extends keyword and lower bounds using the super keyword. An upper bound on a wildcard must be a subtype of the upper bound on the type parameter it is assigned. Generic extends SubtypeOfUpperBound> referenceConstrainedFromAbove; This reference can hold any instantiation of Generic with an actual type parameter of SubtypeOfUpperBound's subtype. A wildcard that does not have an upper bound is effectively the same as one that has the constraint extends Object, since all types implicitly extend Object. A wildcard with a lower bound Generic super SubtypeOfUpperBound> referenceConstrainedFromBelow; can hold instantiations of Generic with any type that is both a subtype of UpperBound and a supertype of SubtypeOfUpperBound. The type bounds are trivial examples that conform.
No objects may be created with a wildcard type parameter: new Generic> is forbidden because Generic> is abstract. In practice, this is unnecessary because if one wanted to create an object that was assignable to a variable of type Generic>, one could simply use any arbitrary type as the type parameter. However, new List> is allowed, because the wildcard is not a parameter to the instantiated type List. The same holds for new List>. An array object that is an array of a parameterized type can only parameterized by an unconstrained type as the component type: new Generic> is correct, while new Generic is not. For both cases, using no parameters is another option. This will generate a warning since it is less type-safe.
Example: lists
In the Java Collections Framework, the class List represents an ordered collection of objects of type MyClass. Upper bounds are specified using extends: A List extends MyClass> is a list of objects of some subclass of MyClass, i.e. any object in the list is guaranteed to be of type MyClass, so one can iterate over it using a variable of type MyClass public void doSomething
However, it is not guaranteed that one can add any object of type MyClass to that list: public void doSomething
The converse is true for lower bounds, which are specified using super: A List super MyClass> is a list of objects of some superclass of MyClass, i.e. the list is guaranteed to be able to contain any object of type MyClass, so one can add any object of type MyClass: public void doSomething
However, it is not guaranteed that one can iterate over that list using a variable of type MyClass: public void doSomething
In order to be able to do both add objects of type MyClass to the list and iterate over it using a variable of type MyClass, a List is needed, which is the only type of List that is both List extends MyClass> and List super MyClass>. The mnemonics PECS from the bookEffective Java by Joshua Bloch gives an easy way to remember when to use wildcards in Java.