• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Method problem

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



a) public static <T> List<T> backwards(List<T> input)
b) public static <T> List<T> backwards(List<? extends T> input)
c) public static <T> List<T> backwards(List<? super T> input)
d) public static <T> List<? extends T> backwards(List<T> input)
e) public static <T> List<? super T> backwards(List<T> input)
f) public static <? extends T> List<T> backwards(List<T> input)

Out of the above option i am having problem with the "e" which answer say is correct?
Can somebody please help me out to make me understand how this is correct?

Also please suggest where can i get the detail of generic method and other generic related topic as i am not very clear with K&B book in this chapter..
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just take an example and try to replace generic terms:



and the code



means in short just extract out return type from signature and return value's type from method you will get.


as <? super Cat> will be <Animal>,<Object> all the type that is up in hierarchy to Cat.
same way
<? super T> will be <Object> and <Super types> of T.
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting question.

List<? super Integer> list = new ArrayList<Number>();

for(Integer n : list); //error

for(Number n: list); // error

So the only type we can use is Object:
for(Object n: list);

But on the other hand it is not logical for me.
Why can't we use Integer for iterating? The only objects that can be added must be of type or subtype of Integer so upcasting to Integer would be always a success, co what's wrong?
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all..But I need a place where i can get more detail of such term of Generic..

It would really help me to understand the concept...
 
Sheriff
Posts: 9707
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
You can read sun tutorial or Generic FAQ...
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But on the other hand it is not logical for me.
Why can't we use Integer for iterating? The only objects that can be added must be of type or subtype of Integer so upcasting to Integer would be always a success, co what's wrong?



Well at run time the object formed can be of the type Object/Number/Integer(anyone) which the compiler doesn't know. So say if it is Object then whatever comes from that list will always be an object. So If you try to iterate this list using Integer or Number then if it were to compile, then the process would choke. So best solution to not compile only. But why it allows Object because there is nothing that can be super of Object. So what ever comes out can always be casted to Object.

But I need a place where i can get more detail of such term of Generic..

It would really help me to understand the concept...



What is that you don't understand? I think if you understand option d then option e should be pretty much clear. option f is not even correct as ? is used
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Lukas see the following code. I made following code for explaination purpose.



@Rajesh you can also refer Sun' tutorial in Generics & JavaGenericsFAQ by Angelika Langer as links are already posted by Ankit in this thread.

Correct me if I am wrong.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, a lot.
I knew that, I just simply had temporary amnesia
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happens when the exams gets near
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ninad,

Your code is really very helpful but in one place i got stuck
List<? super Integer> list3 = list1;
Does (? super Integer) will take super of Integer only or can take ( Integer and super of Integer)
as in your code its taking Integer?
Please correct if i am wrong anywhere?
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<? super Integer> list
It is the reference to List with generic type of Integer and everytning above Integer.
BUT, this list can hold Integers and everything that is below Integer.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/GenericsSuperAndExtends
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Devaka, It was really very helpful link for me..Thanks again
reply
    Bookmark Topic Watch Topic
  • New Topic