Java – Why Can’t an Interface with a Bounded Generic Type Be Implemented?

genericsjava

I have the following interfaces:

public interface Successorable<E> extends Comparable<E> 
    E suc();
}

and

interface IInterval <E extends Successorable<E>> {
    E min();
    E max();
}

I'm trying to implement IInterval by doing:

public class Interval<E> implements IInterval<E> {
 ....

However eclipse complains that the type E is not a valid substitute for the bounded parameter <E extends Successorable<E>> of the type IInterval<E>.

What am I doing wrong?

Thanks!

Best Answer

I believe you need to restate your conditions. The plain old E in Interval<E> doesn't necessarily fulfill E extends Successorable<E> so you need to be more specific about your parameter.

public class Interval<E extends Successorable<E>> implements IInterval<E> {
    @Override
    public E min() {
        throw new UnsupportedOperationException();
    }

    @Override
    public E max() {
        throw new UnsupportedOperationException();
    }
}