Reputation: 65
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();
}
}
Upvotes: 1
Views: 1194
Reputation: 101
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.
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.
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.
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.
Upvotes: 0
Reputation: 1375
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.
Upvotes: 1