• 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

i have not override comparator's equals() method(overrided compare())-working fine-how?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i have written by own sorting for StdNameComparator.I have overrided compare() only.but not equals().
It working fine.

I believe that method declared in the interface(Comparator), must be override by the implementing class(StdNameComparator).

but StdNameComparator is working successfully, eventhough i have not override the equals() method.

Any suggestion?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in your compare method you are doing the check with object's properties.

*if you override equals, then you can compare objects simply as obj1.equals(obj2)*

Note: in your case you are comparing String object, some one already override equals method in string class for you. in any case,if you are comparing an attribute[object] which is not overriding equals then, you will end up with problems.

hth

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
comparator interface dont have any equal method!!!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak kushwaha wrote:comparator interface dont have any equal method!!!



I don't know, why you are asking about the equals() of the StdNameComparator class? It's a third party class which is used to sort another class' object(in your case, Student object). And your StdNameComparator class sorts the Students object according their name(a String) attribute.

Or else, you need check the equality of two StdNameComparator object? What is the purpose?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak kushwaha wrote:comparator interface dont have any equal method!!!

It's "equals(java.lang.Object)" not "equals", and yes it does. Here it is.

It is not essential to override the java.util.Comparator#equals(java.lang.Object) method, unless you need to confirm identicality of two Comparators.
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops sorry for that
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie

As you told

It is not essential to override the java.util.Comparator#equals(java.lang.Object) method, unless you need to confirm identicality of two Comparators



my doubt here is

we already know that Comparator is an interface,so we must override all methods present in an interface.

without override equals() how it work?
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually you are comparing strings and string by default have implemented equals method. if you were comparing two objects then you need to override this method
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Sivathanu wrote:
we already know that Comparator is an interface,so we must override all methods present in an interface.
without override equals() how it work?



Every class implicitly extends the base class Object, so do we need override the equals() method, unless if there is our requirements? And check the method API in Comparator class!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak kushwaha wrote:actually you are comparing strings and string by default have implemented equals method. if you were comparing two objects then you need to override this method



To compare two objects, we don't need to override the equals() method, DO override the equals() method, when you need to check equality of two object of a particular class.
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes thats what i wanted to say
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know that equals() method is in Object class.

comparator also has equals() method,which is needed to be overriden.

All the methods present in interface must be overridden by the implementing class.


this is the rule in java,if i am not wrong.

coming back to point,
I have overrided only the compare().
I have not override the equals().

I have not overrided all the methods present in comparator interface.
how it works?
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ok. let me explain. we all know that every object in java extends object class. and guess what object already have a compare method. now since you are doing operations on object it already inherited the equals method. say object1.equal(object2) , now its your choice if you want to override the equals method or not, but it is there already. even if you cant see it.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean compare or equals?

  • 1: Object doesn't have a public compare() method.
  • 2: Comparator<T> has a public compare(T, T) method
  • 3: You must provide an implementation of compare(T, T) in any class implementing Comparator<T>
  • 4: Comparator<T> has a method equals(Object)
  • 5: An instance of your Comparator<T> must implement an equals(Object) method.
  • 6: All objects have an equals() method from Object or from their own class.


  • It is all explained in the link I provided earlier. How many of you have actually read that link?

    And what was said about comparing two Strings and String already having an equals method was not at all useful information.
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anand Sivathanu wrote:i know that equals() method is in Object class.

    comparator also has equals() method,which is needed to be overriden.

    All the methods present in interface must be overridden by the implementing class.


    this is the rule in java,if i am not wrong.

    coming back to point,
    I have overrided only the compare().
    I have not override the equals().

    I have not overrided all the methods present in comparator interface.
    how it works?



    Please check the API of Comparator and its equals() method! Campbell Ritchie already gave the link! In that they clearly mentioned it!
     
    Master Rancher
    Posts: 4796
    72
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anand Sivathanu wrote:

    All the methods present in interface must be overridden by the implementing class.


    this is the rule in java,if i am not wrong.


    You are wrong.

    All methods in an interface need to be implemented somewhere, eventually. But they don't necessarily have to be implemented in the same class that declares that it implements the interface - let's call that class C. There are two other possibilities:

    1. The methods can be implemented in subclasses of C. This requires that C be declared as abstract. Any concrete (non-abstract) subclasses will have to implement the interface methods.

    2. The methods can already be implemented in a superclass of C. That's what's happening here. The Object class already has an equals(Object) method, and that's good enough for the compiler to be satisfied that yes, the equals(Object) method in the interface has been implemented. It's still possible to override this implementation with a new and better implementation, based on the comments in the JavaDoc for the interface. But it's not required by the compiler.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Abimaran Kugathasan wrote: . . . the link! In that they clearly mentioned it!


    I see you have actually read the link

    And Mike Simmons has explained it too.
     
    Anand Sivathanu
    Ranch Hand
    Posts: 76
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you guys

    I have understood through your detailed explaination.

    thank you for the valuable reply

    thanks a lot
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Abimaran Kugathasan wrote: . . . the link! In that they clearly mentioned it!


    I see you have actually read the link

    And Mike Simmons has explained it too.



    Thanks Campbell, are there any implicit message in your bold 'you'? I couldn't understand it.
     
    Mike Simmons
    Master Rancher
    Posts: 4796
    72
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I see you have actually read the link

    And Mike Simmons has explained it too.


    To be fair, I was trying to concentrate on aspects not directly addressed in the link (which of course I had read). The JavaDoc points out that you don't have to override equals(Object), but doesn't really go into why you don't have to.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Folks,
    It's been very interesting question that why we need to override compare method in Comparator interface and not equals method.

    1. As we know in java every class and interface are children of Object Class.
    2. Since Comparator interface is also child of Object Class, it contains hashcode,equals and string methods etc..
    3. In Comparator interface object's equals method had been overriden.
    4. While implementing Compartor interface developer need to override compare method alone enough. since every class have equals method on their own it automatically overrides equals method of Comparator interface.
    5. still if you want to override equals method in after implementing Compartor interface but it is not advisable

    If you can't understand please consider below thought.
    1. class C implements interface A and interface B.
    2. Both interfaces A and B have same method like doPrint()

    Now the question is class C override doPrint() method of A or B ?
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    Your points 1 2 3 are completely wrong, I am afraid, and 5 is probably incorrect too. There is only a minor error in No 5.
    [edit]It is no 4 that has the minor error. Sorry for that mistake.
     
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Gobi Shanmugam wrote:If you can't understand please consider below thought.
    1. class C implements interface A and interface B.
    2. Both interfaces A and B have same method like doPrint()

    Now the question is class C override doPrint() method of A or B ?



    The answer to that one is: Yes, it does. In fact it overrides both of them.
     
    Bartender
    Posts: 1845
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Being nitpicky.  Does it override the method, or implement it?
    An interface doesn't actually have a method body you can override...
     
    Paul Clapham
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Whatever you want to call it -- the method in the implementing class overrides, or implements, the method from both interfaces.

    (There's probably a correct answer which can be found in the JLS, for those who want to know.)
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    implements
     
    Why is the word "abbreviation" so long? And this ad is so short?
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic