This is probably a larger question that you might realize.
The Java language provides an operator "instanceof" to check the runtime type of an object.
It has been said that, in most cases, instanceof should not be used to test for object equality as it can break the symmetric property of the equals method's equivalence relation. In this case, using the getClass method of Object is the appropriate choice. This makes a lot of sense.
The getClass example would work in a similar manner:
As an even further alternative, you may consider using something like this:
To determine if o1 is a superType pf o2. This may or may not be useful, depending on how you wish to compare o1 and o2
I don't think one can be prescriptive like this. There are two possible approaches (treat different subclasses as possibly equal, or treat them as definitely unequal). Each approach could be appropriate in some circumstances and inappropriate in others.
It's one of those cases where you have to think carefully, make your choice, and explain it in the javadoc comments.
If your equals() method is not expecting to deal with different subclasses, you could add an assertion to check that it doesn't get given them. Or, if there is no need for subclasses in your application, you could declare the class "final", and use the javadoc to explain that this is to avoid potential confusion when comparing objects. [ October 29, 2007: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.