Fixing Employee.equals
- Violates two rules
- Add test for null:
if (otherObject == null) return false
- What happens if otherObject not an Employee
- Should return false (because of symmetry)
- Common error: use of instanceof
if (!(otherObject instanceof Employee)) return false;
// don't do this for non-final classes
- Violates symmetry: Suppose e, m have same name, salary
e.equals(m) is true (because m instanceof
Employee)
m.equals(e) is false (because e isn't
an instance of Manager)
- Remedy: Test for class equality
if (getClass() != otherObject.getClass()) return false;