I have this sample code showing different method signatures:
public class Main {
public static interface MyMap extends Map {}
public void func1(List<MyMap> m) {}
public void func2(List<Map> m) {}
public void func3(List<? extends Map> m) {}
public void func4(List<? extends Map<?, ?>> m) {}
public <M extends Map<?, ?>> void func5(List<M> m) {}
public static void main(String[] args) {
List<MyMap> myMap = null;
Main main = new Main();
main.func1(myMap); // OK
main.func2(myMap); // not applicable
main.func3(myMap); // OK
main.func4(myMap); // not applicable
main.func5(myMap); // OK
}
}
When I have MyMap
extending the raw type Map
I have these confusions about method signature compatibility in Java.
As seen func1
is a control test, obviously IDE will not complain. In func2
, I can understand MyMap
is not exactly a Map
, hence IDE is complaining. After updating the signature like func3
it works again. As for func4
, I sort of thinking Map<?,?>
is not the same as a raw type Map
, thats why IDE is complaining again? But then what puzzles me most is func5
because it looks equivalent to func4
but IDE is not complaining?