Possible Duplicate:
Is `List<Dog>` a subclass of `List<Animal>`? Why aren’t Java’s generics implicitly polymorphic?
I need help understanding whats going on.
I have an interface
public interface Foo {
Map<String, ParameterValue> getParameters();
}
public interface ParameterValue { }
And an implementation of Foo.
class FooImpl implements Foo {
Map<String, ParameterValueImpl> parameters = new HashMap<String, ParameterValueImpl>();
//ParameterValueImpl an implementation of ParameterValue
@Override
public Map<String, ParameterValue> getParameters() {
return ((Map<String,ParameterValue>) parameters);
}
}
I get the following error
[ERROR] Failed to execute goal
failure [ERROR]
src/main/java/domainJPA/model/FooImpl.java:[92,45]
inconvertible types [ERROR] found :
java.util.Map<java.lang.String,domainJPA.model.ParameterValueImpl>
[ERROR] required:
java.util.Map<java.lang.String,domain.model.ParameterValue>
I have to use the ParameterValue in the interface of Foo, and in FooImpl I must just the implementation of ParameterValue (ParameterValueImpl) because its an @Entity and jpa requires it.
How can I make this code compile?
EDIT
If I change the code to ? extends ParameterValue
as suggested in one of the answers, then I cannot use the Foo interface.
The following code results in compilation error: The method put(String, capture#8-of ? extends ParameterValue) in the type Map<String,capture#8-of ? extends ParameterValue> is not applicable for the arguments (String, ParameterValueImpl)
foo.getParameters().put("test", new ParameterValueImpl);