| Author |
i have not override comparator's equals() method(overrided compare())-working fine-how?
|
Anand Sivathanu
Ranch Hand
Joined: Jun 25, 2010
Posts: 76
|
|
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?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
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
|
 |
deepak kushwaha
Ranch Hand
Joined: Jul 17, 2010
Posts: 33
|
|
|
comparator interface dont have any equal method!!!
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32667
|
|
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
Joined: Jul 17, 2010
Posts: 33
|
|
oops sorry for that
|
 |
Anand Sivathanu
Ranch Hand
Joined: Jun 25, 2010
Posts: 76
|
|
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
Joined: Jul 17, 2010
Posts: 33
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
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
Joined: Jul 17, 2010
Posts: 33
|
|
|
yes thats what i wanted to say
|
 |
Anand Sivathanu
Ranch Hand
Joined: Jun 25, 2010
Posts: 76
|
|
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
Joined: Jul 17, 2010
Posts: 33
|
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32667
|
|
Do you mean compare or equals?
1: Object doesn't have a public compare() method.2: Comparator<T> has a public compare(T, T) method3: 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
Joined: Nov 04, 2009
Posts: 2066
|
|
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!
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32667
|
|
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
Joined: Jun 25, 2010
Posts: 76
|
|
thank you guys
I have understood through your detailed explaination.
thank you for the valuable reply
thanks a lot
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32667
|
|
You're welcome
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
|
|
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.
|
 |
 |
|
|
subject: i have not override comparator's equals() method(overrided compare())-working fine-how?
|
|
|