Functional interfaces in Java 8

codahale-metrics, java, java-8

Solution

An abstract class (even if it only has one abstract method) is not a functional interface. Only an interface can be one.

From JLS 9.8:

A functional interface is an interface that has just one abstract method (aside from the methods of Object)... (emphasis added)

The original idea was to let abstact classes be expressed as a lambda; they were called "SAM types," which stood for "single abstract method." That turned out to be a difficult problem to solve efficiently. This thread talks a bit about why; basically, the base class's constructor made it difficult.

Problem

I'm having a hard time sorting out why lambda expressions are assignable to some functional interfaces, but not others. An example, using some functional interfaces from the Metrics library: ``` Gauge<Double> foo = () -> { return null; }; RatioGauge bar = () -> { return null; }; ``` The second statement has a compile error (in Eclipse): The target type of this expression must be a functional interface As far as I can tell, RatioGauge is a functional interface. Am I missing something?

Original source