In java 8, an abstract class with only one abstract method is not a functional interface (JSR 335).
This interface
is a functional interface:
public interface MyFunctionalInterface {
public abstract void myAbstractMethod();
public default void method() {
myAbstractMethod();
}
}
but this abstract class
is not:
public abstract class MyFunctionalAbstractClass {
public abstract void myAbstractMethod();
public void method() {
myAbstractMethod();
}
}
So i can't use the abstract class as a target for a lambda expressions and method references.
public class Lambdas {
public static void main(String[] args) {
MyFunctionalAbstractClass functionalAbstractClass = () -> {};
}
}
The compilation error is: The target type of this expression must be a functional interface
.
Why the language designers imposed this restriction ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…