Subtype Contravariance: ? super Type – Generics

Subtype Contravariance: ? super Type

The wildcard type ? super Type represents all supertypes of Type (including Type itself). The wildcard type ? super Type is called a lower bounded wildcard with Type representing its lower bound.

Figure 11.4 Partial Type Hierarchy for Node<? super Integer>

The wildcard type ? super Integer denotes all supertypes of Integer, and the parameterized type Node<? super Integer> denotes a family of invocations of Node<E> for types that are supertypes of Integer. Figure 11.4 shows a partial type hierarchy for the parameterized type Node<? super Integer>. Note that the parameterized type Node<? super Number> is a subtype of the parameterized type Node<? super Integer>, since the wildcard type ? super Number represents all supertypes of Number, and these are also supertypes of Integer.

Click here to view code image

Node<? super Number> numSupNode = new Node<Number>(100, null);
Node<? super Integer> numIntSupNode = numSupNode;

Subtype Bivariance: ?

As mentioned earlier, the wildcard type ? represents all types. The wildcard type ? is called the unbounded wildcard, since it has no bounds as do the other two wildcard types. By definition, it represents both the upper and the lower bounded wildcards for any bound.

The parameterized type Node<?> denotes the family of invocations of Node<E> for any type—that is, denotes a Node of any kind, and is therefore the supertype of all invocations of Node<E> (see also Figure 11.5, p. 583, and §11.5, p. 584).

Figure 11.5 Partial Type Hierarchy for Selected Parameterized Types of Node<E>

Subtype Invariance: Type

When a concrete type Type is used as an actual type parameter in a parameterized type, it represents Type itself. Since Type can be any concrete type, it is called an unbounded type parameter. The concrete parameterized type Node<Integer> represents the invocation of Node<E> for the concrete actual type parameter Integer. As we have seen earlier, there is no subtype covariance relationship between concrete parameterized types, but there is such a relationship between bounded parameterized types and concrete parameterized types (see also Figure 11.3 and Figure 11.4).

Let us recapitulate the basic terminology before proceeding further. A generic type can specify one or more formal type parameters. A parameterized type is an invocation of a generic type, supplying the required actual type parameters. An actual type parameter can be a wildcard type (possibly bounded) or a concrete type. A concrete type is either a non-generic type or a parameterized type that has concrete types as parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *