A doubt about the Java functional interface: Why does the Comparator interface have two abstract methods compare and equals, and the Comparator is still a functional interface? (@FunctionalInterface)

    First, every functional interface can use an annotation : @FunctionalInterface . Then let's take a look at the javadoc of the annotation:

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
 
Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
 
If a type is annotated with this annotation type, compilers are required to generate an error message unless:
• The type is an interface type and not an annotation type, enum, or class.
• The annotated type satisfies the requirements of a functional interface.
 
However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a Functional Interface annotation is present on the interface declaration.

    Now let's translate it manually :
        According to the definition of the Java language specification, an interface type declaration using this annotation will be regarded as a functional interface . Conceptually, a functional interface has one and only one abstract method . Since the default methods have been implemented, they are not abstract methods. If the abstract method declared in an interface overrides any public method in the superclass Object, these abstract methods will not be counted in the number of abstract methods of the interface. Because the implementation of any interface will get the implementation of these methods from its parent Object or other places.
        Note: The realization of functional interface can be realized by lambda expression , method reference , constructor reference, etc.
        If a type uses this annotation, the compiler will generate an error message, unless the type is an interface type , not an annotation type, enumeration, or class. At the same time, the interface that uses this annotation meets the requirements of a functional interface, that is, a functional interface has and only one abstract method.
        However, the compiler treats all interfaces defined as functional interfaces (that meet the requirements of functional interfaces) as functional interfaces, regardless of whether the annotations for functional interfaces (ie @FunctionalInterface) are used in the interface declaration.
    From it we can know:

  1. A functional interface has one and only one abstract method.
  2. The default methods are not abstract methods because they are already implemented.
  3. A method that overrides any public method in the superclass Object class is not considered an abstract method in the interface.

    So although there are two abstract methods compare and equals in the Comparator interface, equals are not counted as abstract methods in the interface, so the Comparator interface still meets the requirements of a functional interface, and the Comparator interface is a functional interface.

Java-Lambda expressions/method references/constructor references/array references (examples in many cases)

Guess you like

Origin blog.csdn.net/H_X_P_/article/details/105030682