Marco Schmidt wrote:
> If you use instanceof very often there is a good chance you could
> improve the design of your program. Is there any typical situation
> where you would check objects with instanceof?
Well, I guess I can risk a little humiliation to gain a little
education, so here goes:
I have a "primary" Class, with two branches of sub-classes:
MyClassBase
MyClassBaseSquare MyClassBaseNSquare
MyClassBaseSquareX MyClassBaseNSquareA
They are all related objects (um, I suppose you'd guess that from the
superclass) and I have various reasons for loading up Vectors with lots
of MyClass objects. But NSquare objects have different methods that need
to be run.
A specific example is that I have a Vector holding lots of MyClassBase.
I want to create a descriptive list of only the Square, none of the
NSquare. Plus, I need to run a method on all the NSquares, that does not
exist on the Squares.
So, when I get there I have been using a lot of
getClass().getName().indexOf("NSquare)<0 to figure out if I can cast.
I think I might have been better off if I'd done a better job of
designing the classes before I started, but I figured out too much of
what was going to happen after I got going.
>
> Regards,
> Marco
--
Chris Rehm
Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]