Greetings everyone
I keep reading same pages over and over, yet I do not understand a small detail which is a little confuse.
Given this scenario :
And based in these words:
In other words, a method that takes, say, an ArrayList<Animal> will NOT be
able to accept a collection of any Animal subtype! That means ArrayList<Dog>
cannot be passed into a method with an argument of ArrayList<Animal>, even
though we already know that this works just fine with plain old arrays.
Obviously this difference between arrays and ArrayList is consistent with the
polymorphism assignment rules we already looked at—the fact that you cannot
assign an object of type ArrayList<JButton> to a List<Object>. But this is where
you really start to feel the pain of the distinction between typed arrays and typed
collections.
Fine , the declared type argument is Animal, for addAnimal method , not any other, so there's no point passing subtypes .
Then, when it comes to add method (List interface), why this is fine "some times" :
Is it really fine? Isn't suppose JVM it's expecting just a List of Animal class type only?
Wouldn't be better simply to change previous code into : (using wildcard )
The list is supposed to hold Animal type objects, right? So, in this case if you try to add a Dog type object (subtype of Animal class) , shouldn't be wrong? What am I missing?
Thanks in advance ^_^