How to pass argument in Method_reference in Java 8 for user defined interface and method

Viewed 97

My code is something like this,

@FunctionalInterface
interface MathOperation2 {
    int operation2(int a, int b, int c);
 }

public class Method_reference_demo {
    private static int operate2(int a, int b, int c, MathOperation2 obj) 
    { 
        return obj.operation2(a, b, c);
    }
    private void Method_reference_demo01() 
    {       
    MathOperation2 mo2 = Method_reference_demo::operate2;
    mo2.operation2(2,3,4);
    }
}

is there anyway I can make it work by passing parameter through last two lines. means below lines. MathOperation2 mo2 = Method_reference_demo::operate2; mo2.operation2(2,3,4); I want above snippet as working code.
NOTE: I can't change any line of code except these two lines and want to use Java 8 Method reference.

2 Answers

Bottom line:- You need to provide an implementation of the interface at some point in time to make use of it.


The line of code

MathOperation2 mo2 = Method_reference_demo::operate2

in an invalid declaration, since the signature of the operate2 method

int operate2(int a, int b, int c, MathOperation2 obj)

expects a MathOperation2 to be passed to the method as well.

Note, it can work if you change the signature to remove the last parameter, but that would make it inefficient as one would be better defining the abstract method of the interface itself which follows the same signature then.


I can't change any line of code except these two lines

You can then rather define your interface as :

MathOperation2 mo2 = (a, b, c) -> {
    return 0; // perform the operation with a, b and c here
};
System.out.println(mo2.operation2(2, 3, 4)); // just to print the output

for e.g., to add three integers the representation would be:

MathOperation2 mo2 = (a, b, c) -> a + b + c;
System.out.println(mo2.operation2(2, 3, 4)); // would print '9'

(From comments) In terms of method references, the example would translate into something on the lines :

private static int operate2(int a, int b, int c) { // <<-- notice the method signature
    return a + b + c; // <<--  and a definition
}

private void Method_reference_demo01() {
    MathOperation2 mo2 = Method_reference_demo::operate2;
    System.out.println(mo2.operation2(2, 3, 4)); // prints '9'
}

The method reference, MathOperation2 mo2 should point to an implementation of the method, int operation2(int a, int b, int c) i.e. you can do something like

@FunctionalInterface
interface MathOperation2 {
    int operation2(int a, int b, int c);
}

public class Main {
    private static int operate2(int a, int b, int c) {
        return a + b + c;
    }

    private static void methodReferenceDemo01() {
        MathOperation2 mo2 = Main::operate2;
        System.out.println(mo2.operation2(2, 3, 4));
    }

    public static void main(String[] args) {
        methodReferenceDemo01();
    }
}

But it does not fulfill your requirement 100% as you have mentioned:

I can't change any line of code except these two lines

On a side note, you should follow the Java naming conventions e.g. the method name, Method_reference_demo01 does not follow the conventions; it can be named as methodReferenceDemo01.

Related