In order to implement Comparable in a class, what method(s) must be defined in that class?

In order to implement Comparable in a class, what method(s) must be defined in that class?

a)       equals
b)       compares
c)       both lessThan and greaterThan
d)       compareTo
e)       both compares and equals

Answer:  d.

Explanation:  The Comparable class requires the definition of a compareTo method that will compare two objects and determine if one is equal to the other, or if one is less than or greater than the other and respond with a negative int, 0 or a positive int.  Since compareTo responds with 0 if the two objects are equal, there is no need to also define an equals method.


Enhancing Classes

Learn More Multiple Choice Question :