OU blog

Personal Blogs

Attachments:
application/zipEquality.zip
ExLibris

Overriding equals() in Java

Visible to anyone in the world
Edited by Martin Thomas Humby, Saturday, 4 Oct 2014, 14:00

You can now attach files to a post so here is a NetBeans project giving the classes from the original article and a test file.

Permalink Add your comment
Share post

Comments

neil

New comment

Hi Martin

If you are overriding  equals() you'd better be overriding hashCode() too!

arb

neo

ExLibris

New comment

Hi Neil, thanks for pointing this out.

The original article this code belongs to does discuss requirements for overriding hashCode() under each of the three possible equality relationships: 1) equals not overridden in subclass so do not override hashCode(). 2) equals overridden, subclass instance can never be equal to super-class instance: override hashCode(). 3) equals overridden, but subclass instance may be equal to superclass instance when fields match those of a super: either do not override hashCode() and rely on the overridden equalFields() to sort collisions or ensure that significant field values that allow comparison with a super are then excluded from the hashCode computation. 

Have added a link to the original (now that 'permalimks' are available in OU blogs and can make linking more specific than relying on tags)