File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Method problem

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • 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..
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read sun tutorial or Generic FAQ...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happens when the exams gets near
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Devaka, It was really very helpful link for me..Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic