Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No target method found in functional interface

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?

like image 405
sdgfsdh Avatar asked May 23 '17 21:05

sdgfsdh


People also ask

What are the methods allowed in functional interface?

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.

What is not a functional interface?

stream. Stream is not a functional interface–it has numerous abstract methods.

Can we have non abstract method in functional interface?

A functional interface must have exactly one abstract method. A functional interface has any number of default methods because they are not abstract and implementation already provided by the same.

Why there is only one abstract method in functional interface?

The functional interface also known as Single Abstract Method Interface was introduced to facilitate Lambda functions. Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method.


1 Answers

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.

like image 88
dumbPotato21 Avatar answered Oct 20 '22 10:10

dumbPotato21