• 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

Generics

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is below statement will be true?
The type List<? extends B> is assignable to List<? extends A>.

The answer is given it is allowed. I didn't get how it will allow to compile?
I tried in the follwoing way


 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please quote the source of this question ?

? extends B cannot be assigned to ? extends A as you pointed out.
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkata Saraswathi wrote:
Is below statement will be true?


The type List<? extends B> is assignable to List<? extends A>.


The answer is given it is allowed. I didn't get how it will allow to compile?
I tried in the follwoing way


 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:
? extends B cannot be assigned to ? extends A as you pointed out.


I think a type of "? extends B" can be assigned to "? extends A".
First of all, what is a type of "? extends B"? Actually it is just B
and B can be assigned to ? extends B. Just check Venkata code example.
But beware, he mixed the b1 with bl. So here the corrected code:


cheers
Bob
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:"? extends B" can be assigned to "? extends A".


I didn't get your explanation and that wrong you mentioned in given code example.

Can you give a sample program whcih will compile successfully and clarify above statement?

Thanks,
--Venkata.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkata Saraswathi wrote:

Bob Wheeler wrote:"? extends B" can be assigned to "? extends A".


I didn't get your explanation and that wrong you mentioned in given code example.

Can you give a sample program whcih will compile successfully and clarify above statement?

Thanks,
--Venkata.


In your code you made a typo. You wrote b1 (b followed by "one") and also bl (be followed by "l"). I changed that in my code
and that code compiled successfully.

Edit: small explanation in code:


cheers
Bob
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:

Deepak Bala wrote:
? extends B cannot be assigned to ? extends A as you pointed out.


I think a type of "? extends B" can be assigned to "? extends A".
First of all, what is a type of "? extends B"? Actually it is just B
and B can be assigned to ? extends B. Just check Venkata code example.
But beware, he mixed the b1 with bl. So here the corrected code:


cheers
Bob



I meant it the other way round. Thanks for correcting
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,

Thanks, How do you observe that?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried the following code

Please explain for me why? Why can I assign list of B to list of C when both B and C extends A
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do that because both B and C extends A. The type of 'a' is List<? extends A>, that's why it works. You're saying there "forget the instantiated object (LinkedList<C>()) and point to the other one (new LinkedList<B>()).
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic