java - What is the use of recursive type bound in this case -
i have following piece of code
public static <t extends comparable<t>> t max(list<t> list){ iterator<t> iter = list.iterator(); ... // code finding max element in list ... } my question is, need recursive type bound in case? far understanding, t extends comparable < t > means that, t can compared t itself. comes fact that, class 't' implements comparable<t> , so,
class myclass implements comparable<myclass>{ } if wrong here, feel free correct me.
so coming actual doubt, why need specify < t extends comparable < t > > ? ways argument list<t> list going contain elements of same type. i.e., suppose pass parameter of type list < myclass2 > , elements going of same type, i.e. myclass2..
so implication of recursive type bound? in advance.
why need specify <t extends comparable<t>>? because comparable not require implementing class comparable itself. in theory write like
class circle extends shape implements comparable<integer> { ... } since comparable not make restriction, have on end when writing function. however, given bound more restrictive need be. make call
public static <t extends comparable<? super t>> t max(list<t> list) { ... } that mean t comparable superclass of itself. reason want given in fruit example of rgettman. t still comparable itself, comparable not have defined respect t itself.
Comments
Post a Comment