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

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.

主站蜘蛛池模板: 彰化市| 天津市| 虎林市| 澄江县| 阿勒泰市| 北宁市| 兴安盟| 上犹县| 嘉善县| 岢岚县| 花莲市| 永胜县| 桃园县| 镇平县| 海门市| 屏东县| 汶川县| 常州市| 通渭县| 揭西县| 武汉市| 正蓝旗| 沅陵县| 嘉荫县| 澄迈县| 巴林右旗| 宜州市| 定襄县| 西和县| 博罗县| 民勤县| 开化县| 湖南省| 贵定县| 惠安县| 新泰市| 连江县| 阳谷县| 余庆县| 津南区| 海城市|