>>You can avoid it by including an abstract method in the superclass that
>>matches each method in the subclass. Then through polymorphism the
[quoted text clipped - 4 lines]
> (ClassA) is in a library (say java.lang) and so cannot be modified and
> that SubClassA is not to be exposed. (i.e. private class)
This seems like a typical problem encountered hen inheritance is used
instead of delegation.
What would your design look like if you encapsulated the java.lang base
class, rather than derived from it?
For certain, the instanceof check would disappear.
Andrew
WillieLWZ - 23 Feb 2006 06:56 GMT
Do you mean "Favor object composition over class inheritance"?
If so, I'm restricted by two criteria however:
1. Main#getClassA() is used by code to invoke ClassA#someMethod(objB)
2. ClassA does not implement an interface.
I'm stumped and would be thankful for an example.
>From Effective C++, by Scott Meyers :
"Anytime you find yourself writing code of the form "if the object is
of type T1, then do something, but if it's of type T2, then do
something else," slap yourself. "