Yes, reflection is one way. But you can use the interface.
interface I {
int add (int a, int b);
}
let's say you have a class
class B implements I {
int add(int a, int b){
return a + b;
}
}
Now you can create a function such as:
doCalculate(I mehthodInterface) {
\\some calculations
\\u can also use any other functions defined in this interface
methodInterface.add(2, 3);
}
Here you can have an array of interfaces that implement the methods.
source
share