This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Regarding Generics wildcards

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the below code gives me the compiler error, I think, that it should be acceptable, as assigning the right datatype (q2) into q1.





This is the compiler error, I am getting:

Type mismatch: cannot convert from Queue<capture#2-of ? super Integer> to Queue<? extends Number>
Can anybody please suggest me on this.
 
Sheriff
Posts: 22662
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Queue<Object> is a Queue<? super Integer> but not a Queue<? extends Number>.
A Queue<BigDecimal> is a Queue<? extends Number> but not a Queue<? super Integer>.
 
john kerl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob.
 
john kerl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see the same in the below scenario also:

Queue<? super Number> q1 = null;
Queue<? extends Integer> q2 = null;
q1 = q2;

The compiler error should not happen in this time because, q2 is anyway the subclass of q1. can anybody please clarfiy me on this.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say q2 is a Queue<Integer> (which matches Queue<? extends Integer>)

But Integer does not match <? super Number>. So why would you expect to be able to assign it to a Queue<? super Number>?
 
john kerl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding:

(1) In java, you can assign any subtype to its supertype variable place holder. (For Example : Object o = new String();)
(2) So, in the below lines of code,
(a) Queue<? super Number> q1 // It can be a Number or an Object
(b) Queue<? extends Integer> q2 // It can be a Integer or anything below the Integer hierarchy. ***** Integer is a subclass of Number*****.

So, I assume that since Number is a parent class of Integer, the Number class can hold its child class (Integer) or anything below the hierarchy.

So, i am not clear , what I have missed, ....!


 
john kerl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it...!. It's my mistake. Thankyou all.!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic