I am using IntelliJ to auto generate the equals
and hashCode
function for following two classes. I found in the Dog
class' equals
method, it is doing
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Should these two lines be omitted since they're in the parent class? I thought when calling super.equals()
, those checks would happen in the parent class.
class Animal{
String eye;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Animal animal = (Animal) o;
return Objects.equals(eye, animal.eye);
}
@Override
public int hashCode() {
return Objects.hash(eye);
}
}
class Dog extends Animal{
String hair;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Dog dog = (Dog) o;
return Objects.equals(hair, dog.hair);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), hair);
}
}