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/9a561c3e01c20450283ad8e4d99a860d to your computer and use it in GitHub Desktop.
Save albanoj2/9a561c3e01c20450283ad8e4d99a860d to your computer and use it in GitHub Desktop.
public class Foo {
private final String firstName;
public Foo(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
else if (!(obj instanceof Foo)) {
return false;
}
else {
Foo foo = (Foo) obj;
return Objects.equals(foo.firstName, firstName);
}
}
public int hashCode() { /* ... */ }
}
public class Bar extends Foo {
public Bar(String firstName) {
super(firstName);
}
}
Foo foo = new Foo("John");
Bar bar = new Bar("John");
System.out.println(foo.equals(bar)); // true
System.out.println(bar.equals(foo)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment