Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
Master Rancher
Posts: 4932
75
  • 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: 79530
379
  • 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: 15656
367
  • 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
Master Rancher
Posts: 4932
75
  • 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() .
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic