aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The use of ? extends 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 "The use of ? extends" Watch "The use of ? extends" New topic
Author

The use of ? extends

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
From http://blogs.infosupport.com/martinw/articles/generics.aspx



I don't quite get it. What's the use of ? extends Animal if you can't make anything useful with it(based on the above example)?. Could you please explain this further? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You cannot add anything to the Collection, but you can iterate through it.

Using ? super does allow some things to be added.
cathy liu
Greenhorn

Joined: Sep 07, 2006
Posts: 9
You cannot add anything to the Collection, but you can iterate through it.
==> Yes I agree with this.
Using ? super does allow some things to be added.
==> Not sure what do yu mean by this? Could you please explain it more? I am really confused with the "?". I thought when the ? is used in the type, nothing could be added at all, because the type is uncertain.

Thanks for your help in advance.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hmmm... So would this work?

ArrayList<? extends Animal> animalList;

ArrayList<Dog> dogList = new ArrayList<Dog>();

dogList.add( new Dog() );

animalList = dogList;


and then, as you said... I can iterate through a List that has ? extends. Is this right?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Yes, you can do that, but you won't be able to add to animalList.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The use of ? extends