Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call methods in functional interfaces without .Method()

Tags:

java

public class Main {
    public static void main(String[] args) {
        MyFunctionalInterface_1 F;
        MyFunctionalInterface_2 G;
        F = X::Method;
        int i = F.Method(5);
        System.out.println(i);
        G = new X()::Method;
        G.Method();
    }
}

class X{
    public static int Method(double x){
        return (int)(x*x*x);
    }

    public void Method(){
        System.out.println("Huy sosi");
    }
}

@FunctionalInterface
interface MyFunctionalInterface_1{
    int Method(double x);
}

@FunctionalInterface
interface MyFunctionalInterface_2{
    void Method();
}

How can I call function as F() or G()?

I was doing exam question (translate from Russian):

Write an entity declaration F, G and X in Java so that the following code fragment compiles without errors

"F = X::Method; int i = F(0.0); G = new X()::Method; G();"

I don't know how can I call them this way. () and (0.0) Or maybe I should use something different and not just functional interfaces. I am not specialist in java. Can you help me?

like image 991
mike Avatar asked Jan 08 '20 10:01

mike


People also ask

How do you call a functional interface method?

In Functional interfaces, there is no need to use the abstract keyword as it is optional to use the abstract keyword because, by default, the method defined inside the interface is abstract only. We can also call Lambda expressions as the instance of functional interface.

Can we call method inside interface?

You call methods on objects. Not on interfaces.

Can functional interface have default methods?

You can have default methods in a functional interface but its contract requires you to provide one single abstract method (or SAM). Since a default method have an implementation, it's not abstract. Conceptually, a functional interface has exactly one abstract method.

Can functional interface have static methods?

A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method. The above interface still counts as a functional interface in Java, since it only contains a single non-implemented method.


1 Answers

It's possible like this, exploiting the rules around the meaning of a name:

static int F(double d) {
  return 0;
}

static void G() {}

static class X {
  void method();
}

public static void main(String[] args) {
  Consumer<X> F;
  Runnable G;

  F = X::Method; int i = F(0.0); G = new X()::Method; G();
}

Note that F(0.0) isn't invoking the consumer, and G() isn't running the runnable. However, this does meet the criterion of "compiles without errors".


After a bit of thought, I came up with a way to define it so that the method invocations do the same as the functional methods would:

class X {
  static int F(double x) {
    return 0;
  }

  static int Method(double x) {
    return F(x);
  }

  static void G() {}

  void Method() {
    G();
  }

  public static void main(String[] args) {
    DoubleToIntFunction F;
    Runnable G;

    F = X::Method; int i = F(0.0); G = new X()::Method; G();
  }
}

What a mess.

like image 189
Andy Turner Avatar answered Oct 29 '22 02:10

Andy Turner