Java: "? extends I" cannot be converted to "? extends I"

generics, java

Solution

In Java Generics, `?` wildcards cannot be identical, because they represent unknown types. An unknown type cannot equal some other unknown type.

If you want the types to match, then you must define a generic type parameter on the `Test` class, and refer to it throughout your class.

public class Test<T extends I> {

    TableView<T> table;

    Test(ObservableList<T> list) {
        table = new TableView<>();
        table.setItems(list);
    }
}

Problem

This code: ``` interface I {} public class Test { TableView<? extends I> table; Test(ObservableList<? extends I> list) { table = new TableView<>(); table.setItems(list); } } ``` ...produces this message: ``` error: method setItems in class TableView<S> cannot be applied to given types; table.setItems(list); required: ObservableList<CAP#1> found: ObservableList<CAP#2> reason: argument mismatch; ObservableList<CAP#2> cannot be converted to ObservableList<CAP#1> where S is a type-variable: S extends Object declared in class TableView where CAP#1,CAP#2 are fresh type-variables: CAP#1 extends I from capture of ? extends I CAP#2 extends I from capture of ? extends I ``` How can there be a type mismatch between two identical types? What am I missing?

Original source