Yes - you need generics:
interface I <T extends I<T>> { void add(T foo); }
To define a class to use, enter it like this:
class C implements I<C> { @Override public void add(C foo) {
Note that there is no way to prevent the coding of this implementer (assuming D also implements I ):
class C implements I<D> { @Override public void add(D foo) {
However, this would only be a problem if the class C encoder knew about the existence of class D and decided to use it, which is unlikely if they focus on the C encoding class.
Even with this caution, if I asked this exam question, I would expect this to be the answer.
source share