Today I come across two different ways of the overriding the equals method. The first one is with same class strategy which is:
public boolean equals(Object aPerson){
if(aPerson == null) return false;
if(aPerson.getClass() != this.getClass()) return false;
Person p = (Person) aPerson;
return this.name.equals(p.name);
}
and with instance of strategy to be like below:
public boolean equals(Object aPerson){
if(aPerson == null) return false;
if(!(aPerson instanceof Person)) return false;
Person p = (Person) aPerson;
return this.name.equals(p.name);
}
Do both of the implementation work the same way in any condition? Or do they have exception of working for one some times and not working for the other in special case?
My questions focuses on !(aPerson instanceof Person) and aPerson.getClass() != this.getClass().