Suppose I have the following interface:
public interface Interface<T extends Number>{
public Vector<Interface<T>> getVector();
}
and the following class implementing that interface:
public abstract class C<T extends Number> implements Interface<T>{
private Vector<C<T>> vector;
public Vector<Interface<T>> getVector(){ //errror
return this.vector;
}
}
Why is not legal returning a Vector<C<T>> meanwhile is legal ( obviously) returning a Vector<Interface<T>>. C is actually implementing Interface, so it should be possible, right? What am I missing?
EDIT:
why this work for non generics interface? Is this actually a generic related problem?
public interface Interface{
public Interface getVector();
}
public abstract class C implements Interface {
private C;
public Interface getVector(){ //errror
return this.c;
}
}
Because the
Vectoris explicitly made up ofInterface<T>, not things that extendInterface<T>, I believe this would work if you changed the definition toThe problem is that for some
V implements TorV extends TthatFoo<V>is not a supertype ofFoo<T>. The compiler does not test inheritance on the generic arguments unless you explicitly indicate that extension point.Using
Vector<? extends Interface<T>>means “allow any class that implements or extendsInterface<T>, whereasVector<Interface<T>>means a vector consisting only ofInterface<T>items.Perhaps it’s more concrete to consider that
List<Integer>is not an acceptable replacement forList<Number>despiteIntegerextendingNumberfor precisely the same reason.update:
I tested this and the following compiles without any errors or warnings