• 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

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

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic