Programing

Java 8에서 "기능적 인터페이스"의 정확한 정의

lottogame 2020. 11. 22. 18:50
반응형

Java 8에서 "기능적 인터페이스"의 정확한 정의


최근에 저는 Java 8을 탐색하기 시작했고 Java의 람다 표현식 구현에 필수적인 "기능적 인터페이스"개념을 이해하지 못했습니다. Java의 람다 함수에 대한 꽤 포괄적 인 가이드 가 있지만 기능적 인터페이스 개념에 대한 정의를 제공하는 장에 갇혀있었습니다 . 정의는 다음과 같습니다.

보다 정확하게는 기능적 인터페이스는 정확히 하나의 추상 메서드가있는 인터페이스로 정의됩니다.

그런 다음 그는 Comparator인터페이스 의 예를 진행합니다 .

public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
} 

Comparator 인수 대신 람다 함수를 사용할 수 있는지 테스트 할 수 있었고 작동합니다 (예 :) Collections.sort(list, (a, b) -> a-b).

그러나 Comparator 인터페이스에서 compareequals메서드는 모두 추상적입니다. 즉, 두 가지 추상 메서드가 있습니다. 그렇다면 정의에 정확히 하나의 추상 메서드 를 갖는 인터페이스가 필요한 경우 어떻게 작동 할 수 있습니까? 내가 여기서 무엇을 놓치고 있습니까?


링크동일한 페이지에서 :

인터페이스 Comparator는 두 개의 추상 메서드를 선언하지만이 중 하나 (같음)가 Object의 공용 메서드에 해당하는 서명을 가지고 있기 때문에 작동합니다. 인터페이스는 항상 Object의 공용 메서드에 해당하는 추상 메서드를 선언하지만 일반적으로 암시 적으로 그렇게합니다. 암시 적으로 또는 명시 적으로 선언되었는지에 관계없이 이러한 메서드는 개수에서 제외됩니다.

나는 그것을 더 잘 말할 수 없다.

편집 : Maurice의 의견에 따라이 페이지의 최신 텍스트로 업데이트되었습니다 (감사합니다!).


@FunctionalInterface 페이지 에 또 다른 설명이 있습니다 .

개념적으로 기능적 인터페이스에는 정확히 하나의 추상 메서드가 있습니다. 기본 메서드에는 구현이 있으므로 추상이 아닙니다. 인터페이스가의 공용 메서드 중 하나를 재정의하는 추상 메서드를 선언하는 경우 java.lang.Object인터페이스의 모든 구현이 java.lang.Object또는 다른 곳 에서 구현되므로 인터페이스의 추상 메서드 수에 포함되지 않습니다 .

사용하여 어떤 인터페이스가 올바른 기능 인터페이스 인지 테스트 할 수 있습니다 @FunctionalInterface.

예 :

  • 이것은 작동한다

    @FunctionalInterface
    public interface FunctionalInterf {
    
        void m();
    
        boolean equals(Object o);
    
    }
    
  • 이로 인해 오류가 발생합니다.

    @FunctionalInterface
    public interface FunctionalInterf {
    
        void m();
    
        boolean equals();
    
    }
    

    FunctionalInterf 인터페이스에서 발견 된 재정의되지 않는 여러 추상 메서드


Q. 그러나 Comparator 인터페이스에서 compare () 및 equals () 메서드는 모두 추상적입니다. 즉, 두 개의 추상 메서드가 있습니다. 그렇다면 정의에 정확히 하나의 추상 메서드를 갖는 인터페이스가 필요한 경우 어떻게 작동 할 수 있습니까? 내가 여기서 무엇을 놓치고 있습니까?

ㅏ.

기능적 인터페이스는 "기능적 인터페이스"상태에 영향을주지 않고 equals ()와 같이 Object에 의해 정의 된 모든 공용 메서드를 지정할 수 있습니다. 공용 Object 메서드는 기능 인터페이스의 인스턴스에 의해 자동으로 구현되기 때문에 기능 인터페이스의 암시 적 멤버로 간주됩니다.


Java 문서는 다음과 같이 말합니다.

Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order.

Maybe Comparator is special? Maybe, even though it's an interface, there is somehow a default implementation of equals() that calls compare()? Algorithmically, it's trivial.

I thought all methods that were declared in interfaces were abstract (i. e. no default implementation). But maybe I'm missing something.


Definition:

If an interface contains only one abstract method, then such type of interface is called functional interface.

Usage:

  1. Once we write Lambda expression "->" to invoke its functionality , then in this context we require Functional Interface.
  2. We can use the Functional Interface reference to refer Lambda expression.
  3. Inside functional interface we can have one abstract method and n number of default/static methods.

Functional interface with respect to inheritance:

If an interface extends Functional interface and the child interface does not contain any abstract method , then the child interface is also considered to be Functional Interface.

Functional interface is not new to java, its already used in following interface API's:

  1. Runnable : contains run() method only.
  2. Callable : contains call() method only.
  3. Comparable : contains compareTo() method only.

Before Java 8, an interface could only declare one or more methods also known as Abstract Method (method with no implementation, just the signature). Starting with Java 8 an interface can also have implementation of one or more methods (knows as Interface Default Method) and static methods along with abstract methods. Interface Default Methods are marked default keyword.

So the question is, what is Functional Interface? An interface with Single Abstract Method (SAM) is called Functional Interface.

Which means -

  1. An interface with Single Abstract Method is a Functional Interface
  2. An interface with Single Abstract Method and zero or more default methods and zero or more static method is also a valid Functional Interface.

More detail with example code https://readtorakesh.blogspot.com/2018/08/functional-interface-java8.html

참고URL : https://stackoverflow.com/questions/14655913/precise-definition-of-functional-interface-in-java-8

반응형