Reputation: 90
hi guys i have a little problem here to solve.
I have created an equals() method which is only based on the class of the object so that if two obj are of the same class the obj1.equals(obj2) return true. now my question is, How do i implement a hashcode() based only on the obj Class ??
@Override
public boolean equals(Object obj){
if(obj.getClass() == this.getClass()){
return true;
}else{
return false;
}
}
Upvotes: 0
Views: 197
Reputation: 393771
You could return the hashCode
of the Class
instance:
public int hashCode ()
{
return getClass().hashCode();
}
This would ensure that two objects which are equal based on your equals()
implementation would have the same hashCode()
.
Upvotes: 1