1

I wanted to ask why we use the comparable interface in java? Wouldn't it be simpler to just make the compareTo method without using the comparable interface?

Instead of doing this:

//some class that implements comparable

public int compareTo(someClass someInstanceOfThatClass) {
    // do stuff that returns -1,0,or 1 
}

Why can't we just do this:

//some class that does NOT implement comparable, but we still 
//make a compareTo method 

public int compareTo(someClass someInstanceOfThatClass) {
    // do stuff that returns -1,0, or 1
}

I guess my question is why do we bother implementing comparable, if we could just make a compareTo method without being forced to by some interface (comparable)?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Because nobody can call it unless you do. – user207421 Aug 14 '20 at 09:49
  • 2
    @Michael This is not about static vs. dynamic typing; this would work fine in Typescript but Typescript is also statically typed. It's about nominal vs. structural types. – kaya3 Aug 14 '20 at 09:51
  • Java doesn't use [duck typing](https://en.wikipedia.org/wiki/Duck_typing): for something to be a duck, it has to implement the `Duck` interface (or extend the `Duck` class). – Andy Turner Aug 14 '20 at 09:54
  • @MIchael Very poor choice of duplicate. I'm sure it *is* a duplicate, but not of that. He didn't ask why it can't be called. He asked why the entire interface is necessary. – user207421 Aug 14 '20 at 09:55
  • @kaya3 I haven't used it, but to my understanding Typescript is not statically typed, but gradually typed. The type system is optional. But yes, I did make the mistake of conflating dynamic and nominal. – Michael Aug 14 '20 at 09:56
  • Typescript's type system is optional in the sense that it has a special type `any` which is assignable to and from all other types, and missing type annotations (if they cannot be inferred) are assumed to be `any` by default. Typescript is statically typed because the types are checked at compile-time (and indeed, only exist at compile-time). – kaya3 Aug 14 '20 at 09:59
  • @MarquisofLorne "Because nobody can call it unless you do." Poorly phrased. It's perfectly possible to call it, just not as if it were Comparable::compareTo – Michael Aug 14 '20 at 10:05
  • @Michael Only from a class that is bound to the exact class containing the `compareTo()` method or a derived class. – user207421 Aug 15 '20 at 00:54

3 Answers3

3

Comparable is an interface, hence it imposes a contract that others may follow. For example, calling Collections.sort(list) only works if the elements are instances of Comparable, and internally relies on the compareTo method to implement the sorting.

M A
  • 71,713
  • 13
  • 134
  • 174
2

Java's type system is nominal, not structural, so simply having the method required by the interface is not enough to implement it; you also have to declare that the class implements the interface. In some other languages such as Typescript, having the method with the right signature would be enough, but Java is not like that.

If you are only calling the compareTo method from your own code then this may not matter, but if you are using classes or methods from the standard library or from other libraries which take Comparable things as arguments, then your class will need to implement the interface so you can pass your objects to them.

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • Definitely makes sense. I hadn't taken into consideration the usage of classes in JAVA API. Thank you for the answer and I have to say that you have taught me some new words :) ! I'm definitely going to read up on duck typing, nominal vs structural type systems, etc.. – FightBreastCancer Aug 14 '20 at 10:03
0

I think it returns to the innate concept of the interface. You always sure that every class which has implemented Comparable interface, has ability to be compared and sometimes you need this assurance. For example if you have a method that have a parameter with Comparable type, then you are sure that comapreTo is implemented with that parameter and this parameter issemantically comparable. But whitout interface you can't get this assurance.

Pasha
  • 1,534
  • 15
  • 27