• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Comparator

 
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?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • 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)

 
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.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic