Reputation: 33
I have the following sample code and produce a "type parameter is not within its bound" error on the last line. Class C/D reuses lots of code from A/B thru the inheritance. How would I defined class Y to not have the error and still uses class D for type parameter? Is there a way I can define class D to use A.B but still has the signature of D extends S for class Y?
public abstract class S<E extends S<E>> extends somethingElse {}
public abstract class R<E extends S<E>> {}
public class A extends Z {
public class B extends S<B> {
}
}
public class C extends A {
public class D extends A.B {
}
}
public class X extends R<B> {} // OK
public class Y extends R<D> {} // Error: Type parameter D is not within its bound; should extends S<D>
Any help would be appreciated.
Upvotes: 3
Views: 109
Reputation: 29680
To get this to compile, you can change E extends S<E>
to E extends S<? super E>
:
public abstract class R<E extends S<? super E>> {}
The cause of this problem is similar to another question that I answered earlier today.
Without the bounded wildcard, D
was extending S<B>
instead of S<D>
.
Upvotes: 2