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@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

.



Relevant Pages

  • Re: Why this overloading example works this way?
    ... Whenever I override the equals method, ... if (other instanceof SomeClass) { ... public boolean equals{ ...
    (comp.lang.java.programmer)
  • RE: Equality vs Sameness
    ... Good call on my misuse of overriding vs overloading. ... reference types that act like value types. ... I am the same way about Equals. ... you have to override Equals as well. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Comparing Object with null
    ... > public boolean equalsFoo ... hashCodeand equalswork in tandem, so if you override one, so should ... equals() should check any state used ... in computing the hashCode(). ...
    (comp.lang.java.programmer)
  • Re: problem with method overloading
    ... common mistake to overload 'equals' when the intent is to override, ... public boolean equals(Foo other) ... Does it matter which implementation of equals is ...
    (comp.lang.java.programmer)
  • RE: Equality vs Sameness
    ... Equals would not offer any benefit. ... >> from it in the future if the users haven't asked for that functionality. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)