• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparator  RSS feed

 
Chris Bremer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my Cattle Drive OOP-3 method LastNameCompare, I implement Comparator yet I don't override the equals(object obj) method. I am referencing a java book that says when you implement an interface, all methods associated with it must be overriden...can anyone clarify this for me?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Comparator is an interface. So in your case your class will need to implement Comparator and of course override the necessary methods (which compareTo(Object o1, Object o2)

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Bremer wrote:In my Cattle Drive OOP-3 method LastNameCompare, I implement Comparator yet I don't override the equals(object obj) method. I am referencing a java book that says when you implement an interface, all methods associated with it must be overriden...can anyone clarify this for me?


If it truly says "all methods associated with it must be overriddent," then it's a poorly written book indeed.

The rule is that a concrete class cannot have any abstract methods. All the methods defined in an interface are implicitly abstract, so any class that implements the interface must provide implementations for those method--or else itself be delcared an abstract class.

HOWEVER, your class already provides an equals() implementation. Every single class does, by virtue of the fact that the inherit it from Object. So if you don't provide your own equals() method, you'll just use Object's; two distinct Comparator objects will never be equal in that case, Comparator explicitly names equals() even though it's not necessary in order to provide documentation about what equality is intended to mean for Comparators. It even says right there in the docs, "it is always safe not to override Object.equals(Object)."

And do note that the equals() method is for equality of Comparator objects, not of the objects they're comparing.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!