• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • 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


 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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


 
Bob Wheeler
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

Thanks, How do you observe that?
 
yen hoang
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • 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>()).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic