Java generics help
I have class A and a class B that extends A.
I have an abstract class X and a concrete class Y that extends X.
In X I have an abstract method with the following signature:
public abstract Collection<? extends A> getStuff();
In Y I implement the abstract method with the following signature:
public Collection<B> getStuff();
When I try to do the following: classX.getStuff(开发者_如何学Go).add(B)
It gave the following complaint: add (capture ) in Collection cannot be applied to B
Any help would be appreciated.
You can use the following example as your X class.
For example,
public abstract <T extends A> Collection<T> getStuff();
You can also refer to this threads.
加载中,请稍侯......
精彩评论