If you know that they are of a subclass type, then just produce them directly without checking instanceof .
But placing them in an array filled with a superclass tells the compiler to abandon the information that they really are of a subclass type. Either your superclass should expose these methods (possibly as abstract ones), or your array must be a subclass type (so you don’t tell the compiler to forget about the actual type of objects), or you have to suck it in and do the casting (possibly with a test instanceof ).
The only other notable alternative is that you can experiment with a visitor template that passes the action to the object and allows the object to decide what to do with it. This allows you to override classes to ignore or perform actions depending on their type of execution.
source share