Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface Comparator" Watch "Interface Comparator" New topic

Interface Comparator

Eleanor Leong

Joined: Mar 24, 2006
Posts: 21
When a class implements Interface Comparator<T>, it needs to overide all abstract methods of Comparator<T> which are int compare(T o1, T o2) and boolean equals(Object obj)(indicates whether some other object is "equal to" this Comparator).

I am confused now. Then a class will have two equals(). But it doesn't seems to be the case. It seems only int compare(T o1, T o2) needs to be overriden.

Please explain.

Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
You'll always have an equals() method inherited from Object,
so if you explicitly implement equals() in a class, then you're overriding it: you won't have two methods.

The Comparator interface will be satisfied fine if you don't override equals(), but if you do, you have to make sure you obey the contract specified in the API:

See also:

Inquisition: open-source mock exam simulator for SCJP and SCWCD
Eleanor Leong

Joined: Mar 24, 2006
Posts: 21
Thanks very much for your information

Have you tried LearnNowOnline?
subject: Interface Comparator