I have 2 classes that are linked using Generic Wildcards
import java.util.*;
public class Main
{
public static void main(String[] args){
AnotherClass another = new AnotherClass();
List<Integer> a1 = new ArrayList<Integer>();
a1.add(1);
another.runtest(a1);
}
}
import java.util.*;
public class AnotherClass {
public void runtest(List<? extends Number> a){
a.add(2);
}
}
But while executing the above code I am getting exceptions as below:
AnotherClass.java:4: error: no suitable method found for add(int)
a.add(2);
^
method Collection.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
method List.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
where CAP#1 is a fresh type-variable:
CAP#1 extends Number from capture of ? extends Number
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Can someone help me to write the Code in better way or how can we use wildcards during method call. I am not able to understand why it is now allowing extends in method definition.