Reputation: 33
I have a couple of generic classes:
public interface Data<E> {}
public interface Clonable<E extends Clonable<E>> {}
public interface NaturalNumberInterface extends Data<NaturalNumberInterface> {}
public class NaturalNumber implements NaturalNumberInterface {}
public interface SetInterface<E extends Data<E>> extends Clonable<SetInterface<E>> {}
public class Set<E extends Data<E>> implements SetInterface<E> {}
When I'm trying to create the new instance of Set Set<NaturalNumber> s=new Set<NaturalNumber>();
compiler says:
NaturalNumber
is not valid substitute for the type parameter<E extends Data<E>>
of the typeSet<E>
Maybe you can help me to find the mistake, cause I spent a long time and didn't find the solution.
Upvotes: 2
Views: 69
Reputation: 100209
I assume that your SetInterface
is defined in the same way as ListInterface
and Data
is just interface Data<T>
.
The generic argument of SetInterface
is F-bounded: E extends Data<E>
. In your current code NaturalNumber
type extends Data<NaturalNumberInterface>
. So if E
is NaturalNumber
, then condition is violated as it should extend more specific type Data<NaturalNumber>
.
You should use F-bounds for NaturalNumberInterface
as well:
public interface NaturalNumberInterface<T extends NaturalNumberInterface<T>> extends Data<T>
public class NaturalNumber implements NaturalNumberInterface<NaturalNumber>
This way it will work.
Upvotes: 2