wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 5 Study Guide page 634 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP 5 Study Guide page 634" Watch "SCJP 5 Study Guide page 634" New topic
Author

SCJP 5 Study Guide page 634

Anderson Bush
Greenhorn

Joined: Oct 29, 2002
Posts: 5
This is a question about generics. The answer to question 16 says that two of the options can be dismissed because the method signature states that the return type is

List<? super E>

My confusion comes from the fact that, when using generics, base types can be treated polymorphically, but the generic types cannot. For example, we can declare

List<Animal> = new ArrayList<Animal>()

Can you please explain why we can't return an ArrayList<Integer> when List<Integer> is the declared return type?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

Can you please explain why we can't return an ArrayList<Integer> when List<Integer> is the declared return type?


You can return an ArrayList<Integer> when List<Integer> is the declared return type.

The question 16 (page 634) asked you to define the variable type for input/output value. But not the return object at "return" statement.
[ May 12, 2006: Message edited by: wise owen ]
Anderson Bush
Greenhorn

Joined: Oct 29, 2002
Posts: 5
Is there a typo in that reply? Did you mean to say "can't"? If not, I need more explanation. In a non-type-safe method, I can declare a return type of List then return a subtype of List. The answer to the self test question seems to imply that when using generics, if you declare that you return a base type of List, then you are restricting yourself to a List return exclusively. Is this correct? It seems maybe I am missing some fine point here...

Thank you.
Anderson Bush
Greenhorn

Joined: Oct 29, 2002
Posts: 5
I've got more confusion on the same question. Let me detail the process I used to solve it, and perhaps you can see an error in my reasoning.

Let's consider option (C):
1. Method signature: public static <E extends Number> List<? super E>
process(List<E> nums)
2. ArrayList<Integer> input = null
3. The above is going to map to List<E> nums from the signature which makes E evaluate to Integer
4. Check: does E as Integer satisfy <E extends Number> ? Yes.
5. Now the only thing left to check is to make sure that the output is compatible with List<? super Integer>
6. List<Number> output = null. Does List<Number> satisfy List<? super Integer> ? Yes.
7. Conclude based on above reasoning that (C) is a valid option.

==> Please point out the flaw(s) in my reasoning.

Thank you.
Alisson Nunes
Greenhorn

Joined: May 15, 2006
Posts: 1
If you see the K&B errata (http://www.coderanch.com/t/253802/java-programmer-SCJP/certification/Updated-Errata), you will see that the question should be

620....bug.......Q-16: method declaration s/b:
& 634

public static <E extends Number> List<E> process(List<E> nums)


Anyway when you have something looking strange you can always write code compile and run it to let the compiler and JVM tell you what happened

The way the question was written there would not be any right answer. Because if E is an Integer than output should be of type List<? super Integer> or List<?> or just List
Anderson Bush
Greenhorn

Joined: Oct 29, 2002
Posts: 5
Thanks Allison - do you have any insight into my first question and Owen's response?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP 5 Study Guide page 634
 
Similar Threads
Generics
generics
generics
Generics
Generics