• 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

Question on <? super wildcard

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont quite understand the super keyword in the wildcard bounds, when using <? super ClassA> I presumed it could be any superclass in the hierarchy above ClassA, but from what I can tell in the code snippet here, it only allows the immediate superclass Z, or the class itself A. But anything above Z in the chain, will result in a compiler error.

So is this always the case, and when I read about the super wildcard and it says, and I quote "There is also dual bound, called super. As you guess it is used to denote that you can pass only superclasses of the bound."

Can anyone shed some light on this subject?

 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is easier to understand if you start with the caller. I can call your method with ArrayList<Z> or ArrayList<Y>. In other words a list with Z as the generic or a superclass thereof. Make sense so far? (It should as this goes with "super" logically.)

Now, we have a method that can get any of these lists as a parameter. What can be added to that list. Z is clearly ok. So is A because it is a subclass of Z (or Y). The third line is a problem. We can't add Y to the list because it could be ArrayList<Z>.

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the sake of completeness, the code with Jeanne's addition -



The compiler says -



Regards,
Dan
 
Sheriff
Posts: 7137
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this FAQ too.
 
Ranch Hand
Posts: 35
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animal
^
|
|
Dog
^
|
|
Fido

When you are using wildcard List<? super Dog> it means you can pass any type that is Dog or superclass of Dog and we can add anything that is of type Dog or any subclass of Dog, this happens because as we know in polymorphism a parent type reference can hold its subclass object type.For example
(1).addAnimal(new ArrayList<Dog>());//method call or
(2).addAnimal(new ArrayList<Animal>());//method call
(3).public void addAnimal(List<? super Dog> animals) { }//called method
In the above calls the called method guirantees that adding anything that is Dog or subclass of Dog is acceptable weather you pass it (1) or (2) to (3).
 
reply
    Bookmark Topic Watch Topic
  • New Topic