This is the sample piece of program i am working on :
At the Erroneous line i am getting the following error :
Type mismatch: cannot convert from element type capture#1-of ? super Dog to Animal
I changed the argument generic type from to ,so that while adding Dogs to the list the compiler wouldn't give me any compilation error but now when i am trying to iterate through the collection i am facing the specified problem !
Can anyone please explain this peculiar behavior ?
I think my first question to you is to ask why does your "generic" method not take in a List<Animal> as both the method name and variable name suggest.
Secondly to answer your question you cannot know that everything in the list List<? super Dog> is an Animal and as such you can't just convert it. For example it could be of type java.lang.Object or any other superclass that Animal may have.