Wednesday, April 18, 2007

Wildcards v.s. Generic Methods

Wildcards

void printCollection(Collection c) {
for (Object e : c) {
System.out.println(e);
} // ok

Collection c = new ArrayList ();
c.add(new Object()); //compile time error

Since we don’t know what the element type of c stands for, we cannot add objects
to it.

interface Collection {

public boolean containsAll(Collection c);
public boolean addAll(Collection c);
}

We could have used generic methods here instead:

interface Collection {
public boolean containsAll(Collection c);
public boolean addAll(Collection c);
// hey, type variables can have bounds too!
}

In both containsAll and addAll, the type parameter T is used only once. The return type doesn't depend on the type parameter, nor does any other argument to the method (in this case, there simply is only one argument). This tells us that the type argument is being used for polymorphism; its only effect is to allow a variety of actual argument types to be used at different invocation sites. If that is the case, one should use wildcards. Wildcards are designed to support flexible subtyping, which is what we're trying to express here.

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency. A generic method should not be used.

Use both generic methods and wildcards in tandem.

Class Collections {
public static void copy(List dest, list src) {...}
}



No comments: