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

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.

主站蜘蛛池模板: 渝中区| 灯塔市| 莆田市| 土默特左旗| 蕉岭县| 洛阳市| 开化县| 东丰县| 剑川县| 义乌市| 察隅县| 泰州市| 梁平县| 平湖市| 皮山县| 繁昌县| 丹巴县| 温宿县| 宁海县| 陇南市| 多伦县| 长阳| 射阳县| 湄潭县| 龙胜| 塔河县| 东乡县| 五莲县| 利川市| 马公市| 门源| 开鲁县| 突泉县| 叶城县| 澎湖县| 山丹县| 临海市| 阜南县| 和平区| 奎屯市| 同德县|