Re: Why this overloading example works this way?
- From: "Gabriel Belingueres" <gaby@xxxxxxxx>
- Date: 6 Dec 2006 11:23:21 -0800
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@xxxxxxxx> wrote in message
news:1165422853.886152.179910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(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
.
- Follow-Ups:
- Re: Why this overloading example works this way?
- From: Oliver Wong
- Re: Why this overloading example works this way?
- References:
- Why this overloading example works this way?
- From: Gabriel Belingueres
- Re: Why this overloading example works this way?
- From: Chris Smith
- Re: Why this overloading example works this way?
- From: Gabriel Belingueres
- Re: Why this overloading example works this way?
- From: Lew
- Re: Why this overloading example works this way?
- From: Gabriel Belingueres
- Re: Why this overloading example works this way?
- From: Oliver Wong
- Why this overloading example works this way?
- Prev by Date: Re: j2me app
- Next by Date: Re: javamail code in servlet works locally, but not when uploaded to host
- Previous by thread: Re: Why this overloading example works this way?
- Next by thread: Re: Why this overloading example works this way?
- Index(es):
Relevant Pages
|