You can define the compareTo
method in a class without implementing the
Comparable
interface. What are the benefits of implementing the Comparable
interface?
Asked
Active
Viewed 2,092 times
-1

Wololo
- 841
- 8
- 20
-
1[What is the point of interfaces?](http://programmers.stackexchange.com/questions/131332/what-is-the-point-of-an-interface) – Sotirios Delimanolis Jul 05 '15 at 18:36
1 Answers
8
The benefit of implementing the interface is that some methods specifically require object that implements the Comparable
interface. It gives them a guarantee that the object you're passing has a compareTo
method with the correct signature.
There's no way in Java to have a method require that an object implement any given method (such as compareTo
) in and of itself. To get around this, interfaces were created. Any time you have an object that you know is a Comparable
, you also know you can call compareTo
on it.

Sam Estep
- 12,974
- 2
- 37
- 75
-
-
2@Saud Did you look at the question of which yours was marked as a duplicate? – Sam Estep Jul 05 '15 at 18:53
-
-
@Saud I would advise just looking on the internet for more information about Java interfaces and learning that. If you *really* need help from Stack Overflow, put a bounty on [that other question](http://stackoverflow.com/questions/504904/how-are-java-interfaces-actually-used) and explain what exactly you don't understand. – Sam Estep Jul 05 '15 at 18:59
-