Reputation: 378
I am studying for a Java course and my documentation gives this method header as generic example. Unfortunately, the description is lacking. Can someone explain what is going on with this typing?
public static <T extends Object & Comparable <? super T>> T max(T a, T b) {
...
...
...
}
Thanks!
Upvotes: 1
Views: 35
Reputation: 68847
As far as I know,
<T extends Object & Comparable <? super T>>
is equivalent with:
<T extends Comparable<? super T>>
So, this says:
T
, which should be comparable toT
or something that is abstracter thanT
.
This construction forces you to choose a T like MyClass
, if MyClass
is declared like one of these:
MyClass implements Comparable<MyClass>
MyClass implements Comparable<MySuperClass>
MyClass implements Comparable<Object>
But doesn't allow:
MyClass implements Comparable<String>
MyClass implements Comparable<MyChildClass>
Given that: MyChildClass extends MyClass extends MySuperClass extends Object
.
Upvotes: 1