Java Program to Pass Method as an Arguments to Another Method

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 :

  1. Create a functional interface whose method signature corresponds to the method you intend to send as an argument.
  2. Put into practise the method you want to pass as a parameter.
  3. Define the method that will take the argument from the method call. This method must accept a functional interface type parameter.
  4. Call the method that takes a method call argument in the form of a method reference or a lambda expression.
  5. 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
}

Example 1 : 

Run
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 

Example 2 : 

Run
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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription