Re: Why this overloading example works this way?
I think you should be careful with this practice. In the one hand, you
have to think that equals() and hashcode() are intimatelly related, so
you need to kkep track of changes in more methods when you overload
equals.
On the other hand, overloading fixes the actual method to call at
compile time (not runtime, as overriden methods do), so for example,
all the java Collections implementations will ALLWAYS use the
equals(Object) version, never use the one you created. In this case you
may solve the problem my implementing Comparable on your objects.
Gabriel
Oliver Wong ha escrito:
"Gabriel Belingueres" <gaby@ieee.org> wrote in message
news:1165422853.886152.179910@16g2000cwy.googlegroups.com...
(I don't believe that
overloading the equals method is a common idiom, _overriding_ IS a
common idiom)
Really? Whenever I override the equals method, it's pretty much always
overloaded as well:
public SomeClass {
final int field1;
final String field2;
/*Constructors and getters*/
@Override
public boolean equals(Object other) {
if (other instanceof SomeClass) {
return equals((SomeClass)other);
}
return false;
}
public boolean equals(SomeClass other) {
if (this.field1 != other.field1) {
return false;
}
if (!this.field2.equals(other.field2)) {
return false;
}
return true;
}
}
- Oliver