Code Pumpkin

Functional Interface | Java8

April 22, 2017
Posted by Dipen Adroja

In this article, we are going to discuss about one of the Java8 feature: Functional Interface. Java 8 is released in March 2014. You can download JDK 1.8 from here based on your OS Platform requirements.

Java developer around the world frequently uses the Runnable, Comparator, Callable etc. One common thing about the above mentioned interfaces is they have only one method declared to be implemented. These interfaces are called Single Abstract Method(SAM). It is normally implemented using anonymous class as below:



public class Sample {

	public static void main(String[] args){
		
		Thread th= new Thread(
				new Runnable() {
			@Override
			public void run() {
				System.out.println("Running in new thread");
			}
		});
		th.start();
	}
}

Now, in java8, they have introduced functional interfaces which can be represented using Lambda Expressions(Lambda expression explained in article Lambda Expression | Java8).There’s an annotation introduced- @FunctionalInterface which can be used for compiler level errors when the interface you have annotated is not a valid Functional Interface.

Below is example of how we can declare functional interface.



@FunctionalInterface
public interface MyComparator {

	public boolean compareMyValues(int a, int b);
}

Here, if the declared interface is not of type functional the compiler will give error. An interface can have one abstract method excluding the methods inherited from java.lang.Object class(methods such as equals and toString) and any number of default methods can be called as functional interface. If an interface extends other interface, which is functional interface and it does not include any other abstract methods, then the new interface is also called functional interface.

We will also see a small example of how we can use lambda expressions in place of anonymous inner class.



public class Sample {

	public static void main(String[] args){
		MyComparator mycomp = (a,b) -> a>b;
			
		System.out.println("Is 4 is greater than 5: "+ mycomp.compareMyValues(4, 5));
	}
}

In above example, we have passed lambda implementation of the interface as a object and used it latter in the code. Functional interface is just recreation of the concept of Single Abstract Method, which can be implemented using lambda expression as well.

This is the brief introduction of the Functional Interface which can be implemented using Lambda expressions. If you are new to Lambda expressions, you can go to article Lambda Expression | Java8 for more details.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate â‚ą42 per Tree.



About the Author


Coder, Blogger, Wanderer, Philosopher, Curious pumpkin



Tags: , ,


Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
</code></pre>
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook