Search code examples
javagenericstype-parameter

Why does type L not have any type parameters even though it extends ArrayList?


This is my code:

private <T,L extends ArrayList<? extends T>> L Foo(L<T> list1) {
    L list2 = new ArrayList<T>();

    //more code

}

I'm using IDEA and get the following error message for the method parameter:

Type 'L' does not have type parameters

and the following error message for creating the ArrayList:

Incompatible types. <br>Required: L <br> Found: java.utils.ArrayList<T>;

Why does this happen? L extends ArrayList so it should have the same type parameters right?


Solution

  • It's not known if ArrayList<T> is an L. For example, L could be a MyArrayListImpl<T>.

    I think this is what you want:

    private <T> List<T> Foo(List<T> list1) {
        List<T> list2 = new ArrayList<T>();
    
        //more code
    
    }