Skip to content
Advertisement

How to pass a type parameterized interface to a method?

How should I pass a type parameterized interface to a method because java compiler errors out on following code saying “Can not resolve symbol T” in “callTwoParFunc” method signature!

class Test{

    interface TwoParFunc<T>{
        T func(T a, T b);
    }

    public void callTwoParFunc(TwoParFunc<T> f, T a, T b) {
        f.func(a, b);
    }
}

Advertisement

Answer

You need to add the <T> parameter to the method signature:

public <T> void callTwoParFunc(TwoParFunc<T> f, T a, T b) {
    f.func(a, b);
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement