I have a baseclass, Statement
, which several other classes inherit from, named IfStatement
, WhereStatement
, etc... What is the best way t
This is not the way to do things in an object-oriented way, it's a throwback to the old code/data dichotomy. Now that's not necessarily a bad thing (if you know what you're doing) but it should be left to the non-object-oriented languages like C.
With proper design, you don't need that sort of behavior. Instead of the construct:
if (obj.getClass().isInstance(Statement.class)) {
doStuffWithStatements((Statement) obj));
}
(apologies to benjismith for 'stealing' his code), you should really be making the object itself responsible for its own activities thus:
obj.doStuff();
Then each different obj
class will have its own definition for doStuff
. That is the right way to do it.