Search code examples
javacomparable

What is point of implementing the Comparable interface in my classes?


I don't get the point of implementing the Comparable interface, since I can't use the comparison operators <, <=, >=, and > for my custom classes like I would be able to with operator overriding in languages like C++; I still have to call the compareTo method directly.

I could write my own boolean methods like isEqual or bigger, which would be just as useful, if not more so, than the compareTo method.

Am I missing something? What is the point of implementing it?


Solution

  • The Comparable interface provides a means of communication to the implemented sorting algorithms, which would be impossible using custom methods for comparison.