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 –

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The target type of this expression must be a functional interface

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 –

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)”.

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 –

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");
  }
}

 

Digiprove sealCopyright Â© 2014-2022 JavaBrahman.com, all rights reserved.

6 thoughts on “Java 8 Error-The target type of this expression must be a functional interface.”

  1. I have tried the below program with small change.

    public class LambdaExpression {

    public interface SelfMadeLambda{

    public void printName(String s);

    }

    public static void main(String args[]){

    SelfMadeLambda formatStringAppendMr = s -> System.out::println);

    formatStringAppendMr.printName(“John Hammomd”);

    }

    }

    I am getting the same exception:
    The target type of this expression must be a functional interface.

    I just started learning 8 features. Can you throw some light on this.

    1. Hi Srinivas,

      Thanks for asking the query!
      What you are trying to do is correct i.e. you are trying to assign a method reference to a functional interface instance. However, there is a small correction needed in the syntax of the method reference. Specifically, arrow(->) notation is not used for a method reference.The first line of your main() method should be –

      SelfMadeLambda formatStringAppendMr = (System.out::println);

      Your code will work with the above statement now.
      Regards,
      Dhruv

  2. I have tried the below program with small change.

    public class LambdaExpression {

    public interface SelfMadeLambda{

    public void printName(String s);

    }

    public static void main(String args[]){

    SelfMadeLambda formatStringAppendMr = s -> System.out::println);

    formatStringAppendMr.printName(“John Hammomd”);

    }

    }

    I am getting the same exception:
    The target type of this expression must be a functional interface.

    I just started learning 8 features. Can you throw some light on this.

    1. Hi Srinivas,

      Thanks for asking the query!
      What you are trying to do is correct i.e. you are trying to assign a method reference to a functional interface instance. However, there is a small correction needed in the syntax of the method reference. Specifically, arrow(->) notation is not used for a method reference.The first line of your main() method should be –

      SelfMadeLambda formatStringAppendMr = (System.out::println);

      Your code will work with the above statement now.
      Regards,
      Dhruv

Comments are closed.