Skip to content

Instantly share code, notes, and snippets.

@albanoj2
Last active February 28, 2018 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albanoj2/cc255b9866dba674f7742ac25e8e413e to your computer and use it in GitHub Desktop.
Save albanoj2/cc255b9866dba674f7742ac25e8e413e to your computer and use it in GitHub Desktop.
public class Bar extends Foo {
private final String lastName;
public Bar(String firstName, String lastName) {
super(firstName);
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
else if (!(obj instanceof Bar)) {
return false;
}
else {
Bar bar = (Bar) obj;
return Objects.equals(bar.getFirstName(), getFirstName())
&& Objects.equals(bar.lastName, lastName);
}
}
public int hashCode() { /* ... */ }
}
Foo foo = new Foo("John");
Bar bar = new Bar("John", "Doe");
System.out.println(foo.equals(bar)); // true
System.out.println(bar.equals(foo)); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment