58

The full context being:

public class RClass<T extends Comparable<T>>

Would I be right in saying that the statement in the title means that the arguments plugged into the method must either be an object of a class which implements Comparable OR one of its derived classes?

Thanks.

LTH
  • 1,779
  • 3
  • 19
  • 21
  • 14
    Note, `>` (I think you can do that there), would be preferable, as you might want `T` to be more precise than its implementation of `Comparable`. – Tom Hawtin - tackline Dec 16 '11 at 19:03
  • 2
    @TomHawtin-tackline Can you give an example of why this be necessary? Say i have my List data structure that only works with comparable types. The way I would specify this is public class MyList>. I think that would state and enforce that my data structure only works with comparable types, How would your phrasing make a difference? – committedandroider Feb 17 '15 at 01:59
  • @TomHawtin-tackline You might want a natural ordering by *exactly* the type you are dealing with, because polymorphic less than is *funky* – Caleth Apr 26 '19 at 12:31
  • @committedandroider `java.sql.Timestamp` (which is `Comparable`). This example is a bit "funky", but if you're dealing with SQL, as many people have to, then it is what you've got. – Tom Hawtin - tackline Apr 27 '19 at 13:21

7 Answers7

56

This means that the type parameter must support comparison with other instances of its own type, via the Comparable interface.

An example of such a class is provided in the Oracle tutorial Object Ordering. Note the similar pattern to T extends Comparable<T> in the excerpt below:

public class Name implements Comparable<Name> {
   ...
   public int compareTo(Name n) { ... }
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 7
    Why not just have Name implements Comparable? Why need to specify the class name again? – committedandroider Feb 17 '15 at 01:57
  • 14
    @committedandroider because Comparable is a generic interface you need to specify the type – Alex Bitek Jun 23 '15 at 07:59
  • 3
    This is a good answer. But it's interesting in bounded generic types, Java doesn't differentiate between interface implementation and class inheritance - `>` is not legal. – flow2k Jun 28 '18 at 16:27
  • 2
    Why is extends allowed here if `Comparable` is an interface? Shouldn't it be `implements`? You can't extend an interface. – piepi Jun 06 '21 at 10:23
  • @piepi - Because it's useful, and because the [JLS](https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.1.2) says so. Using a single keyword to indicate subtyping in generic type parameters allows cases like `T extends U`, that work whether U is a class type or interface type. This differs from class declarations, in which you're subtyping known (though possibly parameterized) types. – Andy Thomas Jun 06 '21 at 12:44
21

Java- The meaning of <T extends Comparable<T>>?

a) Comparable <T> is a generic interface (remember it's an "interface" i.e not a "class")

b) extends means inheritance from a class or an interface.

From above-said point#a, it is an interface..(Remember it is an inheritance from an "interface" i.e not from a "class")

c)From above-said both points #a & #b,

here "one interface" extends "another interface".

There should be an interface defined for this class.. just an example here is

interface MinMax<T extends Comparable<T>> { 
    T min(); 
    T max(); 
} 

d) now your class i.e public class RClass {} SHOULD

1# EITHER "implement" this "generic interface" Comparable<T> ..!!!

ex: public class RClass<T> implements Comparable<T>

2# OR create an interface and extend to this "generic interface" Comparable<T> ex:

interface MinMax<T extends Comparable<T>> { 
   T min(); 
   T max(); 
} 
class RClass<T extends Comparable<T>> implements MinMax<T> {
    .....
    .....
}

Here, Pay special attention to the way that the type parameter T is declared by RClass and then passed to MinMax. Because MinMax requires a type that implements Comparable, the implementing class (RClass in this case) must specify the same bound. Furthermore, once this bound has been established, there is no need to specify it again in the implements clause.

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
guhan vj
  • 219
  • 2
  • 2
  • 1
    In the question, it's been asked that `public **class** RClass>`. It's a class and not an interface. So please comment on what will be the meaning if it's a class. – Sathish Kumar k k Sep 09 '15 at 13:22
  • great answer. question: why `class A>` but not `class A>`? For example, `A` can be `A`. Here `Integer` is `T` and `Integer` class `implements `Comparable`. – Jesse Zhuang Mar 02 '20 at 22:26
9

Somewhere in that class, the programmer needs to write something like

if(t.compareTo(othert) < 0) {
    ...
}

For that to work, the type T must have a compareTo-method which compares it to another object of type T. Extending Comparable guarantees the existence of such a method, among other things.

jforberg
  • 6,537
  • 3
  • 29
  • 47
4

It means that you can only create an instance of RClass with a type which quite literally extends Comparable<T>. Thus,

RClass<Integer> a;

is acceptable, since Integer extends Comparable<Integer>, while

RClass<Object> b;

is not, since Object is not a class which extends comparable at all.

djhaskin987
  • 9,741
  • 4
  • 50
  • 86
2

Simply put, the generic type T must be comparable in order to compareTo. otherwise you cannot do T.compareTo. In Item 28 Effective java, it suggests: "always use Comparable<? super T> in preference to Comparable<T>. <T extends Comparable<? super T>>"

Andrew
  • 784
  • 9
  • 15
2

Yes, and bear in mind that objects of classes derived from Comparable ARE Comparable objects. Inheritance is a is-a relationship.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

==>guhanvj, <T extends Comparable<T>> means that <T> is having upper bound of Comparable<T> objects. So <T> can have types of Byte, Character, Double, Float, Long, Short, String, and Integer classes which all implements Comparable<T> interface for natural ordering.