I know it can be applied on interface but as Interfaces with default methods ( Java 8+ Interfaces ) are very close now to abstract classes, Just wondering now can we use @FunctionalInterface with Abstract classes?
If not why we can't?
I know it can be applied on interface but as Interfaces with default methods ( Java 8+ Interfaces ) are very close now to abstract classes, Just wondering now can we use @FunctionalInterface with Abstract classes?
If not why we can't?
You can not apply @FunctionalInterface
to abstract class, enum or another annotation. From java doc:
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.
As the name indicates it should be used for interfaces. Applying it to class results in the following:
Error:(10, 1) java: Unexpected @FunctionalInterface annotation xxx is not a functional interface
But this is not much of a problem, as this is just a marker interface for interfaces with one method.
The functional interface is there for the specific purpose - It's used with interfaces only which have strictly one and only one abstract method. if not then the compiler complains -
Invalid '@FunctionalInterface' annotation; TestInterface is not a functional interface
And there is no point in using this with any other class, enum stuffs