Firstly, you could have solved it by having Vehicle[]
which you then added Truck
s to.
The reason you need <? super T>
goes back to the generics rule that Comparator<Truck>
is not a subtype of Comparator<Vehicle>
; the unbounded type T
must match exactly, which it doesn't.
In order for a suitable Comparator
to be passed in, it must be a Comparator
of the class being compared or any super class of it, because in OO languages any class may be treated as an instance of a superclass. Thus, it doesn't matter what the generic type of the Comparator
is, as long as it's a supertype of the array's component type.