Java generics: Illegal forward reference
- by Arian
Given a generic interface
interface Foo<A, B> { }
I want to write an implementation that requires A to be a subclass of B. So I want to do
class Bar<A, B super A> implements Foo<A, B> { }
// --> Syntax error
or
class Bar<A extends B, B> implements Foo<A, B> { }
// --> illegal forward reference
But the only solution that seems to work is this:
class Bar<B, A extends B> implements Foo<A, B> { }
which is kind of ugly, because it reverses the order of the generic parameters.
Are there any solutions or workarounds to this problem?