Java 8 Error-The target type of this expression must be a functional interface.
If you are working in Java 8 and getting the following compilation error -
Then this implies that you are trying to assign a Lambda Expression
Article explaining concepts of Lambda Expressions to a reference which is not a Functional Interface.
To understand this error one should be aware of a couple of new concepts in Functional Programming which Java 8 has brought forth namely Lambdas & Functional Interfaces Tutorial on Functional Interfaces.
To better understand why you are getting this compilation error lets look at a sample code snippet where this error will occur -
SelfMadeLambda is an interface defined inside the class LambdaExpression. In the main() method of this class I tried to create 2 lambda expressions "s -> System.out.println("Mr."+s);" and " System.out.println("Mister."+s)".
When I compile this program I get a compilation error saying "The target type of this expression must be a functional interface.".
As per Java specification a lambda expression can only be assigned to a reference of a Functional Interface. Since the interface SelfMadeLambda has 2 abstract methods, it is not a functional interface.
Now, if I remove the non-used abstract method of SelfMadeLambda - "public void secondMethod(int n)", such that only one abstract method "public void printName(String s)" remains, then SelfMadeLambda becomes a valid Functional Interface (see Functional Interfaces Tutorial on Functional Interfaces tutorial to understand) and the code compiles. The corrected code minus the secondMethod() method is as shown below -
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The target type of this expression must be a functional interface
To understand this error one should be aware of a couple of new concepts in Functional Programming which Java 8 has brought forth namely Lambdas & Functional Interfaces Tutorial on Functional Interfaces.
To better understand why you are getting this compilation error lets look at a sample code snippet where this error will occur -
package com.javabrahman.java8;
public class LambdaExpression {
public interface SelfMadeLambda{
public void printName(String s);
public void secondMethod(int n);
}
public static void main(String args[]){
SelfMadeLambda formatStringAppendMr = s -> System.out.println("Mr."+s);
SelfMadeLambda formatStringAppendMister = s-> System.out.println("Mister."+s);
formatStringAppendMr.printName("John Hammomd");
formatStringAppendMister.printName("Francois");
}
}
SelfMadeLambda is an interface defined inside the class LambdaExpression. In the main() method of this class I tried to create 2 lambda expressions "s -> System.out.println("Mr."+s);" and " System.out.println("Mister."+s)".
As per Java specification a lambda expression can only be assigned to a reference of a Functional Interface. Since the interface SelfMadeLambda has 2 abstract methods, it is not a functional interface.
Now, if I remove the non-used abstract method of SelfMadeLambda - "public void secondMethod(int n)", such that only one abstract method "public void printName(String s)" remains, then SelfMadeLambda becomes a valid Functional Interface (see Functional Interfaces Tutorial on Functional Interfaces tutorial to understand) and the code compiles. The corrected code minus the secondMethod() method is as shown below -
package com.javabrahman.java8;
public class LambdaExpression {
public interface SelfMadeLambda{
public void printName(String s);
}
public static void main(String args[]){
SelfMadeLambda formatStringAppendMr = s -> System.out.println("Mr."+s);
SelfMadeLambda formatStringAppendMister = s-> System.out.println("Mister."+s);
formatStringAppendMr.printName("John Hammomd");
formatStringAppendMister.printName("Francois");
}
}