This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 6 question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP 6 question" Watch "SCJP 6 question" New topic
Author

SCJP 6 question

samir vasani
Ranch Hand

Joined: Nov 24, 2010
Posts: 60
Dear all,

I am not getting the answer of the following question .


Please explain me how this answer comes.
Thank in advance.
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
C is obvious as you can assign anything to a List<?> generic type
samir vasani wrote:

Anything which extends B can be assignable to List<? extends B>. Since class D is extending class B, List <D> can be assigned to List<? extends B>

Here you can assign any list that extends B to List<? extends A> as class B is extending class A.

If you feel the answer should be something else, please put your points here as to why you think, so that we can discuss.


samir vasani
Ranch Hand

Joined: Nov 24, 2010
Posts: 60
bhanu chowdary wrote:C is obvious as you can assign anything to a List<?> generic type
samir vasani wrote:

Anything which extends B can be assignable to List<? extends B>. Since class D is extending class B, List <D> can be assigned to List<? extends B>

Here you can assign any list that extends B to List<? extends A> as class B is extending class A.

If you feel the answer should be something else, please put your points here as to why you think, so that we can discuss.



Can you programmaticaly show this statement "The type List<? extends B> is assignable to List<? extends A>."
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
samir vasani wrote:
Can you programmaticaly show this statement "The type List<? extends B> is assignable to List<? extends A>."


Riza Aktunc
Greenhorn

Joined: Sep 07, 2011
Posts: 24
Shouldn't choice A be correct, too?



This compiles!


OCPJP - 93%
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Riza Aktunc wrote:


The above will generate a warning I suppose.

Welcome to the Ranch
Riza Aktunc
Greenhorn

Joined: Sep 07, 2011
Posts: 24
Thanks
I had assumed that just a warning would not mean "The type List<A> is not assignable to List. ". Am I wrong with this assumption?
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Riza Aktunc wrote:I had assumed that just a warning would not mean "The type List<A> is not assignable to List. ". Am I wrong with this assumption?

No not at all. But if I were asked to choose 3 options I would not choose option A.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Riza Aktunc wrote:Shouldn't choice A be correct, too?


This compiles!


Let's try this...



If you still have doubt or not clear reread K & B book on Chapter 7, page 638 on "Generics and Legacy Code" section for
more insight.

This sort of generic stuffs will likely on the exam...

OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Riza Aktunc
Greenhorn

Joined: Sep 07, 2011
Posts: 24
Yes, I got this perfectly. I was aware that if the referance type is not generic, type casting is required during the access of the list element.
However, I was trying to state that this type casting need is not enough to say "The type List<A> is not assignable to List. ".
In fact, you stated that "Anything can assign to "list" since List is not Generic", too. Am I missing a point here?

Thanks anyway for the mind refreshing information about generics
Riza Aktunc
Greenhorn

Joined: Sep 07, 2011
Posts: 24
After all, just to clarify:

Can't I say " The type List<A> is assignable to List."?
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Riza Aktunc wrote:After all, just to clarify:

Can't I say " The type List<A> is assignable to List."?


The question is so obvious "Which three statements are true?" that meant which is true based on the given scenario in this case type List<A> is assignable to List, but not true because of List is not generic and needed a cast to retrieve an object.

In programming the type List<A> is assignable to List, but it doesn't work 100% unless you cast it to specific type. The question asking which is true meaning that 100% working so, the given answer "The type List<A> is assignable to List." is not applied here.

You need to pay very close attention to this type of question otherwise, you won't get answer correctly.

Hope that clear your confusion...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP 6 question