This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubts about Generics

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

This code is from Niko's SCJP Mock Tests (Generics) Q#48


Question is will the above code compile ? yes/no
the answer given is No

However when I tried the following piece of code on my machine it worked perfectly well



The output was
parent
child
child

ie the method void say(List<T>) was working like a normal overridden method with runtime polymorphism....

so what gives? is there a typo on the website or what...

BTW I am using Netbeans 6.5 and haven't tried compiling it from the command line.. dont know it that could make a difference...

Also one other thing...
given a collection like List<? extends T> list where T is some concrete type -

Case 1: Retrieving an element from the collection
Here we are 100% sure that whatever is returned from the collection IS-A T(ie passes the instanceof test for T). Thus the only object reference that can be on the left hand side of the statement list.get(0) is of type T.Nothing else will do for the compiler irrespective of whether it is legal at runtime or not.

Case 2 - Adding an element to the collection
Not possible in this case since there is nothing equivalent to an ArrayStoreException for typed collections. Thus the compiler forbids any modification whatsoever.


However given a collection like List<? super T> list where T is some concrete type -
Case 1: Retrieving an element from the collection
Here we are 100% sure that whatever is returned from the collection IS-A Object(ie passes the instanceof test for Object). Thus the only object reference that can be on the left hand side of the statement list.get(0) is of type Object.Nothing else will do for the compiler irrespective of whether it is legal at runtime or not.

Case 2 - Adding an element to the collection
Here we can add elements of type T only because a collection typed with type X can accept subtypes of X(direct as well as indirect) as elements.

Also <?> is equivalent to <? extends Object> and hence follows the above rules for extends.
Of course null can be added to the collection in both cases.

Just want to know if my observation is right...
please clarify me if i have missed something or have a misconception..

Thanks in advance
 
Chaitanya Lele
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I tried the code on the command line and it failed to compile because of name clash..

i read the previous discussion on this very topic and the implications of type erasure etc and seem to have got it.

Just want to know if my summing up of rules regarding <? extends T> and <? super T> are correct.

Sorry for re-posting the same topic.

Thanks
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chaitanya the rules are simple.

<? super T>

elements can be retrieved into references of type Object
elements can be added of type T or sub-type of T

<? extends T>

elements can be retrieved into references of type T or super type of T
elements cannot be added
 
Chaitanya Lele
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ankit
Got it now
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic