When writing a method that takes two objects, with two type parameters in a subtype-supertype relation, what is the best way of declaring your intentions out of these options?
Declare both
super
andextends
:public static <T> void copy(List<? super T> dst, List<? extends T> src) { ... }
Declare just the
extends
:public static <T> void copy(List<T> dst, List<? extends T> src) { ... }
Declare just the
super
:public static <T> void copy(List<? super T> dst, List<T> src) { ... }
From my understanding, all three are correct, and are equivalent to each other, as all you're interested in is the relative inheritance of the type arguments of dst
and src
. So which is better?
I think extends is most common and if there is no need to use both you shouldn't. So I'd go with extends only.
So there is not a correct way, unless you have a convention that specifies it.