Can @FunctionalInterfaces have default methods?

前端 未结 4 1391
天命终不由人
天命终不由人 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:15

    I just want to add a few more points.

    1. We can have any number of Abstract method in FuntionalInterface.

    2. We can also have any number of Static method in FuntionalInterface.

    3. We can also declare an abstract method overriding one of a public method from the Object's class but there must be some other custom abstract method in this functional interface too as shown in below code

      @FunctionalInterface public interface SAM { public void helloSam();

          default void xyz() {
              System.out.println("xyz");
          }
      
          static void abc() {
              System.out.println("abc");
          }
      
          static void abc1() {
              System.out.println("abc1");
          }
      
          default void xyz1() {
              System.out.println("xyz1");
          }
      
           boolean equals(Object o);
      }
      

提交回复
热议问题