aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Method problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Method problem" Watch "Generic Method problem" New topic
Author

Generic Method problem

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72



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

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

You can read sun tutorial or Generic FAQ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

@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.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks, a lot.
I knew that, I just simply had temporary amnesia
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Happens when the exams gets near
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Apr 20, 2009
Posts: 804
    
    1

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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

http://faq.javaranch.com/java/GenericsSuperAndExtends


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Thanks Devaka, It was really very helpful link for me..Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Method problem
 
Similar Threads
Generics
Help me understand
Generics
Doubt
Solution needed for Generics Question