Home > OS >  How to pass a type parameterized interface to a method?
How to pass a type parameterized interface to a method?

Time:10-21

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);
    }
}

CodePudding user response:

You need to give the parameter a name

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

CodePudding user response:

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);
}
  • Related