aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55

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

Joined: Feb 24, 2006
Posts: 6661
    
    5

Please quote the source of this question ?

? extends B cannot be assigned to ? extends A as you pointed out.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55
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
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


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55
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
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Sep 27, 2008
Posts: 55
Hi Bob,

Thanks, How do you observe that?
yen hoang
Ranch Hand

Joined: Apr 05, 2009
Posts: 58
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: 155
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>()).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics