This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B 5: Which equals() method is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B 5: Which equals() method is better?" Watch "Doubt in K&B 5: Which equals() method is better?" New topic

Doubt in K&B 5: Which equals() method is better?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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

Joined: Aug 03, 2008
Posts: 9400

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

My Techie Blog - Java Questions
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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.

I agree. Here's the link:
subject: Doubt in K&B 5: Which equals() method is better?
It's not a secret anymore!