0
public interface Store<T> {
    Optional<List<? extends T>> method1();
}

public StoreImpl<R extends SomeType> implements Store<SomeType> {

    @Override
    public Optional<List<R>>    method1() {
        Optional<List<R>>  toReturn = Optional.empty();
           :
        toReturn = somePrivateMethodReturningListOfR();
           :
        return toReturn;
    }
}

Compiler cribs reporting incompatible return types for method1() between the interface and concrete implementation.

What am I missing here ?

Mahesh
  • 133
  • 6
  • 1
    You don't need ```? extends``` Since T will be defined in the implementation – Marcos Vasconcelos Jun 15 '18 at 17:50
  • @MarcosVasconcelos I do not understand your suggestion. I want to be able to return a list of concrete objects extending T. – Mahesh Jun 15 '18 at 17:54
  • A dupe of https://stackoverflow.com/q/2745265 `Optional` is not assignable to `Optional`, even if `X` is assignable to `Y`, you'd need `Optional extends Y>` i.e. `Optional extends List extends T>>`. – Jorn Vernee Jun 15 '18 at 18:04
  • You probably don't need the wildcard at all. You can put objects which extend `T` in a `List`. – Radiodef Jun 15 '18 at 18:06

0 Answers0