Reputation: 45
I am having trouble understanding a code that is using an interface function but i don't seem to find the implementation for it. Is it possible to call an unimplemented interface function? The code is below, The class that calls the unimplemented function combineResults:
public final class Parallel {
public static <T> T For(int start, int end,
ParallelForDelegate<T> delegate, T input) {
T res = null;
for (int i = start; i < end; i++) {
res = delegate.combineResults(res, delegate.delegate(i, input));
}
return res;
}
}
The interface:
public interface ParallelForDelegate<T> {
public T delegate(int i, T input);
public T combineResults(T result1, T result2);
}
Upvotes: 2
Views: 834
Reputation: 340
In the method
public static T For(int start, int end, ParallelForDelegate delegate, T input)
of Parallel class you are taking ParallelForDelegate as a reference, so, you can call methods of ParallelForDelegate Interface. the key thing is you have to pass implementation class object of ParallelForDelegate interface when you call For (-,-,-,-) method. So,
delegate.combineResults(res, delegate.delegate(i, input));
calls, that implementation class method at runtime.
Upvotes: 1
Reputation: 633
There has to be an implementation somewhere, but sometimes you can have a hard time finding it, because of various factories, dependecy injections, 'on-the-fly' building, ... mecanisms
On complex APIs like Hibernate, it can be tedious. However, the interface is supposed to give you enough details on its usage so you don't actually have to know how it's implemented.
Upvotes: 1