When is the functional interface @Functional Interface??

Posted by chaddsuk on Mon, 05 Aug 2019 11:28:43 +0200

When is the Functional Interface @Functional Interface

When is the Functional Interface @Functional Interface

Source Code Definition

/**
 * An informative annotation type used to indicate that an interface
 * type declaration is intended to be a <i>functional interface</i> as
 * defined by the Java Language Specification.
 *
 * Conceptually, a functional interface has exactly one abstract
 * method.  Since {@linkplain java.lang.reflect.Method#isDefault()
 * default methods} have an implementation, they are not abstract.  If
 * an interface declares an abstract method overriding one of the
 * public methods of {@code java.lang.Object}, that also does
 * <em>not</em> count toward the interface's abstract method count
 * since any implementation of the interface will have an
 * implementation from {@code java.lang.Object} or elsewhere.
 *
 * <p>Note that instances of functional interfaces can be created with
 * lambda expressions, method references, or constructor references.
 *
 * <p>If a type is annotated with this annotation type, compilers are
 * required to generate an error message unless:
 *
 * <ul>
 * <li> The type is an interface type and not an annotation type, enum, or class.
 * <li> The annotated type satisfies the requirements of a functional interface.
 * </ul>
 *
 * <p>However, the compiler will treat any interface meeting the
 * definition of a functional interface as a functional interface
 * regardless of whether or not a {@code FunctionalInterface} 
 * annotation is present on the interface declaration.
 *
 * @jls 4.3.2. The Class Object
 * @jls 9.8 Functional Interfaces
 * @jls 9.4.3 Interface Method Body
 * @since 1.8
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}

translate

  1. If an interface has only one abstract method, it is a functional interface.
  2. If there is an abstract method in an interface, which rewrites Object's method, then it will not add 1 to the abstract method of the interface. The abstract method of the interface is still one and still meets the definition of functional interface, then the interface will also be defined as functional interface.
  3. If we declare a Functional Interface annotation on an interface, the compiler will require the interface according to the definition of a functional interface, and if it is not satisfied, it will report an error.
  4. If there is only one abstract method for an interface, but we do not declare a Functional Interface annotation on that interface, the compiler will still treat the interface as a functional interface.

Example

Correct Definition

  • General definition
package com.demo.fuctionalface;

@FunctionalInterface
public interface InterfaceDemo {
    void say();
}
  • Without the @Functional Interface annotation compiler, this interface will also be treated as a functional interface. It is recommended that the @Functional Interface annotation compiler be added.
package com.demo.fuctionalface;

public interface InterfaceDemo {
    void say();
}
  • Rewriting the method in Object is still a functional interface
package com.demo.fuctionalface;

public interface InterfaceDemo {
    void say();
    @Override
    String toString();
}

Explain why the third is also a functional interface:

  • First, the Object class is the parent of all java, so the implementation class of InterfaceDemo inherits the Object class directly or indirectly, that is to say, the toString method of Object is also inherited and implemented in Object. You just need to implement the say() method. Subclasses only need to implement an abstract method, which conforms to the definition of functional interfaces.

Error Definition

  • The compiler will report errors
package com.demo.fuctionalface;

@FunctionalInterface
public interface InterfaceDemo {
    void say();
    String MyString();
}

Topics: Java Lambda