0

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?

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62

3 Answers3

4

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.

See more Abstract class as functional interface

Community
  • 1
  • 1
Ruslan
  • 6,090
  • 1
  • 21
  • 36
1

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.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
-2

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

Eclipse showing error

And there is no point in using this with any other class, enum stuffs

Arvind Kumar
  • 459
  • 5
  • 21