Java generics: Illegal forward reference
Posted
by
Arian
on Stack Overflow
See other posts from Stack Overflow
or by Arian
Published on 2011-01-07T17:52:15Z
Indexed on
2011/01/08
15:54 UTC
Read the original article
Hit count: 288
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?
© Stack Overflow or respective owner