My Question might be very simple,
I have a class Result with some inner fields , setters and getters.
Additionally, i have class Special1Result which extends Result and includes several more fields and Special2Result with some more data.
In different class Dispatcher, i have written the following method:
processResults(List<? extends Result> results), which is only familiar with Result (I need this method to query if there is specific field in the extended Result object – i am using annotations).
So i have decided to use the extended for-each loop: for (Result res : results) {}
So what is my question ? i am trying to find over the web how to write this for loop for extended objects, e.g. something like this for (? extends Results res: results){}
Is it possible? How is the correct way to write it?
No, this is not possible: you cannot statically type items supplied dynamically at run-time.
You are already doing it:
If you would like to test for
Special2Resultinside that loop, you can do it, but usually it tells that your design can be improved. A better alternative is to use a mechanism of double dispatch, such as the Visitor Pattern, to hide the details of special treatment for your subclasses.