| Article: |
Don't Let Hibernate Steal Your Identity | |
| Subject: | Instanceof in equals() | |
| Date: | 2006-09-14 09:58:33 | |
| From: | jbrundege | |
|
Response to: Instanceof in equals()
|
||
|
I don't agree with the idea that you should never use instanceof in an equals() method. You have to test the class of the given object somehow, and the two techniques are instanceof and getClass(). instanceof allows subclasses to inherit the equals() method, but with the danger that someone will override equals in a subclass which breaks the symmetric property of equals(). The alternative is to use getClass(), which only compares the runtime types and does not consider subclasses equal to their superclasses. This has the inverse danger when the author of the subclass intends to inherit equals() and is surprised to find that their subclass can never be equal to an instance of the parent class. This is a somewhat contentious issue. Josh Block has written about it here: http://www.artima.com/intv/bloch17.html
|
||
Women in Technology
Hear us Roar
