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.
Implementing the Extends Comparable<T> Interface in Java This method compares the object with the specified object for the order. It returns a negative integer if the object is less than specified. It will return zero if the object and the specified object are equal.
The meaning is T implements Comparable interface! Also it means all the elements in the List<T> must implement Comparable interface, or it should be mutually comparable. All Wrapper classes, String, Date etc implements Comparable.
The Java Comparable interface, java. lang. Comparable , represents an object which can be compared to other objects. For instance, numbers can be compared, strings can be compared using alphabetical comparison etc. Several of the built-in classes in Java implements the Java Comparable interface.
< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.
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) { ... } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With