Let's say I have these classes:
public interface Ordinal{}
public class One implements Ordinal {
public static final One FIRST = new One();
}
public class Two extends One {
public static final Two SECOND = new Two();
}
public class Three extends Two {
public static final Three THIRD = new Three();
}
Then an interface, with a method which accepts any sub type of Ordinal
interface.
public interface UseOrdinal<T extends Ordinal> {
void use(T ordinal);
}
Then a client which implements interface.
public class Client implements UseOrdinal<Three> {
@Override
public void use(Three ordinal) {};
}
The problem is that Client
's use
method can only accept instances of class Three
. But I want that it would accept it's super types as well, such as One
and Two
. How can I achieve this?
Edit (added question Context): I have loads of small and different collections ranging from 1 to 12 elements in my application. So to make it more readable and reduce exception handling, I want to encapsulate access to it's elements, so that instead of T get(int index)
I would have T get(Ordinal o)
where Ordinal
would contain only allowed indexes for particular collection.