Reputation: 1590
TLDR: I want to make a generic interface definition stricter after extension.
Let's say we've operations and request/response objects on which they can operate.
I've defined a base interface which mandates every operation should have execute method in it.
public interface Operation<S, T> {
T execute(S request);
}
I want to extend it to bunch of other interface which are for specific operations. e.g.
public interface ReadOperation<S extends ReadRequest, T extends ReadResponse> extends Operations {
T execute(S readRequest);
}
Typically I would want to overwrite the base interface execute definition with a stricter definition in extending interface. But unfortunately it's not achievable as java adds an overloaded method with new execute definition.
I want to understand where does my intuition lack and how in some other form I can achieve it. Tell me if I'm doing it horribly wrong.
Closest I could find here: Overriding a method contract in an extended interface that uses generics (Java)?
Upvotes: 2
Views: 1603
Reputation: 10613
The problem is because you're using a raw type. You don't provide a generic type to the extended Operation
interface, so the compiler doesn't know that the S
and T
in each are meant to be the same. Change your ReadOperation
to this:
public interface ReadOperation<S extends ReadRequest, T extends ReadResponse> extends Operation<S, T> {
T execute(S readRequest);
}
Upvotes: 4