When providing a proper version of equals,
many authors
use code like:
public boolean equals(final Object pObject)
{
if ( ! (pObject instanceof Date) )
{
return false;
}
final Date tDate = (Date)pObject;
return iYear==tDate.iYear && iMonth==tDate.iMonth && iDay==tDate.iDay;
}
This code
causes problems if later
inheritance is used to subclass this class
and you want the subclass to override the
Date's
equals.
The problem is that this
equals
does not satisfy a rule of the contract of
equals
mentioned in
Object's
WWW pages.
For more information about this, see:
pages 44 to 59 of the book
'Practical Java'
([11])
by Peter Haggar;
an article by Mark Roulo
entitled
'How to avoid traps and correctly override methods from java.lang.Object'
([14]).
Peter Haggar says:
‘A quick glance through the source code of the Java libraries
shows the use of instanceof in
equals method implementations is common.
You also find the use of getClass.
The Java libraries are not consistent in how they implement the
equals
methods of their classes,
thereby making consistent equality comparisons difficult’.