-7

In java 8 when we have a functional interface it carries only one method which is not implemented :

@FunctionalInterface
public interface Interface1 {

    void method1(String str);

}

Now when we implement this Interface in a Class :

public class MyClass implements Interface1 {

    @Override
    public void method1(String str) {
    }
}

Finally Lets say when I need to call this method from another class

Interface1 object = new MyClass();
object.method1("Say Hello");

Will this be equivalent to

object.("Say Hello");

If so then we don't need method name for funcitonal interfaces.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

1 Answers1

0

Since the example you give here is actually a Consumer, it can be applied to a stream of data (a String Consumer in your example) to perform a given action on each of the elements of the stream. In this case you don't need to be explicit about the method name; like:

List<String> strList = ...;
strList.stream().forEach((str) -> System.out.println(str));

where

(str) -> System.out.println(str)

is your String Consumer

But this is only because the use of the functional interface's method is hidden in the "forEach" definition.

I suppose this may be what you meant by "method name not required".

UmshiniWami
  • 69
  • 1
  • 8