0

Let's say I have two interfaces, Foo and Bar:

interface Foo<T extends Bar<U>, U> {
    T getBar();

    default U get() {
        return getBar().get();
    }
}

interface Bar<U> {
    U get();
}

The implementations would look like this:

class BarImpl implements Bar<String> {
    @Override
    String get() {
        return "bar";
    }
}

class FooImpl implements Foo<BarImpl, String> { // Specifying String here is redundant, we know that BarImpl implements Bar<String>
    @Override
    public BarImpl getBar() {
        return new BarImpl();
    }
}

Ideally, Foo could be defined with just a single specified type parameter (T) and U could be inferred, so the declaration of FooImpl could simply be class FooImpl implements Foo<BarImpl>. At this point though, so that we may use U in the Foo interface, we require a secondary parameter. Is there any way around this?

Ivan G.
  • 700
  • 8
  • 19
  • Possible duplicate of [Why are implicit generics in classes not allowed in Java?](https://stackoverflow.com/q/54139983/1553851) – shmosel Feb 06 '19 at 01:08
  • 2
    since you *want* to distinguish between `T` and `U` (in `Foo` interface), no way around this....but you can (if) re-design your api, and return `Bar` instead of `U` – xerx593 Feb 06 '19 at 01:30

0 Answers0