I have a question about technique and implementation, rather than an actual problem to solve. Recently I created an abstract class, let's called it A, which defines the common behaviors of its subclasses.
I use this to construct several subclasses B, C, and D, which are then passed to some other method outside of the superclass-subclass structure which takes in a type A (thus being able to handle all of B, C and D).
In Java, I use instanceof to retrieve the true type, however I end up having to do a lot of casting to the real type, and it looks like a mess. Does anyone have any suggestions as to how to make the code cleaner?
One method I tried was by reconstructing an instance of the subclass type from the parameters of the superclass, so that I would be handling the object with it's actual type, thus avoiding typecasting. Since each of these subclasses are singletons (yes, they maintain state), I feel okay doing so. Does this seem like a crummy thing to do?
Thanks to anyone who answers, it is greatly appreciated!
Edit: I added further clarification in italics. Thanks.