I am trying to create a generic list which sorts the items entered into it using the .compareTo() method of the type. However, I ran into a problem in the very first line. Since the type must be one which implements Comparable<T>, is there any way to enforce this? I suppose the syntax :
public class GenList<T implements Comparable<T>>{
//Class body.
}
won’t work. Any suggestions? Thanks in advance!
I know it’s a bit counter-intuitive, but for this you write
extendsrather thanimplements:(Note that I also changed the
?toT, which I think is what you meant. A reference can have typeGenList<?>, or typeGenList<? extends Comparable<String>>, or whatnot, but it doesn’t make sense to declare the class itself as taking a wildcard parameter.)