SWE 619 In Class Exercise Number 0A
August 29, 2017


Instructions: Work with your neighbors in groups. This exercise touches on some of the thorny issues in data abstraction and inheritance. There is a lot going on in this example. Hence don't worry if it seems confusing today. We'll revisit this example several times over the course of the semester.

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);
    }
}
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;
    }
}
  1. What does it mean for the equals() method to be correct? How do you know? Be as concrete as you can. Can you check correctness with test cases?




  2. How does the given implementation of equals() in in class User stack up? Again, be concrete. Can you find a test case that demonstrates the problem?




  3. How does inheritance complicate the correctness discussion for equals() in class SpecialUser?




  4. What do you think of the equals() method in the SpecialUser class?