JAVA 8 Functional Interface - Functional Interface

What is a functional interface (Functional Interface)

In fact, I mentioned before when I talked about Lambda expressions, the so-called functional interface, of course, is an interface first, and then there can only be one abstract method in this interface .

This type of interface is also known as SAM interface, or Single Abstract Method interfaces.

Functional interface usage

They are mainly used for lambda expressions and method references (which can actually be considered lambda expressions).

For example, a functional interface is defined as follows:

    @FunctionalInterface
    interface GreetingService
    {
        void sayMessage(String message);
    }

Then you can use a Lambda expression to represent an implementation of the interface (Note: Before JAVA 8, it was generally implemented with an anonymous class):

GreetingService greetService1 = message -> System.out.println("Hello " + message);

About @FunctionalInterface annotation

Java 8 introduces a new annotation @FunctionalInterface for functional interfaces, which is mainly used for compile-level error checking . With this annotation, when the interface you write does not conform to the functional interface definition, the compiler will report an error.

Correct example , no errors reported :

    @FunctionalInterface
    interface GreetingService
    {
        void sayMessage(String message);
    }

In the wrong example , the interface contains two abstract methods, which violate the definition of functional interface. Eclipse reports an error indicating that it is not a functional interface.

Reminder: Adding or not adding @FunctionalInterface has no effect on whether the interface is a functional interface or not. This annotation knowledge reminds the compiler to check whether the interface contains only one abstract method

Default methods are allowed in functional interfaces

The functional interface can contain default methods, because the default method is not an abstract method, it has a default implementation, so it conforms to the definition of the functional interface;

The following code will not throw an error:

copy code
    @FunctionalInterface
    interface GreetingService
    {
        void sayMessage(String message);

        default void doSomeMoreWork1()
        {
            // Method body
        }

        default void doSomeMoreWork2()
        {
            // Method body
        }
    }
copy code

Static methods can be defined in functional interfaces

A functional interface can contain static methods, because a static method cannot be an abstract method, it is an already implemented method, so it conforms to the definition of a functional interface;

The following code will not throw an error:

copy code
    @FunctionalInterface
    interface GreetingService
    {
        void sayMessage(String message);
        static void printHello(){
            System.out.println("Hello");
        }
    }
copy code

Functional interfaces allow to define public methods in java.lang.Object

Functional interfaces can contain public methods in Object. These methods are not considered abstract methods for functional interfaces (although they are abstract methods); because the implementation of any functional interface inherits Object by default. Classes that contain implementations of these abstract methods from java.lang.Object;

The following code will not throw an error:

copy code
    @FunctionalInterface
    interface GreetingService  
    {
        void sayMessage(String message);
        
        @Override
        boolean equals(Object obj);
    }
copy code

Example of functional interface in JDK

java.lang.Runnable,

java.awt.event.ActionListener, 

java.util.Comparator,

java.util.concurrent.Callable

Interfaces under the java.util.function package, such as Consumer, Predicate, Supplier, etc.

References

https://sanaulla.info/2013/03/21/introduction-to-functional-interfaces-a-concept-recreated-in-java-8/

http://howtodoinjava.com/java-8/functional-interface-tutorial/

Reprinted from: https://www.cnblogs.com/chenpi/p/5890144.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326089147&siteId=291194637