简体   繁体   中英

Why the functional interface is not implemented by the class?

I saw the following code in a tutorial. My question is why the interface Drawable is not implemented by the class LambdaExpressionExample2? I am familiar with composition between classes. Is that the case here as well ? Thank you.

@FunctionalInterface  //It is optional  
interface Drawable{  
    public void draw();  
}  
  
public class LambdaExpressionExample2 {  
    public static void main(String[] args) {  
        int width=10;  
          
        //with lambda  
        Drawable d2=()->{  
            System.out.println("Drawing "+width);  
        };  
        d2.draw();  
    }  
}  

You can implement in main class also:

public class LambdaExpressionExample2 implements Drawable{

    @Override
    public void draw() {
        System.out.println("Implemented it in main class !!!");
    }

    public static void main(String[] args) {

        LambdaExpressionExample2 lm = new LambdaExpressionExample2();
        lm.draw();

    }

 }

But that won't be lambda.

Or you can define another implementation like :

public class DrawableImpl implements Drawable {
    @Override
    public void draw() {

        System.out.println("I have implemented Drawable !!!");

    }
}

public class LambdaExpressionExample2{

    public static void main(String[] args) {

        DrawableImpl dm = new DrawableImpl();
        dm.draw();

    }

    }

But that is also not lambda. You can also use anonymous class as below (old style):

public class LambdaExpressionExample2 {

    public static void main(String[] args) {
        Drawable anonymus_class = new Drawable() {
            @Override
            public void draw() {
                System.out.println("Anonymus class");
            }

        };
        anonymus_class.draw();
    }
}

If you compare all, you will see lambda notation is most precise and intuitive.

The interface is used for the lambda function itself, not the LambdaExpressionExample2 class.

To see what I mean let's have a look at other ways you could achieve this functionality.

  1. Instead of using a lambda, you could use an anonymous inner class. Before lambda functions became a thing in Java 8, this is how you would have had to do it.
public static void main(String[] args) {
    int width=10;

    // with anonymous inner class
    Drawable d1 = new Drawable() {
        @Override
        public void draw() {
            System.out.println("Anonymous Class: Drawing " + width);
        }
    };

    d1.draw();
}

Here you can see the anonymous class is implementing the Drawable interface, by providing an implementation for the draw method.

  1. We could use a concrete class that implements the Drawable interface
@FunctionalInterface  
interface Drawable{
    public void draw();
}

class ConcreteDrawable implements Drawable {
    @Override
    public void draw() {
        System.out.println("Concrete Class: Drawing");
    }
}

public class LambdaExpressionExample2 {
    public static void main(String[] args) {

        // with concrete class
        Drawable d1 = new ConcreteDrawable();
        d1.draw();
    }
}

In this example, we no longer have access to the local width variable in the draw method. But you can see that the ConcreteDrawable class also provides an implementation of the Drawable interface.

  1. Or finally we can use a lambda function.
public static void main(String[] args) {
    int width = 10;
    
    // with lambda
    Drawable d1 = () -> { System.out.println("Lambda: Drawing " + width); };
    
    d1.draw();
}

Here the lambda function is implementing the Drawable interface, by giving an implementation of the draw method. The only reason why we can use a lambda function here is that the Drawable interface only declares one method.

So in summary, it is not the LambdaExpressionExample2 class that should implement Drawable but instead the lambda function itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM