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 Why Can't I Add To This Generic? 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 "Why Can Watch "Why Can New topic
Author

Why Can't I Add To This Generic?

Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
The book says you can't add it in the line below. Why? Where can you add new object then?

Thanks for helping me out.



SCJP with 98% by...
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Erap Estrada wrote:


You are declaring the Arraylist as the list of mugs. But when



? extends container mean any thing who extends a container. The compiler throws an error if you use the add method because In a list of Mugs you may accidentally add say a Cup which is also extending Container.
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
So you're saying that you can add an object to that original list object creation but not within this method?

What type or subtypes can you add inside this method?

You can read only?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Erap Estrada wrote:So you're saying that you can add an object to that original list object creation but not within this method?

What type or subtypes can you add inside this method?

You can read only?



Wildcards give you the ability to handle many types -- in this case, any type that extends container. But it comes with a price, since your method must work with any type, and you don't know the generic type (except to say that it extends container). How can you add anything? You can only add items that satisfies all possible types supported by your method, and that is only null.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Generics are there to provide type safety. Means if you are having a list of cats nobody should be able to add dogs to it. Cat means cats only no dogs should be allowed in tha list.
So if you are having a list <? extends animal> it means anythingn that is extending animal means dog or cat. If you are allowed to add anything to this list then you can add dog or cat ot it which violates the purpose of type safety.

but still you can use this object to refer any list which is subclass of animal.

Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
Henry Wong wrote:
But it comes with a price, since your method must work with any type, and you don't know the generic type (except to say that it extends container). How can you add anything? You can only add items that satisfies all possible types supported by your method, and that is only null.

Henry



But changing the above line to this compiles fine:



It's still many possibilities right?

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Erap Estrada wrote:


It's still many possibilities right?



Right.
But any possibility would be a Mug or a parent of Mug (here Container). A Mug IS-A Mug, and also a MUG IS-A Container. So you will not be violating any rules of generics when you add it to the list of Mugs.

- Nidhi


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Can't I Add To This Generic?