I have to apologize - there was an error somewhere else. (Nevertheless,
generics still confuse me).
But thank you anyway - I didn't realize that I could complare classes
the way you suggested.
Uli
>> @Override
>> boolean equals(Object other)
>> {
>> if(!(other instanceof A))
>> {
>> // error
It's not an error.
>> }
>>
[quoted text clipped - 5 lines]
>
> I do not see what this has to do with generics, but how about:
Other than it doesn't work on generic types. It compares the objects
erasure, so you cannot distinguish, say, ArrayList<String> from
ArrayList<JComponent>.
> if(getClass()!=other.getClass())
In the case of equals, that should read:
if (other == null || this.getClass() != other.getClass()) {
return false;
}
(Or split into separate if statements, if you value readability.)
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Oliver Wong - 08 Dec 2005 19:00 GMT
>> if(getClass()!=other.getClass())
>
[quoted text clipped - 3 lines]
> return false;
> }
It's not a hard requirement that classes of two different objects be
non-equal. For example I usually code my equals() method so that
occasionally an object of type ImmutableFoo can be found to be equal to an
object of type MutableFoo.
You just gotta make sure the contract for the equals() method is
satisfied (reflexive, symmetric, transitive, consistent, non-null). See
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#equals(java.lang.Object)
If you're overriding equals(), you'll probably have to also override
hashCode(), and generally be careful around Comparator and/or Comparable.
- Oliver
Thomas Hawtin - 08 Dec 2005 20:28 GMT
> It's not a hard requirement that classes of two different objects be
> non-equal. For example I usually code my equals() method so that
> occasionally an object of type ImmutableFoo can be found to be equal to an
> object of type MutableFoo.
You are shooting off at a tangent. The context of the thread is
distinguishing between a class and its subclasses.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/