public static void main(String... args) {
List<Child> list1 = new ArrayList<Child>();
method2(list1);
}
public static void method2(List<Parent> list1) {
}
I get below compilation error
The method method2(List) is undefined ...
Above issue can be solved with modifying List<Parent> list1
to List<? extends Parent> list1
.
But if i try to add child object like below
public static void method2(List<? extends Parent> list1) {
Child child1 = new Child();
list1.add(child1);
}
It gives compilation error again
The method add(capture#1-of ? extends Parent) in the type List is not applicable for the arguments (Child)
So my question is if List<Child>
can be passed as parameter to List<? extends Parent> list1
why we can't add child object under List<? extends Parent>
?