File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic
Author

generics

susan waters
Ranch Hand

Joined: Feb 21, 2006
Posts: 68
WHat I understnad is

void addToGeneric(List<? extends Animal> ani){
// Here I can NOT add anyting to the generic collection since I
// use extends keyword
}

void addToGeneric(List<? super Animal> ani){

// Here I CAN ADD Animal or subtype of Animal (but not super type
// of Animal) to the generic collection right ?

}
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Yes both are correct.

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Im not so sure that may be the correct way to think it.
void addToGeneric(List<? extends Animal> ani){
// Here I can NOT add anyting to the generic collection since I
// use extends keyword
}


The word 'extends' as such is not a problem for adding but the word used with the wildcard '?' will not let you add.


void addToGeneric(List<? super Animal> ani){

// Here I CAN ADD Animal or subtype of Animal (but not super type
// of Animal) to the generic collection right ?
}


Here you can add animal or a SuperType of animal which is probably object assuming you havent derived Animal class from some other class. A subtype of animal like stated in the K&B p597 cant be added.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by Amitabha Batranab:
Here you can add animal or a SuperType of animal which is probably object assuming you havent derived Animal class from some other class. A subtype of animal like stated in the K&B p597 cant be added.


What you told is for assignment not for adding like this:

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

Naseem
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
What you told is for assignment not for adding like this:
List <? super Dog> anim=new ArrayList<Animal>();
Naseem


The above isnt an adding. Its a declaration. By adding I mean using the add() to actually add elements to the collection the legality of which was asked by the original poster.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Folowing code works fine.




Thanks & Regards


Naseem
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
How can the code above work? If this works then is the Book wrong? Can someone please clarify this? I have an older compiler so i cant try this, but does this result in a runtime exception or something?
Pad Ven
Ranch Hand

Joined: Apr 29, 2005
Posts: 38
Hi Amitabha and Naseem,
Note that in Naseem's code, he is creating a non-type safe list and passing it to the addToGeneric method. That's why it is working, as all type-safety check requirements are not needed anymore. Try giving Animal or Object as in "List <Animal> anim=new ArrayList<Animal>()" and it will compile and work, while using "List <Dog> anim=new ArrayList<Dog>()" to call the addToGeneric obviously results in compile error. So
<? extends type> means cannot ADD anything but can accept that type or its subtypes while <? super type means ADD anything.

Hope this helps. I just completed reading this chapter in K&B but unfortunately lost the plot. This question helped me get back to the chapter and understand the concept.

Regards,
Sathish
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
So basically if Icreate an untype safe list as in 'List anim=new ArrayList();' and pass it to a type safe list such as 'static void addToGeneric(List<? super Animal> anim)' I lose all type safetyness? But I thought the compiler would be checking to make sure I dont add the wrong thing to it like a Dog object?
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Okay I get it now, The type safety is only limited to method parameter, it can dicatet what comes in, but the list itself not being type safe can accept all animals.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
I meant dictate.
Is the above interpretation right though I still am not 100%?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics