![]() ![]() String message = exp.myMethod(name) → The lambda expression exp calls the abstract method myMethod() by passing the string name as argument. The method printMessage() takes two parameters: a string name and a lambda expression exp implementing the abstract method of the functional interface myFuncInterface. Public static void printMessage ( String name, myFuncInterface exp ) On calling the method, the body of the lambda expression gets executed and 18 gets returned.ĭidn’t the number of lines reduced and readability increased on using a lambda expression in place of an anonymous inner class for the functional interface implementation? This is one of the reasons we use lambda expressions in Java. In this statement, the reference (variable) v storing the lambda expression calls the method getVotingAge(). Now look at the following statement in the main method. In this way, the lambda expression implements the abstract method of the interface Voting. As a result, the lambda expression () -> 18 also takes no parameter and returns an integer 18. The method getVotingAge() in the functional interface Voting takes no parameter and returns an integer. How does this lambda expression implement the abstract method getVotingAge()? This returned lambda expression is assigned to v. ![]() Here, () -> 18 is a lambda expression that implements the functional interface Voting by implementing the abstract method defined in the functional interface. In the main method, notice the following statement. Let’s first see an example of implementing a functional interface using an anonymous inner class. Using lambda expressions makes code shorter as compared to anonymous inner classes. Lambda expression is a feature introduced in Java 8. Post Java 8: Using lambda expressions instead of anonymous inner classes. Pre Java 8: Using anonymous inner classes. Implementing Abstract Method of Functional InterfaceĪ functional interface can be implemented without creating a class using the following ways. We discussed anonymous inner classes in the chapter Inner classes and we will see how a lambda expression is used to implement an interface in this chapter. To make the program short and concise, we can use an anonymous inner class or a lambda expression to implement a functional interface. If you want to call the method defined by the functional interface at only a few times (once or twice) in the code, then creating a class for implementing the functional interface and then creating an object of the class to call its abstract method will make the code unnecessarily long. Such a long code for calling the abstract method of the functional interface just once in the program! Then in the main method, we created an object v of VotingClass and this object calls the method getVotingAge(). The class VotingClass implements the interface Voting and provides definition (body) to the method getVotingAge(). In this example, the functional interface Voting has an abstract method getVotingAge(). One way to implement it is by creating a class that implements it and provides definition to its abstract method, as shown below. A lambda expression is used to implement the abstract method of a functional interface.Īs we already know that a functional interface is just an interface with a single abstract method. We will cover the use of lambda expressions in both these use cases in detail in this chapter. Implementing the abstract method of a functional interface.There are two main cases in which lambda expressions can be helpful. ![]() ![]() A shorter code is quite desirable when you have a program having thousands of lines of code. Lambda expressions are mainly used because they can make code shorter and more readable. Now let’s come to the main part - significance of lambda expressions and using them in our programs. So these are the different types of lambda expressions that we can define in Java. In the above lambda expression also, there are two statements in the body. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |