This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic


Anit Nair

Joined: May 09, 2008
Posts: 12
Following is the piece of code from K&B 1.5 pg 587

Besideds above code theres a parent class Animal and Dog class extends Animal.
Now compiling above code gives error

The explanation given for error states that ArrayList of subtype cannot be assigned to ArrayList of supertype, rightly so. But I feel that even if the
checkAll method was modified as below

It would still give compilation error just because we cannot pass ArrayList reference to a List reference regardless of the generic type used. Please correct me if i am wrong.
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Did you try to compile it ?

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Anit Nair

Joined: May 09, 2008
Posts: 12
yes i did, try out a similar example.I think regardless of the generic type passing a List reference to Arraylist reference would give compilation errors. Let me know if you any other thoughts on this.
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
There is an explanation following the example, of why it throws errors in the K&B book.

After reading it, you will find

public void checkAnimals(ArrayList<Animal> animals)

should be used as

public void checkAnimals(ArrayList<? extends Animal> animals)
It is sorta covered in the JavaRanch Style Guide.
subject: Generics
jQuery in Action, 3rd edition