I have the following classes:
abstract class A
{
protected HashMap<Integer, ?> genericMap;
public HashMap<Integer,?> getMap(){
return this.genericMap;
}
}
class B extends A
{
public B() {
this.genericMap = new HashMap<Integer,Rock>();
}
public Rock getRock(Integer key){
return (Rock) this.genericMap.get(key);
}
public void addRock(Integer key, Rock value){
this.genericMap.put(key, value);
}
}
Basically all the classes that extends A (like B, C, D, etc...) are going to instantiate in the constructor an HashMap with the same key type but a different type for the value.
When I do the get of the hashmap value, in a subclass, I simply need to do the cast.
But when I try, in a subclass, to put something into the hashmap I get the following error:
The method put(Integer, capture#3-of ?) in the type HashMap<Integer, capture#3-of ?> is not applicable for the arguments (Integer, Rock).
I've also tried the arguments (Integer, Object)
, but same error.
What I'm doing wrong? Thank you.