wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic argument method ? super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic argument method ? super" Watch "Generic argument method ? super" New topic
Author

Generic argument method ? super

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Why can NOT be added a new Object into a List<? super Apple> if with this type is supposed to be able to add any supertype of Apple?

There was a mistake, it is Why can not be added...



If we change argument to List list, of course it works :s

Any good guide for wildcards, I read SCJP 6 and oracle documentation but I still have a lot of doubts
Source - Guideline for Wildcard use http://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Joey Sanchez wrote:Why can be added a new Object into a List<? super Apple> if with this type is supposed to be able to add any supertype of Apple?



Well, this is where your mistake is... The list isn't "supposed to be able to add any supertype of Apple". It is a list of a specific generic type, and the compiler does *not* know what the type is. It can be a List<Apple> or it can be a List<Object> -- the compiler doesn't know which. So, in order to add to this list, the instance being added has to be IS-A Object and IS-A Apple. This means that the compiler will allow you to add Apple, or any sub type of Apple.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 721
Hi, Joey.
That is a good question.
The compiler is designed in this way that you can only add Apple to a because :

But you can only add Apple to mybasket because what if the object is a Cat and a Cat is not in the hierachy chain of Apple.


Try to work around your Organic class. Change <? extends Organic> into <? super Organic> and see how it works.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86



Why can we use Mixer<Animal> in return type if in the case before at compiler time it didn't know about the Class of the object, now is because of is a class <Animal>?¿?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Joey Sanchez wrote:
Why can we use Mixer<Animal> in return type if in the case before at compiler time it didn't know about the Class of the object, now is because of is a class <Animal>?¿?


Well, simply, because it different. In the first case, you are trying to add an instance to a generic collection, where the compiler can't confirm the type. In the second case, you are assigning (returning) an generic object (with a known type) to a wildcard reference, or basically, the code is "forgetting" the type.

Henry
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 721
Hi, Joey,
When it comes to return type, think in this way:


I am sure if you do this:


Joey Sanchez wrote:

Why can we use Mixer<Animal> in return type if in the case before at compiler time it didn't know about the Class of the object, now is because of is a class <Animal>?¿?
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

To sum up a little...
Case1

In the first case, you are trying to add an instance to a generic collection, where the compiler can't confirm the type.
It is a list of a specific generic type, and the compiler does *not* know what the type is. It can be a List<Apple> or it can be a List<Object> -- the compiler doesn't know which. So, in order to add to this list, the instance being added has to be IS-A Object and IS-A Apple. This means that the compiler will allow you to add Apple, or any sub type of Apple.


So we can add Apple or subtype of Apple because autoboxing upcast (implicit) works to become an Apple type at compiler time.
The argument type (List<? super Apple) is not allowed to add other type different from Apple or subtype of Apple. At compile time, we don't know what are we going to add, the instantiation generic type is known at runtime, that's why to keep list type safe we can't add any supertype of Apple. (rewritten)

Henry
Generics actually does not exist at runtime -- the type information does not exist in the bytecode. So, all processing (ie. type checking of generics) is done at compile time.


Case2

In the second case, you are assigning (returning) an generic object (with a known type) to a wildcard reference, or basically, the code is "forgetting" the type.

In this case, we are limited to class generic and return type, that they are<A extends Animal> and Mixer<? super Dog>. So we know that we can return any type that it is Mixer<Dog> or Mixer<Animal> and we can not use Mixer<Object>.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Joey Sanchez wrote:
So we can add Apple or subtype of Apple because autoboxing work to become an Apple type at compiler time.


Autoboxing is the feature in Java to implicitly wrap primative types to there corresponding wrapper class types. The Apple class is neither a primative nor a wrapper class.


Joey Sanchez wrote:
At runtime, we will know the type of List<Object> is a List<? super Apple>, but because at compiler time is not known, it is not allowed to add other type different from Apple or subtype of Apple. This way is guaranteed the type safe of the list.


Generics actually does not exist at runtime -- the type information does not exist in the bytecode. So, all processing (ie. type checking of generics) is done at compile time. Anyway, not sure what you are trying to say here -- but my response was referring to compile time only.

Henry

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic argument method ? super
 
Similar Threads
Generics <?>
Generic doubt
Generics problem......
Generics question from bonus exam
one more doubt abt generics