I have the first print of the book, so maybe this is different in later ones. My doubt is about the AnimalDoctorGeneric class which is mostly on page 587.
Here is one the error messages from the book:
From page 588:
The compiler stops us with errors, not warnings. You simply CANNOT assign the individual ArrayLists of Animal subtypes (<Dog>, <Cat>, of <Bird> ) to an ArrayList of the supertype <Animal>, which is the declared type of the argument.
I believe, the compiler cannot assign a java.util.List to a java.util.ArrayList anyway, even if it would have the right type (<Animal> ) . The example should creat ArrayLists instead :
Or change the parameter of the checkAnimals() to List:
Is my book too old? Am I wrong? Thank you. And just want to use the opportunity to thank the authors for this AWESOME BOOK!
[ April 09, 2008: Message edited by: Irina Goble ] [ April 10, 2008: Message edited by: Irina Goble ]
Joined: Apr 01, 2008
You're right about the type of the parameter - it should either be List<Animal>, or the base type of the various Lists should be ArrayList. However, you are incorrect in believing that this would be sufficient. The declared types for the method invocation must match.
If you declare a method with List<Animal>, you must pass in a List<Animal> or a sub-type of List with a declared type of Animal, e.g. LinkedList<Animal>, ArrayList<Animal>... You are simply not allowed to pass in List<Dog> or List<Cat>.
List<Animal> is not the supertype of List<Dog>. List<Animal> is the supertype of ArrayList<Animal>, etc..
Hope this helps,
Dave [ April 09, 2008: Message edited by: David Grant ]
Joined: May 09, 2004
By the type I meant a type parameter (parammeterized type), <Animal> in this case.
Edit: David, the purpose of that example is to show exactly what you are saying, but even if the List<Dog> will be changed to List<Animal> it still will not compile the class. That's what I'm trying to point out. Thank you.
[ April 09, 2008: Message edited by: Irina Goble ] More edit: It would be more fair to get an error message like:
[ April 09, 2008: Message edited by: Irina Goble ] [ April 09, 2008: Message edited by: Irina Goble ]