Why use super in crazy generics module #158
-
In MaxHolder or StrictProcessor class, the input param has been defined as Comparable<? super T>. Why use super instead of specific type T i.e. Comparable< T > ? Adding the code snippet for more clarity: public static class MaxHolder<T extends Comparable< T >> { // vs MaxHolder<T extends Comparable<? super T>>
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Suppose we have next classes: class A implements Comparable<A>
class B extends A With |
Beta Was this translation helpful? Give feedback.
Suppose we have next classes:
With
MaxHolder<T extends Comparable< T >>
you will not be able to putB
in MaxHolder, becauseB
does not implementComparable<B>
, it implementsComparable<A>
.By using
MaxHolder<T extends Comparable<? super T>>
you can do that because MaxHolder will acceptComparable<A>
.May be useful: https://stackoverflow.com/a/25783345