Consider the following (textbook) code:
public class User { private String name; public User (String name) { this.name = name; } @Override public boolean equals (Object obj) { if (!(obj instanceof User)) return false; return ((User) obj).name.equals(this.name); } // other methods omitted } public class SpecialUser extends User { private int id; public SpecialUser (String name, int id) { super(name); this.id = id; } @Override public boolean equals (Object obj) { if (!(obj instanceof SpecialUser)) return false; return super.equals(obj) && ((SpecialUser) obj).id == this.id; } // other methods omitted }
equals()
method
in class User
for a few well-chosen (pairs of) objects.
What happens at each point in the execution?
equals()
implementation
to be correct? How do you know?
Be as concrete as you can.
equals()
in class User
correct?
Again, be concrete.
If there is a problem, find a specific object (test case!) that demonstrates the problem.
equals()
in class SpecialUser
?
equals()
method in the SpecialUser
class?