File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on  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 "Question on  Generics" Watch "Question on  Generics" New topic

Question on Generics

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello All,

I have a question on Generics from K&B book page 596.

for the first one, we say List of Anytype can point to Dog arraylist. But is this not a potential problem creator? Does this not mean that Integer to refer Dog?

for the fifth one, we say any class Dog or any superclass of it can refer Animal.How is this possible? How can a Dog refer Animal?

for the sixth one, we say Animal or any superclass Animal to refer Dog. This makes sense as Animal refers Dog and any super class of animal too refers Dog.But in the book, it is not an option.

Please explain me on this.
Abhishek Kanchan
Ranch Hand

Joined: May 10, 2007
Posts: 35
Hi Ravi,
That is safe because as 1) will not let you add anything to list,not even a Dog.In 5) also you can add only Dog and nothing else and Dog IS-A Animal in this example.

SCJP 1.5<br />SCWCD 5.0
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

1) List<?> list=new ArrayList<Dog>();

List<?> is read as �List of unknown�. It can refer to any kind of list. It is semantically equivalent to List<? extends Object> (i.e. any kind of list)

5) List<? super Dog>blist=new ArrayList<Animal>();

List<? super Dog> is read as �List of any type that is Dog or a superclass of Dog�. It can refer to any of these: List<Dog>, List<Animal> and List<Object>.

6) List<? super Animal>dlist=new ArrayList<Dog>();

List<? super Animal> is read as �List of any type that is Animal or a superclass of Animal�. It can refer to any of these: List<Animal> and List<Object>.

This is why it cannot refer to a list of Dog (List<Dog> .

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
I agree. Here's the link:
subject: Question on Generics
It's not a secret anymore!