It's not the same thing.
In the first example, you're saying, "For some type T
, such that I can cast any T
to a Comparable
of a supertype of T
, then I'll accept a list of T
and return an instance of T
."
When you call the method with a List<B>
, since every B
is a Comparable<A>
, it's therefore true to say: "I can cast any B
to a Comparable
of a supertype of B
", and the type parameter T
resolves to concrete type B
.
In the second example, you're saying, "For some type T
, such that I can cast any T
to a Comparable of T
, then I'll accept any list of T
or any list of any subtype of T
, and return an instance of T
."
When you call this one with A List<B>
, then the type parameter T
resolves to A
; of course any A
is a Comparable<A>
, and you can pass a List<B>
into the method because a list of B
is indeed a list of a subtype of A
.
So that's why both will compile.
But you're asking about the purpose of being able to say List<? extends T>
. This lets you remain open to receiving collections (or other parameterized types) of subclasses of the class you're working with. This is usually what you want when you're going to receive objects of a certain type and do something with them, and usually, you'd be equally happy to do that to a subtype (since a subtype should respect the same contract).
The super
keyword means that you can work with objects whose type parameter is a superclass of your type parameter. The typical case is when you want to add an object to a collection. Suppose that T
here resolves to concrete type B
. If you received a List<A> myList
then List.add(B)
would be legal, because it's OK to add a B
to a List<A>
. It's also useful if your method returns, say, a Set<T>
- in that case, the calling code can't assign it to a Set<A>
even though everything in the set is an A
, but if you instead return Set<? super T>
then it can. In your case, you're going to call Comparable<A>.compare(B)
and here, similarly, it's legal to pass an instance of B
to Comparable<A>.compare(A)
.
There's a mnemonic for this, PECS "Producer Extends, Consumer Super" discussed here: What is PECS (Producer Extends Consumer Super)?