Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Can't I Add To This Generic?

 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20992
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic