| Author |
Doubt in K&B 5: Which equals() method is better?
|
Kaydell Leavitt
Ranch Hand
Joined: Nov 18, 2006
Posts: 679
|
|
I have a doubt about K&B 5.0 on page 526. The book says that you should use the instanceof method to determine whether the types are the same. I believe that it would be better to require that the classes be the same in order for the objects to be return true from the equals() method. How should the equals() method be defined? Like equals1() or like equals2()? [ September 11, 2008: Message edited by: Kaydell Leavitt ]
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Well this can create a long debate. Just to say the least, instanceof must be used. It will help you to check for equality of objects of sub-classes of Moof class. Read the following at your own risk Actually it depends whether your class is final or not. If your class is not final then any overriding class will have to be careful while overriding the equals method. If it does something wrong, then the result will be that two calls to equals might not be symmetric(a contract of equals method). Let's see it- This code might look just fine but if you call it this way Base baseObj = new Base(); baseObj.i = 10; Derived derivedObj = new Derived(); derivedObj.i = 10; derivedObj.j = 10; in this case calling baseObj.equals(derivedObj); //returns true and calling derivedObj.equals(baseObj); //returns false So this breaks symmetric behaviour. To solve this, you must either make equals final in Base. Otherwise both equals methods in Base and Derived must use getClass instead of instance of.
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Harshit Rastogi
Ranch Hand
Joined: Apr 15, 2008
Posts: 131
|
|
the only thing with instanceOf is that is a check if a check is made for will return true; this is the automatic code generated when you write a code in Eclipse IDE and ask the IDE to override equals and hashcode method. As you can see getClass() is used. Also dont forget to implement hashcode(). In the end, finally its you call but i would say use getClass() you can check the difference between instanceOf and getClass() at Help link
|
<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
|
 |
Kaydell Leavitt
Ranch Hand
Joined: Nov 18, 2006
Posts: 679
|
|
Here's what I think now. 1. I overrode the equals() method, so I needed to override the hashCode() method. 2. The test for == is an optimization for speed but is not necessary. 3. I don't want to assume that the class Moof is never going to be extended so it is safer to compare classes using the getClass() method than it is to use the instanceof operator. 4. I don't know how to use the instanceof() method, but I don't think that I need to use it here because my equals() method and my hashCode() method both obey their contracts. The following code is my solution to my exercise. I would appreciate comments.
|
 |
 |
|
|
subject: Doubt in K&B 5: Which equals() method is better?
|
|
|