title: How to implement IDE's suggestion: "Anonymous new Foo() can be replaced with lambda"
body: <p>I'm learning anonymous inner classes and lambda functions in Java, especially polymorphism and &quot;passing&quot; interfaces and classes (which apparently means passing an object reference to them, which I'm trying to show below).</p>
  <p>I'd like to make the change that my IntelliJ IDEA IDE suggests at <code>poly3</code> where it says, &quot;Anonymous new Foo() can be replaced with lambda.&quot; How do I complete that?</p>
  <pre class="lang-java prettyprint-override"><code>package org.example;
  
  interface Foo {
      String foo();
  }
  
  public class Bar implements Foo {
      @Override
      public String foo() {
          return &quot;hi&quot;;
      }
  
      public static void main(String[] args) {
          Object poly1 = new Bar();
          Foo poly2 = new Bar();
          Object poly3 = new Foo() { public String foo() { return &quot;hi&quot;; }}; // Anonymous new Foo() can be replaced with lambda
          Object poly4 = () -&gt; { return &quot;hi&quot;; }; // Error: Target type of a lambda conversion must be an interface
      }
  }
  </code></pre>