• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Comparator

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic