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
Joined: Apr 24, 2009
Posts: 317
posted
0
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:
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
Joined: Apr 24, 2009
Posts: 317
posted
0
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.
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
Joined: Sep 27, 2008
Posts: 55
posted
0
Hi Bob,
Thanks, How do you observe that?
yen hoang
Ranch Hand
Joined: Apr 05, 2009
Posts: 58
posted
0
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
I love Mozart (^o^)
Eduardo Bueno
Ranch Hand
Joined: Jun 04, 2009
Posts: 154
posted
0
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>()).