Can @FunctionalInterfaces have default methods?

前端 未结 4 1414
天命终不由人
天命终不由人 2021-01-01 08:59

Why can\'t I create a @FunctionalInterface with a default method implementation?

@FunctionalInterface
public interface MyInterface {
    default         


        
4条回答
  •  鱼传尺愫
    2021-01-01 09:30

    That's because @FunctionalInterface can have default methods, as many as you want. For example, consider the java.util.Function interface. It contains two default methods: compose and andThen. But there should be exactly one non-default method. Otherwise how compiler would know which of your default methods should be mapped to lambda?

提交回复
热议问题