Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

comparator interface has equals method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a question that why comparator interface has equals() method in spite that every class get it by default from Object class.Please help me .Thanks in advance.
 
Rancher
Posts: 4071
56
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because they wanted to provide additional JavaDoc comments about how to override equals for a Comparator.
 
Marshal
Posts: 74780
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
sudhakarc kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply Campbell and Mike . you mean to say that equals() method from Comparator could have been eliminated but it is just a reminder that in this case we can override equals() and how should we override it. Please explain it a little bit more because i dont think that in java there are things that have no reason. Everything thing contains some special meaning.Thanks again.
 
Saloon Keeper
Posts: 13505
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Several interfaces do this. They "override" a method declaration in a super-interface, just to expand on the general contract of the method.

A method's contract is at least as important as the method's implementation, if not more. Extending an interface for this reason is perfectly valid.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudhakarc kumar wrote:Please explain it a little bit more because i dont think that in java there are things that have no reason. Everything thing contains some special meaning.Thanks again.

The reason you've been given is the only reason. All interfaces automatically have a toString method included in them (JLS Section 6.4.4), so the only reason for explicitly including it in an interface is so that you can expand the Javadoc.
 
Mike Simmons
Rancher
Posts: 4071
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudhakarc kumar wrote:Please explain it a little bit more because i dont think that in java there are things that have no reason.


Ah, to be young again!
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In common java objects, we will decide two objects are equal by checking its one or more than one instance variables.

Form javaDoc Comparator
return true -> only if the specified object is also a comparator and it imposes the same ordering as this comparator.
Means we need to check for its business logic (This is what really makes two comparators objects equal)

Also from JavaDoc
Note that it is always *safe not* to override Object.equals(Object).

Since the POJO class can have its own equals() which would check for some of its instance variables(Which makes it really unique). If we implement comparator interface in the same POJO class and provide the equals () for comparator then we cannot check equality for POJO class, vice versa. That is the reason it mentioned it's not to override equals().

As explained earlier this is to 'just to expand on the general contract of the method'. So that we would treat comparator equals() is different from POJO equals() .
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic