Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

SCJP 6 question

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>."



 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't choice A be correct, too?



This compiles!
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

reply
    Bookmark Topic Watch Topic
  • New Topic