Java Reference
In-Depth Information
static void addString(SingleLinkQueue<? super String> sq) {
sq.add("Hello");
}
No matter what queue is passed to
addString
, you're guaranteed that it
can hold
String
objects.
Wildcards can be used in most declarations: fields, local variables, para-
meter types, and return types. But you can't use them to name a class
or interface in an
extends
or
implements
clause. Any parameterized types
that a class (or interface) extends or implements must be concrete types
in which the immediate type argument is not a wildcard. For example,
you can't define a class that implements
List<?>
. However, the type ar-
gument may itself be a parameterized type with a type argument that is
a wildcard so implementing
List<List<?>>
is okay.
You'll see more examples of the use of wildcards when we look at the
collection classes in detail in
Chapter 21
.