When providing a proper version of equals,
use code like:
public boolean equals(final Object pObject)
if ( ! (pObject instanceof Date) )
final Date tDate = (Date)pObject;
return iYear==tDate.iYear && iMonth==tDate.iMonth && iDay==tDate.iDay;
causes problems if later
inheritance is used to subclass this class
and you want the subclass to override the
The problem is that this
does not satisfy a rule of the contract of
For more information about this, see:
pages 44 to 59 of the book
by Peter Haggar;
an article by Mark Roulo
'How to avoid traps and correctly override methods from java.lang.Object'
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
methods of their classes,
thereby making consistent equality comparisons difficult’.