Can @FunctionalInterfaces have default methods?

You can have default methods in a functional interface but its contract requires you to provide one single abstract method (or SAM). Since a default method have an implementation, it’s not abstract.

Conceptually, a functional interface has exactly one abstract method.
Since default methods have an implementation, they are not abstract.

and

If a type is annotated with this annotation type, compilers are
required to generate an error message unless:

The type is an interface type and not an annotation type, enum, or
class.

The annotated type satisfies the requirements of a functional
interface.

Here you don’t satisfy the functional interface’s requirement, so you need to provide one abstract method. For example:

@FunctionalInterface
interface MyInterface {

    boolean authorize(int val);
    
    default boolean authorize(String value) {
        return true;
    }
}

Note that if you declare an abstract method overriding one of a public method from the Object’s class it doesn’t count, because any implementation of this interface will have an implementation of those methods through at least the Object’s class. For example:

@FunctionalInterface
interface MyInterface {

    default boolean authorize(String value) {
        return true;
    }

    boolean equals(Object o);
}

does not compile.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)