Either in Javadoc as well as the code itself, Comparator interface defines:
int compare(T o1, T o2);
boolean equals(Object obj);
But then this gives no probs compilating:
Comparator a = new Comparator() {
@Override public int compare(Object o1, Object o2) {
//..
}
};
But this does:
Comparator a = new Comparator() {
@Override public boolean equals(Object comparator) {
//..
}
};
How its done for the interface for allowing us not to override method?
First of all JavaDocs explain clearly that you should implements this method:
But later:
How is it possible not to override
equals(), even though it is part of an interface? Because this method is already implemented for each and every object in Java (inObjectclass).The declaration in the interface is there only to emphasise the importance of
equals()with regards toComparatorby adding extra JavaDoc explanation.BTW if your comparator is stateless you should have only one instance of it – in which case the default
equal()implementation is just fine.