Java 8 Functional interface-functional Interface

Source: Internet
Author: User

Read Catalogue
    • What is a functional interface (functional Interface)
    • Functional interface Uses
    • About @functionalinterface annotations
    • Allows defining default methods in a functional interface
    • Allows defining static methods in a functional interface
    • A function interface allows you to define public methods in Java.lang.Object
    • An example of a functional interface in the JDK
    • Resources
What is a functional interface (functional Interface)

In fact, before talking about lambda expression, the so-called function interface, of course, first is an interface, and then there can only be an abstract method in this interface.

This type of interface is also known as the Sam Interface, which is the single Abstract Method interfaces.

Functional interface Uses

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

As defined by a functional interface as follows:

    @FunctionalInterface    interface Greetingservice     {        void Saymessage (String message);    

You can then use a lambda expression to represent an implementation of the interface (note: JAVA 8 is generally implemented with anonymous classes):

Greetingservice GreetService1 = message, System.out.println ("Hello" + message);
about @functionalinterfaceAnnotations

Java 8 introduces a new annotation @functionalinterface for the functional interface, which is used primarily for compile-level error checking , and when you write an interface that does not conform to the functional interface definition, the compiler will give an error.

Correct example , no error :

    @FunctionalInterface    interface Greetingservice    {        void Saymessage (String message);    

Error Example , the interface contains two abstract methods, violates the definition of the function interface, eclipse error indicates that it is not a functional interface.

Reminder: Add @functionalinterface for the interface is not a functional interface has no effect, the annotation knowledge reminds the compiler to check whether the interface contains only an abstract method

Allows defining default methods in a functional interface

The function interface can include the default method, because the default method is not an abstract method, it has a default implementation, so it is consistent with the definition of functional interface;

The following code does not error:

    @FunctionalInterface    interface Greetingservice    {        void Saymessage (String message);        default void DoSomeMoreWork1 ()        {            //method body        }        default void DoSomeMoreWork2 ()        {            //method Body        }    }
Allows defining static methods in a functional interface

In a functional interface, a static method can be included, 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 does not error:

    @FunctionalInterface    interface Greetingservice     {        void Saymessage (String message);        static void Printhello () {            System.out.println ("Hello");        }    }
A function interface allows you to define public methods in Java.lang.Object

The function interface is a public method that can contain object, which is not considered an abstract method for a functional interface (although they are abstract methods), because the implementation of any functional interface inherits the object class by default and contains the The realization of these abstract methods in Java.lang.Object;

The following code does not error:

    @FunctionalInterface    interface Greetingservice      {        void Saymessage (String message);                @Override        boolean equals (Object obj);    }
An example of a functional interface in the 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.

Java 8 Functional interface-functional Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.