What is a functional interface? What are some functional interfaces in the Java API? How can you declare that an interface is functional?
- [Instructor] Hi, welcome to this video, introducing the notion of functional interface. So in this video, we're going to see what is a functional interface, we're going to see some examples of these interfaces, and we're going to introduce the corresponding new annotation called functional interface. So what's a functional interface? This is an extremely simple concept. It's just any interface having a single abstract method, which means that interface can also have many static and default methods as long as it only has one abstract method.
For instance, our scalable interface from our previous video is a functional interface because it has a single abstract method called setScale. Also, in the Java API, even before versions 8 and 9, there were a number of functional interfaces and you may have heard of them. The most well-known ones are Runnable, which is used in multi-threading, Comparable and Comparator, which are used to sort objects, and Iterable which is a very important interface in the Java collection framework.
So for instance, the only abstract method in Runnable is called run, and it's the entry point for a thread and the only abstract method in Iterable is called iterator, and it's a factoring method that returns a new iterator. Also, some functional interfaces are pure. So here by pure, we mean the same meaning that we saw when introducing functioninal programming, so pure in the sense of stateless.
In particular, a pure functional interface is intended to be implemented by stateless classes, so classes with no instance fields. Let's see some examples. Our scalable interface is not pure because classes implementing it are not expected to be stateless. There are going to be some graphical items and these graphical items will most likely have a position and most certainly a size.
Also, Runnable is not pure because classes implementing it are most likely to have instance fields. On the other hand, Comparator is a pure functional interface because usually classes implementing Comparator have no instance fields because they have no states to remember. Notice that this class of functional interfaces, pure functional interfaces, respect the main property of the functional programming paradigm, which is being stateless.
Hence, we will see that they play an important role in conjunction with a new library in the Java API which is called streams, and it's the object of the second volume of this course. These functional interfaces come with a corresponding new annotation, which of course is called FunctionalInterface. This annotation is intended only for pure functional interfaces, and the concrete effect of this annotation is that the compiler is going to check the single abstract method property and the compiler is going to raise a compile time error if the interface annotated with this annotation has more than one abstract method.
So clearly the compiler cannot check that the interface is actually a pure functional interface because that's not a syntactic property of the interface. When you look at the interface, you cannot tell from the code alone whether the interface is pure or not because it's not a property of the code; it's a property of the contract of that interface, so it's usually written in the documentation. And that's what the annotation is there for. It's a semi-formal way of specifying that an interface is actually a pure functional interface.
For instance, Comparator is annotated with it, whereas Comparable is not. And in the Java 9 API, we can find more than 40 pure functional interfaces. They're all contained in the same package java.util.function and they are discussed in detail in a later section. For the moment, let's just, as a preview, consider two simple examples from this package.
First, an interface representing the function accepting an object. The interface is called Consumer, so it's just one method, accept T and returns nothing. Second example. An interface representing a function that produces an object, so it's the dual of the first one. The interface is called Supplier of T, and it has a single abstract method called get, which accepts nothing and returns an object of type T.
So in this video we have seen what's a functional interface, what's a pure functional interface, and we have introduced the corresponding annotation which applies to pure functional interfaces. That's all for this video. Let's move on to the next section on lambda expressions.
Note: To get the best results from this course, you should be familiar with basic Java programming concepts, including lists, maps, and sets, and have a suitable IDE, such as Eclipse, NetBeans, or IntelliJ.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- What is functional programming?
- What are functional interfaces?
- Writing lambda expressions
- Creating functional interfaces
- Composing functions
- Sequential data processing with streams
- Using parallel streams