Originally posted by Thennam Pandian:
When you implement an interface you should implement all its methods.
But when we implement Comparator interface you can leave the equals()
method [unimplemented]. where this method is implemented? (A Interface can't
implement it's method ...)
Realize that the interface is repeating the equals method of class Object, but with a refined specification:
Indicates whether some other object is "equal to" this Comparator. This method must obey the general contract of Object.equals(Object). Additionally, this method can return true only if the specified Object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.
Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct Comparators impose the same order.
When your Comparator doesn't define it's own equals method, it's inheriting this method from Object.
As another example of an interface repeating an Object method with more to its specification, take a look at equals() and hashCode() in java.util.List.