This code is taken from the OCP textbook. Why do versions 1 and 2 of the addSound method not compile but versions 3 and 4 compile? Compilation error message is "add(capture ) in List cannot be applied to java.lang.String"
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
strings.add("tweet");
List<Object> objects = new ArrayList<>(strings);
addSound(strings);
addSound(objects);
}
//version 1
public static void addSound(List<?> list) {
list.add("quack");
}
//version 2
public static void addSound(List<? extends Object> list) {
list.add("quack");
}
//version 3
public static void addSound(List<Object> list) {
list.add("quack");
}
//version 4
public static void addSound(List<? super String> list) {
list.add("quack");
}