-1

You can define the compareTo method in a class without implementing the Comparable interface. What are the benefits of implementing the Comparable interface?

Wololo
  • 841
  • 8
  • 20

1 Answers1

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
  • I'm still confused. Examples/analogies would help.. – Wololo Jul 05 '15 at 18:49
  • 2
    @Saud Did you look at the question of which yours was marked as a duplicate? – Sam Estep Jul 05 '15 at 18:53
  • yeah but that didnt help either – Wololo Jul 05 '15 at 18:57
  • @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
  • ok... **thanks** for ur help – Wololo Jul 05 '15 at 19:26