From the course: Java: Lambdas and Streams
Functional interfaces - Java Tutorial
From the course: Java: Lambdas and Streams
Functional interfaces
- [Instructor] Lambdas are a way of using functional programming to write concise and powerful code in Java. And the first thing you need to know about using Lambdas is that they implement something called functional interfaces. So let's start off with seeing what functional interfaces look like. So I'm going to create one by right clicking on com.example, and going to new Java class. And I'm going to select the interface option, and I'm going to call this Greeting. So now I've got my interface, and I need to add one single method to it to make it a functional interface. So this method is just going to print out some text to the console. So it's going to be a void return type, and I'll call it, say hello. And I don't need to implement the body of the method here, because this is in the interface. It's also a good practice to add the functional interface annotation to the class. So above where it says public interface greeting, I'm going to add @functionalInterface. This doesn't really look like it does anything, but under the hood, it's basically making sure that I can't add any more methods. So let's say someone came along later, and they wanted to add a say goodbye method. And they did void say goodbye. Now I've got a compiler warning that basically tells me I can't have more than one method in this interface, because it's a functional interface. So I'm going to go ahead and remove that say goodbye method again. Now the next step is to implement this functional interface. The best and quickest way is to do this with a Lambda. But first, I'm going to do it in the long hand object oriented way. This is because it's easier to understand this way to begin with. And if you understand this bit, then you'll understand what Lambdas are doing under the hood and how they work. So to create my concrete implementation, I'm going to right click on com.example again and go to new Java class. And I'm going to call this one Hello Greeting. And this one needs an implement greeting added to it to make sure that it implements my greeting interface. So now I need to override the say hello method. So I'm going to do public void say hello. And I'm just going to put a system.out.printline with the phrase, hello world in it. And there's a handy IntelliJ shortcut for this, which is SOUT and then Tab. And that will fill out system.out.printline for me. And in the brackets, I'm just going to put hello world. So now I've got my functional interface, and my concrete implementation of it. And now I just need somewhere to run my code. So I'm going to create one more Java class by right clicking on com.example again and going to new Java class. And I'm going to call this one Main. Now I want a public static void main method in here to run some code. So I'm going to use another handy shortcut, which is PSVM and then Tab. And that will fill out public static void main for me. And now I'm going to create greeting object. So I'm going to do greeting, and let's call it Greeting. And then I'm going to do equals new hello greeting. And then I can do greeting.sayhello to call my say hello methods. And this should print out my hello world message to the console. So I'm going to run this now by clicking on the green arrow on the left, and clicking on run main domain. And I can see down in the console here, that it prints out my message, hello world, as expected. So that's one way of implementing the interface, but it was quite a lot of code for something quite small. I mean, it's just a single method, and I had to create an interface and then a class to implement it. But because it's a functional interface, there's actually a quicker way of doing this. I didn't actually need to create the class, hello greeting. Instead, I can just create a new instance of greeting right here in the main method, and override the say hello method right here. So let's see what that looks like. So I'm going to do greeting, and let's call this new one Greeting2, and then I'm going to do equals new greeting. And then IntelliJ again has a shortcut which will fill out the rest for me. So I'm going to press Enter, and it adds in the overridden say hello method for me. So I'm going to put in the same content as in my original hello greeting one. So I'm going to do system.out.printline, and pass in hello world again. And then I just need to add a semicolon after this curly brace. Then I can call my say, rephrase. Then I can call my say hello method in the same way. So I'm going to do greeting2.sayhello. And let's run this again by clicking on the green arrow again. And now I can see in the console, I've got two hello world messages. We can actually now make this even shorter by using a Lambda instead. So in the next video, I'm going to show you how to make this cleaner and more concise by using Lambdas.