jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: topic Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B SCJP 5: topic Generics" Watch "Doubt in K&B SCJP 5: topic Generics" New topic
Author

Doubt in K&B SCJP 5: topic Generics

Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
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 ]
David Grant
Greenhorn

Joined: Apr 01, 2008
Posts: 13
Hi,

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 ]
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
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 ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You are raising an interesting point. I'll check if it has been corrected or not.


[My Blog]
All roads lead to JavaRanch
 
jQuery in Action, 2nd edition
 
subject: Doubt in K&B SCJP 5: topic Generics