Skip to content

@yshavit /gist:212ea023e549e833d250 secret
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
public class Animal {
@Override
public final boolean equals(Object other) {
// take care of the boilerplate stuff and ensure that "other" is of the same exact class as this
if (other == null) return false;
if (other == this) return true;
if (other.getClass() != this.getClass()) return false;
Animal otherAnimal = (Animal) other;
return animalFieldsAreEqual(otherAnimal);
}
protected boolean animalFieldsAreEqual(Animal other) {
return other.legCount = legCount; // or whatever
}
@Override
public int hashCode() {
// remember to override this, too!
}
...
}
public class Dog extends Animal {
@Override
protected boolean animalFieldsAreEqual(Animal other) {
if (!super.animalFieldsAreEqual(other)) return false;
Dog otherDog = (Dog) other; // guaranteed to work due to the class checking in equals
return otherDog.breed = breed; // or whatever
}
...
@Override
public int hashCode() {
// remember to override this, too!
int hash = super.hashCode();
hash = ...
return hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.