Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing an interface with two abstract methods by a lambda expression

In Java 8 the lambda expression is introduced to help with the reduction of boilerplate code. If the interface has only one method it works fine. If it consists of multiple methods, then none of the methods work. How can I handle multiple methods?

We may go for the following example

public interface I1() {     void show1();     void show2(); } 

Then what will be the structure of the main function to define the methods in the main itself?

like image 679
Soumya Kanti Naskar Avatar asked Mar 26 '16 09:03

Soumya Kanti Naskar


People also ask

CAN interface have 2 abstract methods?

And in any case, the specification says a functional interface cannot have more than one abstract method. So an interface with two abstract methods cannot be used by specification.

Can lambda function have multiple methods?

Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method.

Can we use lambda expression with abstract class?

Java 8 Lambda Expression can create a method argument or consider the entire code as data. It can implement a function that does not have any specific class, which means an abstract class. This expression can be passed to the next object and can be executed whenever required.


1 Answers

Lambda expressions are only usable with functional interface as said by Eran but if you really need multiple methods within the interfaces, you may change the modifiers to default or static and override them within the classes that implement them if necessary.

public class Test {     public static void main(String[] args) {         I1 i1 = () -> System.out.println(); // NOT LEGAL         I2 i2 = () -> System.out.println(); // TOTALLY LEGAL         I3 i3 = () -> System.out.println(); // TOTALLY LEGAL     } }  interface I1 {     void show1();     void show2(); }  interface I2 {     void show1();     default void show2() {} }  interface I3 {     void show1();     static void show2 () {} } 

Inheritance

You shouldn't forget the inherited methods.

Here, I2 inherits show1 and show2 and thus can not be a functional interface.

public class Test {     public static void main(String[] args) {         I1 i1 = () -> System.out.println(); // NOT LEGAL BUT WE SAW IT EARLIER         I2 i2 = () -> System.out.println(); // NOT LEGAL     } }  interface I1 {     void show1();     void show2(); }  interface I2 extends I1 {     void show3(); } 

Annotation

To make sure your interface is a functional interface, you may add the following annotation @FunctionalInterface

@FunctionalInterface <------- COMPILATION ERROR : Invalid '@FunctionalInterface' annotation; I1 is not a functional interface interface I1 {     void show1();     void show2(); }  @FunctionalInterface interface I2 {     void show3(); } 
like image 198
Yassin Hajaj Avatar answered Sep 29 '22 20:09

Yassin Hajaj