Reputation: 864
I have an interface Exec
public interface Exec<T, U> {
U execute(final T context);
}
Now can I have a class which implements interface Exec as below
public class BatchExec<T, U> implements Exec<List<T>, List<U>>
My doubt is Exec accepts T and U as type parameters and in this case we are passing it as List and List but BatchExec expects T and U?
Upvotes: 0
Views: 51
Reputation: 7166
As Oliver Charlesworth pointed out, the the U
and T
in the BatchExex<...>
are different than those in Exec<T, U>
. I.e. if you declare BatchExec
like this:
public class BatchExec<T, U> implements Exec<List<T>, List<U>>
Then the execute method signature will contain List<T>
and List<U>
:
public List<U> execute(List<T> context)
This might be confusing so let's create an OtherbatchExec
with other type parameters:
public class OtherBatchExec<P, Q> implements Exec<List<P>, List<Q>> {
@Override
public List<Q> execute(List<P> context) {
return null;
}
}
Just to demonstrate it, you can invoke their constructor the same exact way:
Exec<List<String>, List<Integer>> exec = new BatchExec<String, Integer>();
Exec<List<String>, List<Integer>> otherExec = new OtherBatchExec<String, Integer>();
For the sake of readability, I added the type parameters to the constructor call too. You can use the diamond operator too:
Exec<List<String>, List<Integer>> exec = new BatchExec<>();
Exec<List<String>, List<Integer>> otherExec = new OtherBatchExec<>();
Upvotes: 1