• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Doubt in K&B SCJP 5: topic Generics

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are raising an interesting point. I'll check if it has been corrected or not.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic