• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic argument method ? super

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
reply
    Bookmark Topic Watch Topic
  • New Topic