Last active
February 28, 2018 14:31
-
-
Save albanoj2/9a561c3e01c20450283ad8e4d99a860d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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