Last active
February 28, 2018 14:31
-
-
Save albanoj2/cc255b9866dba674f7742ac25e8e413e 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 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