Functional Interface Of Java
One of the major feature release of Java programming language development is Java 8. It is released on 18th March 2014. The new update of JAVA 8 includes new functionality, upgrades and bug fixes to increase the performance of Java programs in development and running. In respect to the release of Java 8, Java provided supports for functional programming, new Java Script engine, new APIs for date time manipulation, new streaming API, etc. “Functional Interface” is one of the feature of Java 8. Functional interface is the interface with one abstract method. They can have only one functionality to exhibit. We can use predefined functional interfaces provided by Java or create your own functional interface and use it. Functional Interface serves as a data type for Lambda expressions. Since a Functional interface contains only one abstract method, the implementation of that method becomes the code that gets passed as an argument to another method. Predefined functional interface is also known as Single Abstract Method or SAM this is because it only contains one abstract method.
Important points and observation
- @FunctionalInterface annotation used to specify an interface as Functional Interface.
- This annotation prevents another abstract method to it. if you try to add another abstract method,then compiler throws an error.
- It is safer to use the annotation to define explicitly it is a Functional Interface,actually This annotation is OPTIONAL.
- You can pass interfaces as parameters like in functional programming languages That’s why it called as “functional interface”.
Advantages of Functional Interfaces
- Using Functional interface we can add a method to an interface without breaking its implementations and it helps in maintaining backward compatibility in applications.
- Functional interfaces are for lambda expressions, method references, and constructor references, they can still be used, like any interface, with anonymous classes, implemented by classes, or created by factory methods.
Java 8 contains many built-in functional interfaces like-
- Predicate: It has an abstract method test which gives a Boolean value as a result for the specified argument. Its prototype is
- Binary Operator: It has an abstract method apply which takes two argument and returns a result of same type.
- Function: It has an abstract method apply which takes argument of type T and returns a result of type R.
In this blog we have gone through the new @
annotation introduced in Java 8. Want to know the key skills required for Java developers ?! Stick into our blog …FunctionalInterface