官术网_书友最值得收藏!

Functional interface

In Java 8, all the Single Abstract Method interfaces (SAM interfaces), such as java.util.Comparator, java.io.FilenameFilter, java.lang.Comparable, java.lang.Runnable, and so on, are given a new name called the Functional interface.

Functional interfaces are the interfaces that consist of the Single Abstract Method. These interfaces are the target for Lambda expressions. So in other words, Lambda expressions can only implement the interfaces that have only one abstract method. This is the other difference between Lambda and anonymous inner classes.

A new annotation type @FunctionalInterface is added in Java 8. So if an interface is annotated with @FunctionalInterface, it is not allowed to have more than one abstract method.

The following declaration will work:

@FunctionalInterface 
public interface Interface1 { 
  void method1(); 
} 

If you try to add one more abstract method to this interface, the compiler will throw an error stating - Interface1 is not a Functional Interface.

主站蜘蛛池模板: 呼图壁县| 明光市| 甘泉县| 罗江县| 阿勒泰市| 调兵山市| 平乐县| 辽宁省| 乌拉特后旗| 长宁县| 洛川县| 昭平县| 象州县| 汝南县| 哈尔滨市| 公主岭市| 石景山区| 墨玉县| 石门县| 当雄县| 嘉义县| 灌阳县| 武清区| 泸溪县| 迁安市| 宝兴县| 图们市| 遵义市| 建昌县| 井研县| 鄱阳县| 周至县| 莲花县| 洛浦县| 赤水市| 乌兰察布市| 廉江市| 古浪县| 同江市| 巴彦县| 松江区|