java8 functional interface (FunctionalInterface) [1]

A functional interface represents a contract, a contract for a specific function type. Where it appears, a function that conforms to the contract is actually expected. A lambda expression cannot exist out of context, it must have a clear target type, and this target type is a functional interface.
Citing content from references

The functional interface is newly added in java8. It is different from the definition of ordinary interface in that it only defines a single abstract method (Single Abstract Method (SAM)). Can be written using lambda expressions.

There are many descriptions of lambda expressions and functional interfaces on the Internet, but after reading it, I still do not understand the creation and use of functional interfaces, so I found some information and recorded them to deepen understanding.

本文中主要介绍有以下几点:
1. 唯一抽象方法的定义是什么。
2. 如何使用创建自己的函数式接口。
3. 如何使用lambda表达式编写函数式接口。

A functional interface looks like this

@FunctionalInterface
public OneFunctionalInterface{
    void action();
}

The OneFunctionalinterface interface has only one abstract method, so it is a functional interface.
Here is an example that is not a functional interface

//Not Function Interface, will be compile error
@FunctionalInterface
public NotFunctionalInterface{
    void action();
    void invoke();
}

The NotFunctionalInterface interface will not compile because it is a functional interface marked with @FunctionalInterface, but it has two abstract methods, so it does not meet the requirements of a functional interface.

An interface is a special abstract class. In java, all classes are children of Object, so functional interfaces can contain public methods in the Object class.

@FunctionalInterface
public interface TwoFunctionalInterface{
    void action();

    //Object's public methods
    String toString();
    int hashCode();
    boolean equals(Object o);
}

The TwoFunctionalInterface interface is still a functional interface. If it contains a non-public method of Object (such as: void finalize()), an error will occur.

//Not Function Interface, will be compile error
@FunctionalInterface
public interface NotFunctionalInterfaceTwo{
    void action();

    //Object's protected methods
    void finalize();
}

Although the finalize() method in the NotFunctionalInterfaceTwo interface is also a method in Object, it is modified by the protected keyword, so NotFunctionalInterfaceTwo cannot be defined as a functional interface.

Functional interface can contain static methods
Before java8, static methods can only be defined in classes. In java8, interfaces can define static methods. Static methods are not abstract methods, so a functional interface can contain one or more static methods.

@FuncationalInterface
public interface ThreeFunctionalInterface{
    void action();
    static void dosomethings(){
        System.out.println("write words.");
    }
}

The ThreeFunctionalInterface interface contains an abstract method action(), and a static method dosomethings(), which is still a functional interface.

A functional interface can contain default methods.
Java 8 allows the use of the key default to define a default interface. Because the default method has a concrete implementation, not an abstract method, a functional interface can have one or more default methods.

@FunctionalInterface
public interface FourFunctionalInterface{
    void action();
    default String hello(){
        return "world";
    }
}

Interface inheritance
We all know that interfaces can be inherited. If the parent interface is a functional interface, then the child interface may also be a functional interface.

@FunctionalInterface
public SubFunctionalInterface extends FourFunctionalInterface{

}

The SubFunctionalInterface interface is still a functional interface.

If in a subinterface, another abstract interface is defined, it is no longer a functional interface.

//Not Function Interface, will be compile error
@FunctionalInterface
public interface NotSubFunctionalInterface extends FourFunctionalInterface{
    void action2();
}

The NotSubFunctionalInterface interface inherits the FourFunctionalInterface interface, the FourFunctionalInterface interface has an abstract method action(), and NotSubFunctionalInterface defines another abstract method action2(), then the NotSubFunctionalInterface has two abstract methods, so it is not a functional interface.

Running the functional interface
Let's create a new functional interface and create an instance using a lambda expression.

@FunctionalInterface
public interface MyHandler{
    void action();
    defaul String hello(){
        retrun "world";
    }
}

write test class

public class FunctionalInterfaceTest{
    public static void main(String... args){
        System.out.println("create Functional Interface");
        MyHandler h = ()->{System.out.println("print by action();")};
        h.action();
        System.out.println(h.hello());
        System.out.println("end of test.");
    }
}

operation result

create Functional Interface
print by action();
world
end of test.



Summarize

  • A functional interface can only contain one abstract method
  • A functional interface can contain all public methods in the Object class
  • A functional interface can contain one or more static methods or default methods

This article briefly introduces the definition of functional interfaces, and has a general understanding of functional interfaces. The next article will introduce the judgment of method signatures and declaring exceptions when functional interfaces are inherited.



Guess you like

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