functional-interface

  • Java ์ŠคํŽ™์— ์ •์˜๋œ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•œ ์ •๋ณด์„ฑ ์• ๋…ธํ…Œ์ด์…˜

  • ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค ?

    • ํ•œ๊ฐœ์˜ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง

    • default ์„ ์–ธ๋œ ๋ฉ”์†Œ๋“œ๋Š” ๊ตฌํ˜„์ด ์žˆ์œผ๋ฏ€๋กœ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹˜

    • java.lang.Object ์˜ ๊ณตํ†ต ๋ฉ”์„œ๋“œ(equals()..๋“ฑ) ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋Š”, ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ„ํ•œ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋กœ ์—ฌ๊ฒจ์ง€์ง€ ์•Š์Œ.

@FunctionalInterface
public interface MyFunctionalInterface {
    void hello(); // ์ถ”์ƒ ๋ฉ”์†Œ๋“œ
}
  • ํ•˜๋‚˜์˜ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ์ ์ ˆํ•œ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค

@FunctionalInterface // compile error -> No target method found
public interface MyFunctionalInterface {
    default void hello() {}
}

@FunctionalInterface // compile error -> No target method found
public interface MyFunctionalInterface<T> {
    boolean equals(Object obj);
}
  • default method๋‚˜ Object์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๋Š” ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ


  • @FunctionalInterface๋กœ ์„ ์–ธ๋œ ํƒ€์ž…์€..

    • interface ํƒ€์ž…์ด์–ด์•ผํ•จ. annotation, enum, class ๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ.

  • ๊ทธ๋ ‡์ง€๋งŒ @FunctionalInterface ์–ด๋…ธํ…Œ์ด์…˜ ์„ ์–ธ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ, ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ •์˜ ์ฆ‰ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํ•˜๋‚˜์˜ ์ถ”์ƒ๋ฉ”์„œ๋“œ๋งŒ ๊ฐ–๋Š” ๋‹ค๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ทจ๊ธ‰ํ•จ.

๋Œ€ํ‘œ์ ์ธ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค

์ฐธ๊ณ 

  • https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/FunctionalInterface.html

  • https://johngrib.github.io/wiki/java-functional-interface/

  • https://www.baeldung.com/java-8-functional-interfaces

Last updated