Why is .compareTo() in an interface while .equals() is in a
- The early [JLS 1.0 §4.3.2](http://titanium.cs.berkeley.edu/doc/java-langspec-1.0.pdf) defines the class `Object` in the following way:
- > 4.3.2. The Class `Object`
- >The standard class `Object` is a superclass (§8.1) of all other classes. (...) All class and array types inherit the methods of class `Object`,
- which are summarized here (...)
- > The members of `Object` are as follows:
- > The method `getClass` returns the `Class` (§20.3) object (...)
- > The method `toString` returns a `String` representation of the object.
- > The methods `equals` and `hashCode` are declared for the benefit of hashtables
- such as `java.util.Hashtable` (§21.7). The method equals defines a notion
- of object equality, which is based on value, not reference, comparison.
- > The method `clone` is used to make a duplicate of an object.
- > The methods `wait`, `notify`, and `notifyAll` are used in concurrent programming
- using threads, as described in §17.
- > The method `finalize` is run just before an object is destroyed and is
- described in §12.6.
- So, that's why `equals` is in `Object` but `compareTo` is in a separate interface. Java devs included only the methods immediately needed for all uses intended by then. To be hashable, every `Object` would need `equals` and `hashCode` - but not all objects would need to have a concept of **ordering**, which is what `compareTo` is used for.
RAW Paste Data