I ran into a general problem against a template in java.
class A<T> {
T value;
}
class B<T> {
T value;
}
public <T> A<? extends B<T>> methodA() {
A<B<Object>> gg = fetchFromSomeLocation();
return (A<? extends B<T>>) gg;
}
public <T, E extends B<T>> A<E> methodB() {
A<B<Object>> gg = fetchFromSomeLocation();
return (A<E>) gg;
}
So method B () compiles fine, no problem. But method A causes compilation to fail with:
incompatible types: A<B<java.lang.Object>> cannot be converted to A<? extends B<T>>
I can’t understand why this is so.
PS on Intellij shows how correct the correct code.
EDIT
so apparently this compiles just fine
public static <T> A<? extends B<T>> methodC() {
A<B<Object>> gg = new A<>();
return (A<? extends B<T>>)(A<?>) gg;
}
source
share