I am puzzled by this compilation error. Consider the code snippet below, ConcreteClass1
has the following compilation error:
Error:(16, 28) java: incompatible types: java.util.Map java.lang.String,java.lang.String> cannot be converted to java.util.Map java.lang.String,java.lang.Object>
Yet ConcreteClass2
compiles successfully, simply because it extends AbstractClass
rather than AbstractClass<Object>
. Why is this?
abstract class AbstractClass<T extends Object> {
void testGenerics(Map<String, Object> map) {
}
}
class ConcreteClass1 extends AbstractClass<Object> {
public void testGenerics() {
Map<String, String> map = new HashMap<>();
super.testGenerics(map);
}
}
class ConcreteClass2 extends AbstractClass {
public void testGenerics() {
Map<String, String> map = new HashMap<>();
super.testGenerics(map);
}
}