1

I have a generic interface defined as:

public interface Service<T extends Slicer, E extends Slice> {
}

And an implementation:

public class DynamoDbService implements Service<DynamoDbSlicer, DynamoDbSlice> {
}

If I try to instantiate DynamoDbService and assign it to a variable of type Service<DynamoDbSlicer, DynamoDbSlice> like so:

public class Test {

    private void test() {
        final Service<Slicer, Slice> service = new DynamoDbService();
    }
}

I get an incompatible types error:

Incompatible types.
Required: Service <Slicer, Slice>
Found: DynamoDbService

As DynamoDbService implements the generic interface, shouldn't this be ok? The following example without generics works how I'd expect and how I'd like the previous example to work:

public interface ThingInterface {
}

public class Thing implements ThingInterface {
}

private void thingTest() {
    final ThingInterface thingInteface = new Thing();
}
Stuart Leyland-Cole
  • 1,243
  • 7
  • 19
  • 35
  • 1
    `Service` is not a valid super-type of `Service`. See [duplicate](https://stackoverflow.com/q/2745265/5221149) for why. – Andreas May 29 '19 at 22:14
  • You want `Service service`. – rgettman May 29 '19 at 22:16
  • 1
    @rgettman Or `Service extends Slicer, ? extends Slice>` – Andreas May 29 '19 at 22:16
  • @Andreas thanks very much, `? extends X` was the part I was missing. This got me most of the way there but I added a follow up question here: https://stackoverflow.com/questions/56375349/generic-interface-and-implementation-type-cannot-be-converted – Stuart Leyland-Cole May 30 '19 at 09:27

0 Answers0