Q:

Compile-time error using List.addAll to add a List to another one

In this code, the last line (list1.addAll(list2);) generates the compile-time error

The method addAll(Collection< ? extends capture#3-of ? extends BaseClass>) in the type List< capture#3-of ? extends BaseClass> is not applicable for the arguments (List< capture#4-of ? extends BaseClass>)

I do realize that if this was allowed it would be possible to add to the same list instances of different classes. However I don't know what's the right way to go about this.
My getList() method needs to return subclasses of BaseClass, I cannot change that requirement.
I have these two lists, declared in the same way and containing the same type of objects, and I need to add one into the other one.
Any thoughts about this would be appreciated.
BaseClass.java:
package main;

public class BaseClass {}

Main.java:
package main;

import java.util.ArrayList;
import java.util.List;

public class Main {

    private static List<? extends BaseClass> getList() {

        return new ArrayList<>();
    }

    public static void main(String[] args) {

        List<? extends BaseClass> list1 = getList();

        List<? extends BaseClass> list2 = getList();

        list1.addAll(list2); // Compile-time error: "The method addAll(Collection<? extends capture#3-of ? extends BaseClass>) in the type List<capture#3-of ? extends BaseClass> is not applicable for the arguments (List<capture#4-of ? extends BaseClass>)".
    }

}

A:

You could make a new List<BaseClass> and add both lists to that:
List<BaseClass> merged = new ArrayList<>();
merged.addAll(list1);
merged.addAll(lise2);

But it might be a better idea to have getList return a List<BaseClass>.

