The Artima Developer Community
Sponsored Link

Legacy Design Forum
Canonical Object Idiom

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Errors in both

Posted by Dave Roberts on February 21, 2000 at 5:04 PM


> I think the class of both objects must be the same, which
> none of these approaches are checking for. I'll post again
> with a way to do that. And, I'll change the idiom to match.
> Any ideas yourself?

Exactly. You need to check if the class names of both classes are the same. The class name is specific to the actual object, irrespective of subtyping. Any type of casting (whether done with explicit casting or testing with instanceof) allows subtypes to get "through the screen" if you will. For instance, the following will get through:

class A {
public boolean equals(Object o) {
... as written by either approach in this thread...
}
}

class B extends A {
...methods...
}

int main(...) {
A a = new A();
B b = new B();

if (a.equals(b)) {
// the original implementations will get here
// if b's fields derived from A are equal to a's fields
// since (b instanceof a) is true.
}
}

-- Dave



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us