Java Program to Pass Method as an Arguments to Another Method
What is Method in Java ?
A method in Java is a section of code that executes a single task or group of related actions. It is used to arrange and encapsulate functionality within a programme and is a key building element of object-oriented programming.
Parameters, or the inputs that the method uses, and return values, or the results the method creates, are both possible for methods. Code reuse and modularity are made possible by the ability of methods to be called or invoked from other sections of the programme.
Steps to Pass Method as an Arguments :
- Create a functional interface whose method signature corresponds to the method you intend to send as an argument.
- Put into practise the method you want to pass as a parameter.
- Define the method that will take the argument from the method call. This method must accept a functional interface type parameter.
- Call the method that takes a method call argument in the form of a method reference or a lambda expression.
- Invoke the method on the functional interface in the method that accepts the method call as a parameter. The method that was supplied as a parameter will now be executed.
Pseudo Code for the above algorithm :
public interface MyFunctionalInterface { void doSomething(); } public class MyClass { public void myMethod() { System.out.println("Hello, world!"); } } public static void main(String[] args) { MyClass obj = new MyClass(); callMethod(obj::myMethod); // pass myMethod as an argument to callMethod } public static void callMethod(MyFunctionalInterface func) { func.doSomething(); // invoke the method call on the functional interface } public interface MyFunctionalInterface { void doSomething(); } public class MyClass { public void myMethod() { System.out.println("Hello, world!"); } } public static void main(String[] args) { MyClass obj = new MyClass(); callMethod(obj::myMethod); // pass myMethod as an argument to callMethod } public static void callMethod(MyFunctionalInterface func) { func.doSomething(); // invoke the method call on the functional interface }
Using the method reference syntax, we may send MyClass::myMethod as an argument to callMethod. We can call doSomething on the functional interface by passing an argument of type MyFunctionalInterface to callMethod. The myMethod of MyClass that we handed in as an argument will be executed when we call the functional interface.
Example 1 :
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // Call methodOne and pass methodTwo as an argument methodOne(arr, Main::methodTwo); } public static void methodOne(int[] arr, MethodTwoInterface func) { func.methodTwo(arr); // call methodTwo on the functional interface } public interface MethodTwoInterface { void methodTwo(int[] arr); } public static void methodTwo(int[] arr) { for (int i : arr) { System.out.print(i + " "); } } }
Output :
1 2 3 4 5
Using the method reference syntax, we may give an argument to methodOne called Main::methodTwo. We can invoke methodTwo on the functional interface by passing methodOne an argument of type MethodTwoInterface. The methodTwo of Main that we passed in as a parameter will be executed when we call the functional interface. This software will produce the numbers 1, 2, 3, 4, and 5.
Example 2 :
public class Main { public static void main(String[] args) { int num1 = 5; int num2 = 10; // Call methodOne and pass methodAdd and methodMultiply as arguments methodOne(num1, num2, Main::methodAdd, Main::methodMultiply); } public static void methodOne(int num1, int num2, MathFunctionInterface funcAdd, MathFunctionInterface funcMultiply) { int resultAdd = funcAdd.doMath(num1, num2); // call methodAdd on the functional interface int resultMultiply = funcMultiply.doMath(num1, num2); // call methodMultiply on the functional interface System.out.println("Addition result: " + resultAdd); System.out.println("Multiplication result: " + resultMultiply); } public interface MathFunctionInterface { int doMath(int num1, int num2); } public static int methodAdd(int num1, int num2) { return num1 + num2; } public static int methodMultiply(int num1, int num2) { return num1 * num2; } }
Output :
Addition result: 15 Multiplication result: 50
Using the method reference syntax, we may provide Main::methodAdd and Main::methodMultiply to methodOne as arguments. method One allows us to call doMath on the functional interfaces by accepting two parameters of type MathFunctionInterface. The methodAdd and methodMultiply of Main will be executed when we call the functional interfaces; we gave those methods as arguments.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment