<Object>.equals
Contents
The <Object>.equals function (by definition) must be equivalent in the following ways:
- reflexive -
x.equals(x) - symmetric -
x.equals(y) && y.equals(x) - transitive -
x.equals(y) && y.equals(z) && x.equals(z) - consistent -
while (true) x.equals(y) x.equals(null) == false
Implementing <Object>.equals
In writing an equals function, we perform the following routine:
- Add the
@Overrideannotation before the method signature - Check that the supplied
objis notnull - Check that the classes match
- Cast the
objto the correct type - Perform necessary equality checking
| |
Note: Reminder, that to compare strings we need the <String>.equals function, rather than the == operator
Inherited classes
For inherited classes, it is beneficial to test the equality of their superclasses.
| |