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?
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.