Lets say we have a baseclass called A and some subclasses (B,C,D, etc.). Most subclasses have the method do() but the baseclass does not.
Class AA provides a method called getObject(), which will create an object of type B, or C or D, etc., but returns the object as type A.
How do I cast the returned object to the concrete type and call its do() method, if this method is available?
EDIT:
I’m not allowed to change the implementation of Class A, the subclasses or AA, since im using a closed Source API.. And yeah, it does have some design issues, as you can see.
I think a better idea is to actually have class
Adefine thedo()method either as an abstract method or as a concrete empty method. This way you won’t have to do any cast.If you are not allowed to change any of the classes than you could define a class
MyA extends Awhich defines thedo()method andMyB,MyC,… and aMyAAthat would basically do whatAAdoes, just that it returns objects of typeMyB,MyC….If this is not ok then I don’t see another way than checking if the returned object is of type
Band do a cast toBand so on.