This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP 6 question

 
samir vasani
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samir vasani wrote:
Can you programmaticaly show this statement "The type List<? extends B> is assignable to List<? extends A>."


 
Riza Aktunc
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't choice A be correct, too?



This compiles!
 
bhanu chowdary
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riza Aktunc wrote:


The above will generate a warning I suppose.

Welcome to the Ranch
 
Riza Aktunc
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Riza Aktunc
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After all, just to clarify:

Can't I say " The type List<A> is assignable to List."?
 
Tommy Delson
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic