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?