Functional Interfaces In JAVA8
What is Functional Interface ?
Functional Interface is a new concept that is introduced in Java 8 as part of Project Lambda. If you have worked with Interfaces in Java previous versions, you might think all the methods declared in Interface are public and abstract implicitly. Although this was true prior to JDK 8, but beginning with JDK 8 it is also possible to specify default behavior for a method declared in Functional Interface. This is called default method.
All the non default methods in Functional Interface are implicitly abstract and you don't need to specify modifier "abstract" before the method name.
The existing Interfaces such as Runnable, ActionListener are Functional Interfaces in Java8.
Functional Interfaces are also considered as Type for Lambda Expressions. They are sometimes referred to as SAM types, where SAM stands for Single Abstract Method.
How To Define Functional Interface ?
Use "@FunctionalInterface" Annotation while creating a custom Functional Interface. Here is an example.
In the above example, the method print() is implicitly abstract and it is the only abstract method defined in SimpleFI interface.
Advantages Of Functional Interfaces Over Normal Interfaces :
You can implement Functional Interfaces just like implementing normal Interfaces as shown below.
You can also implement Functional Interfaces using Lambda Expressions. Here is how to.
An Example Of Functional Interface With default and Abstract Methods From Object Class :
Along with single abstract method, a Functional Interface can also have default methods and abstract methods from Super class java.lang.Object.
Generic Functional Interfaces :
A Functional Interface associated with a Lambda Expressions can be generic. If you want to use a Functional Interface for various types of parameters such as String or Integer, it is good choice to make it as Generic one. Just like Generic Classes, creating Functional Interfaces is simple. Here is the code.
When To Create Functional Interface ?
If you want to frequently instantiate a type using an anonymous class, it is good decision to make that interface as Functional Interface with @FunctionalInterface.
This is all about Functional Interfaces. If you know much about them, please share in comments section. I will enhance this post based on your helpful comments.
Functional Interface is an Interface which specifies only one abstract method.
Functional Interface is a new concept that is introduced in Java 8 as part of Project Lambda. If you have worked with Interfaces in Java previous versions, you might think all the methods declared in Interface are public and abstract implicitly. Although this was true prior to JDK 8, but beginning with JDK 8 it is also possible to specify default behavior for a method declared in Functional Interface. This is called default method.
All the non default methods in Functional Interface are implicitly abstract and you don't need to specify modifier "abstract" before the method name.
The existing Interfaces such as Runnable, ActionListener are Functional Interfaces in Java8.
Functional Interfaces are also considered as Type for Lambda Expressions. They are sometimes referred to as SAM types, where SAM stands for Single Abstract Method.
How To Define Functional Interface ?
Use "@FunctionalInterface" Annotation while creating a custom Functional Interface. Here is an example.
@FunctionalInterface public interface SimpleFI { void print(); }
In the above example, the method print() is implicitly abstract and it is the only abstract method defined in SimpleFI interface.
Advantages Of Functional Interfaces Over Normal Interfaces :
- You can define method definition inside Interface using default modifier which helps to modify existing Interface with out breakage of application.
- As mentioned earlier, they serves as type for lambda expressions which are used to pass behavior around in a compact way.
You can implement Functional Interfaces just like implementing normal Interfaces as shown below.
package com.speakincs.funcinterfaces; public class FiIMP implements SimpleFI{ @Override public void print() { System.out.println("Welcome To Speakingcs"); } public static void main(String[] args) { FiIMP sl = new FiIMP(); sl.print(); } }
You can also implement Functional Interfaces using Lambda Expressions. Here is how to.
package com.speakincs.funcinterfaces; public class FirstLambda { public static void main(String[] args) { SimpleFI sf = () -> {System.out.println("Hi SpeakingCs");}; sf.print(); } }
An Example Of Functional Interface With default and Abstract Methods From Object Class :
Along with single abstract method, a Functional Interface can also have default methods and abstract methods from Super class java.lang.Object.
A Functional Interface can contain any public method defined by Object, such as equals(), without affecting its "functional interface" status. The public Object methods are considered implicit members of a Functional Interface because they are automatically implemented by an instance of Functional Interface.
package com.speakincs.funcinterfaces; @FunctionalInterface public interface SimpleFI { void print(); default void printHello() { System.out.println("Hello From Simple FI"); } String toString(); boolean equals(Object obj); }
Generic Functional Interfaces :
A Functional Interface associated with a Lambda Expressions can be generic. If you want to use a Functional Interface for various types of parameters such as String or Integer, it is good choice to make it as Generic one. Just like Generic Classes, creating Functional Interfaces is simple. Here is the code.
package com.speakincs.funcinterfaces; @FunctionalInterface public interface GenericFI< T > { T func(T t); }
When To Create Functional Interface ?
If you want to frequently instantiate a type using an anonymous class, it is good decision to make that interface as Functional Interface with @FunctionalInterface.
This is all about Functional Interfaces. If you know much about them, please share in comments section. I will enhance this post based on your helpful comments.
Comments
Post a Comment