About Question enthuware.ocpjp.v7.2.1445 :
Posted: Mon Sep 23, 2013 1:52 pm
"The important concept here once the method returns, there is no way to know what is the exact class of objects stored in the returned List. So you cannot declare out in a way that ties it to any particular class, not even Object. Thus, the only way to accomplish this is to use non-typed reference type, such as: List result;"
Hi
Sorry, I don't get this, and it's come up in another question I think.
When you define a method like this:
"public static <E extends CharSequence> List<? super E> doIt(List<E> nums)"
you're saying, the method will return a List of objects whose type will be "E extends CharSequence" or a superclass of "E extends CharSequence". Correct?
That means, the method is *guaranteed* to return a List that satisfies that criteria? Is that right?
If that's so (maybe it's not but assuming it is), given that Object is a superclass of "E extends Charsequence", i.e. for any class E that extends CharSequence, then why can't List<Object> can't satisfy the assignment? I know you're right, because I checked it; I just don't understand the reason why.
You say " once the method returns, there is no way to know what is the exact class of objects stored in the returned List." But you know what they must be - a class that extends CharSequence, or CharSequence, or any parent of CharSequence.
Btw my knocked-up test code is
Q39.java:6: error: incompatible types
List<Object> list = Inner.doIt(stringList);
^
required: List<Object>
found: List<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: String from capture of ? super String
1 error
All this CAP stuff is gobbledegook to me.
Thanks.
Hi
Sorry, I don't get this, and it's come up in another question I think.
When you define a method like this:
"public static <E extends CharSequence> List<? super E> doIt(List<E> nums)"
you're saying, the method will return a List of objects whose type will be "E extends CharSequence" or a superclass of "E extends CharSequence". Correct?
That means, the method is *guaranteed* to return a List that satisfies that criteria? Is that right?
If that's so (maybe it's not but assuming it is), given that Object is a superclass of "E extends Charsequence", i.e. for any class E that extends CharSequence, then why can't List<Object> can't satisfy the assignment? I know you're right, because I checked it; I just don't understand the reason why.
You say " once the method returns, there is no way to know what is the exact class of objects stored in the returned List." But you know what they must be - a class that extends CharSequence, or CharSequence, or any parent of CharSequence.
Btw my knocked-up test code is
Code: Select all
import java.util.*;
public class Q39 {
public static void main(String ... args) {
List<String> stringList = new ArrayList<>();
//List list = Inner.doIt(stringList); // works
List<Object> list = Inner.doIt(stringList); // fails
}
static class Inner<E> {
public static <E extends CharSequence> List<? super E> doIt(List<E> nums) {
return new ArrayList<E>();
}
}
}
Q39.java:6: error: incompatible types
List<Object> list = Inner.doIt(stringList);
^
required: List<Object>
found: List<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: String from capture of ? super String
1 error
All this CAP stuff is gobbledegook to me.
Thanks.