I am reading about generic methods from OracleDocGenericMethod. I am pretty confused about the comparison when it says when to use wild-card and when to use generic methods. Quoting from the document.
interface Collection<E> { public boolean containsAll(Collection<?> c); public boolean addAll(Collection<? extends E> c); }
We could have used generic methods here instead:
interface Collection<E> { public <T> boolean containsAll(Collection<T> c); public <T extends E> boolean addAll(Collection<T> c); // Hey, type variables can have bounds too! }
[…] 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.
Don't we think wild card like (Collection<? extends E> c);
is also supporting kind of
polymorphism? Then why generic method usage is considered not good in this?
Continuing ahead, it states,
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.
What does this mean?
They have presented the example
class Collections { public static <T> void copy(List<T> dest, List<? extends T> src) { ... }
[…]
We could have written the signature for this method another way, without using wildcards at all:
class Collections { public static <T, S extends T> void copy(List<T> dest, List<S> src) { ... }
The document discourages the second declaration and promotes usage of first syntax? What's the difference between the first and second declaration? Both seems to be doing the same thing?
Can someone put light on this area.
In your first question: It means that if there is a relation between the parameter's type and the method's return type then use a generic.
For example:
Here you are extracting some of the T following a certain criteria. If T is
Long
your methods will returnLong
andCollection<Long>
; the actual return type is dependent on the parameter type, thus it is useful, and advised, to use generic types.When this is not the case you can use wild card types:
In this two example whatever the type of the items in the collections the return types will be
int
andboolean
.In your examples:
those two functions will return a boolean whatever is the types of the items in the collections. In the second case it is limited to instances of a subclass of E.
Second question:
This first code allow you to pass an heterogeneous
List<? extends T> src
as a parameter. This list can contain multiple elements of different classes as long as they all extends the base class T.if you had:
and
you could do
On the other hand
constrain
List<S> src
to be of one particular class S that is a subclass of T. The list can only contain elements of one class (in this instance S) and no other class, even if they implement T too. You wouldn't be able to use my previous example but you could do: