5

I am trying to learn more about the Java 8 FunctionalInterface annotation. I wrote the following code as an experiment, but it does not compile:

@FunctionalInterface
public interface HasToString {

    String toString();
}

No target method found

Interestingly, this does compile:

@FunctionalInterface
public interface HasToString {

    String notToString();
}

Why is this?

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
  • You're overriding the toString() method of Object. So it's not an abstract method, but a concrete one. – JB Nizet May 23 '17 at 21:46

1 Answers1

7

This is stated in the JLS 9.8

A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.

As toString is a "public instance method of the class Object", your interface doesn't qualify to be a functional interface.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34