Java Generics: Warning requires an uncontrolled act to match <InterfaceName>
I have an interface
interface x {
A getValue();
}
and implementation
class y implements x {
public B getValue() { return new B();}
}
B is a subclass of A. This works because of covariant redefinition, I think.
But if I rewrote the interface as
interface x{
<T extends A> T getValue();
}
I get a warning in the implementation that
Warning requires an uncontrolled cast to match A.getValue ()
What is the difference between the two versions of the interface? I thought they were the same.
+5
4 answers
, , . T , - ? T:
interface x <T extends A> {
T getValue()
}
, , , :
interface x {
<T extends A> T getValue(T someArgument)
}
interface x {
<T extends A> T getValue(Class<T> someArgument)
}
Then there would be no warning.
edit: see waxwing post for an explanation of why you should not use generics for this particular problem. I just show how generics will be used.
+1