The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in generics.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help in generics...." Watch "Help in generics...." New topic

Help in generics....

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

This question is from SCJP 6 khalid mughal. The answer to this question is b and c.

well i understand that car can be added but how is sedan put because it says car or super of car(Vehicle or Object).....

Also please explain the other <? extends Car> also.

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

For Garage<? super Car> we can pass either Car or any subclass of the Car to the add() method, but we cannot pass a super class of Car. Also see this

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I guess i confused myself..... This example is like Garage<? super Car> g = new Garage<Car/SUpertype>();

I confused it to to the example given in K&B page 619 where they pass List<Animal> a. reference "a" is passed to a method(List<? super Dog> e) But still a little confusion..... why does it allow as we pass a list<Animal> to a method(list<? super Dog>.)

with raw type object references its not possible as the compiler has no idea which instance it points to or which super type it is going to point to.

I guess i understand a little bit over the problem here..... It is because the V in garage cannot be determined at compile time where as in animal and dog it was determined at compile time that the super of dog is animal.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I really confused myself big time...but i am clear now. When we pass a List<animal> a to a method(List<? super Dog> e)..here we are dealing with references and the compiler knows it. Well if we are talking about List<? super Dog> a = something. compiler really doesn't know what the something object will be that's why we cannot add anything into the list.
I agree. Here's the link: http://aspose.com/file-tools
subject: Help in generics....
Similar Threads
instanceof Comparison
Inheritance, need help
Generics questions about ? extends Type