{"id":2854,"date":"2018-09-15T11:39:32","date_gmt":"2018-09-15T11:39:32","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2854"},"modified":"2019-03-18T12:51:09","modified_gmt":"2019-03-18T12:51:09","slug":"what-are-functional-interface-how-it-works","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/what-are-functional-interface-how-it-works\/","title":{"rendered":"What are Functional Interface? How it works?"},"content":{"rendered":"<p>Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method compareTo is used for comparison purpose<br \/>\nFunctional Interface is an interface which has one and only one abstract method. Apart from abstract method, it can have any number of default and static methods which have an implementation and are not abstract and overridden method from Object.These interfaces are also called Single Abstract Method Interfaces. Few Functional Interfaces are Comparable, Runnable and etc.<\/p>\n<p>Example of Functional Interface<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@FunctionalInterface\r\npublic interface MyFunctionalInterface \r\n{\r\n\tpublic void MethodOne(int i, double d);\r\n} \r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@FunctionalInterface\r\npublic interface MyFunctionalInterface \r\n{\r\n\tpublic void MethodOne(int i, double d);\r\n\t\r\n\tdefault boolean methodTwo(String value) \r\n\t{\r\n        return true;\r\n  }\r\n} \r\n<\/pre>\n<p>@FunctionalInterface annotation is used to mark an interface as Functional Interface<br \/>\nnot mandatory to use it. If the interface is annotated with @FunctionalInterface annotation and when we<br \/>\ntry to have more than one abstract method, it throws the compiler error.<\/p>\n<p>There are two ways the abstract method definition in the functional interface could be done <\/p>\n<p>One is by Anonymous Inner class and other is by Lambda Expression<\/p>\n<p>For example in Java, if we have to instantiate runnable interface anonymously, then our code looks like below. It&#8217;s bulky<\/p>\n<p><strong>Anonymous Inner class way of method definion for Functional Interface<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nRunnable r = new Runnable(){\r\n @Override\r\n public void run() \r\n {\r\n\tSystem.out.println(&quot;My Runnable&quot;);\r\n }};\r\n<\/pre>\n<p>lambda expressions for the above method implementation is<\/p>\n<p><strong>Lambda Expressions way of method definion for Functional Interface<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nRunnable r1 = () -&gt; {\r\n System.out.println(&quot;My Runnable&quot;);\r\n};\r\n<\/pre>\n<p>Functional interface with abstract method(oneMethod) and default(getMulty), static methods(getSum) which have an implementation and are not abstract and methods overridden from Object Class(toString and equals).<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@FunctionalInterface\r\npublic interface MyFunctionalInterface \r\n{\r\n\tpublic void oneMethod(int i, double d);\r\n\tpublic String toString();\r\n\tpublic boolean equals(Object o);\r\n\r\n\tpublic static int getSum(int a,int b)\r\n        {\/\/ valid-&gt;method static\r\n\t\treturn a+b;\r\n\t}\r\n\r\n\tpublic default int getMulty(int c,int d)\r\n        {\/\/valid-&gt;method default\r\n\t\treturn c+d;\r\n        }\r\n}\r\n<\/pre>\n<p>Functional Interface could be classified into the following 5 Types based on the parameters and the way the abstract method behaves<\/p>\n<ol>\n<li>Supplier<\/li>\n<li>Consumer<\/li>\n<li>Predicate<\/li>\n<li>Function<\/li>\n<li>Operator<\/li>\n<\/ol>\n<table border=\"1px solid black\">\n<thead>\n<tr>\n<th>Functional Interface<\/th>\n<th>Parameter Types<\/th>\n<th>Return Type<\/th>\n<th>Abstract Method Name<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tr>\n<td>Runnable<\/td>\n<td>none<\/td>\n<td>void<\/td>\n<td>run<\/td>\n<td nowrap=\"nowrap\">Runs an action without arguments or return value<\/td>\n<\/tr>\n<tr>\n<td>Supplier<br \/>\n\t\t\t\t\t\t\t<T>\n\t\t\t\t\t\t\t<\/td>\n<td>none<\/td>\n<td>T<\/td>\n<td>get<\/td>\n<td>Supplies a value of type\u00a0T<\/td>\n<\/tr>\n<tr>\n<td>Consumer<br \/>\n\t\t\t\t\t\t\t\t<T>\n\t\t\t\t\t\t\t\t<\/td>\n<td>T<\/td>\n<td>void<\/td>\n<td>accept<\/td>\n<td>Consumes a value of type\u00a0T<\/td>\n<\/tr>\n<tr>\n<td>BiConsumer<br \/>\n\t\t\t\t\t\t\t\t\t<T, U>\n\t\t\t\t\t\t\t\t\t<\/td>\n<td>T,\u00a0U<\/td>\n<td>void<\/td>\n<td>accept<\/td>\n<td>Consumes values of types\u00a0T\u00a0and\u00a0U<\/td>\n<\/tr>\n<tr>\n<td>Function<br \/>\n\t\t\t\t\t\t\t\t\t\t<T, R>\n\t\t\t\t\t\t\t\t\t\t<\/td>\n<td>T<\/td>\n<td>R<\/td>\n<td>apply<\/td>\n<td>A function with argument of type\u00a0T<\/td>\n<\/tr>\n<tr>\n<td>BiFunction<br \/>\n\t\t\t\t\t\t\t\t\t\t\t<T, U, R>\n\t\t\t\t\t\t\t\t\t\t\t<\/td>\n<td>T,\u00a0U<\/td>\n<td>R<\/td>\n<td>apply<\/td>\n<td>A function with arguments of types\u00a0T\u00a0and\u00a0U<\/td>\n<\/tr>\n<tr>\n<td>UnaryOperator<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t<T>\n\t\t\t\t\t\t\t\t\t\t\t\t<\/td>\n<td>T<\/td>\n<td>T<\/td>\n<td>apply<\/td>\n<td>A unary operator on the type\u00a0T<\/td>\n<\/tr>\n<tr>\n<td>BinaryOperator<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<T>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<\/td>\n<td>T,\u00a0T<\/td>\n<td>T<\/td>\n<td>apply<\/td>\n<td>A binary operator on the type\u00a0T<\/td>\n<\/tr>\n<tr>\n<td>Predicate<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t<T>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/td>\n<td>T<\/td>\n<td>boolean<\/td>\n<td>test<\/td>\n<td>A Boolean-valued function<\/td>\n<\/tr>\n<tr>\n<td>BiPredicate<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<T, U>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/td>\n<td>T,\u00a0U<\/td>\n<td>boolean<\/td>\n<td>test<\/td>\n<td>A Boolean-valued function with two arguments<\/td>\n<\/tr>\n<\/table>\n<p><strong>What is need for Default Method in Functional Interface?<\/strong><\/p>\n<ol>\n<li>If we want to add additional methods in the interfaces, it will require change in all the implementing classes. <\/li>\n<li>As interface grows old, the number of classes implementing it might grow to an extent that its not possible to extend interfaces.<\/li>\n<li> That&#8217;s why when designing an application, most of the frameworks provide a base implementation class and then we extend it and override methods that are applicable for our application.<\/li>\n<li>\u201cDefault Method\u201d or  Virtual extension methods or Defender methods feature, which allows the developer to add new methods to the interfaces without breaking their existing implementation. It provides the flexibility to allow interface to define implementation which will use as the default in a situation where a concrete class fails to provide an implementation for that method.<\/li>\n<\/ol>\n<p>Lets Imagine we have UserDevices which later wants to provide support for blackberry devices at later part of Software release. You cannot have a abstract method for blackberrySupport and make the implementing classes to do method definition.Instead of that I am writing as default method in interface which prevents all the implementing classes to write its own method definition.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface UserDevices {\r\n    default void blackberrySupport(){\r\n       System.out.println(&quot;Support for Blackberry Devices&quot;);\r\n    }\r\n}\r\n\r\npublic class Device implements UserDevices {\r\n}\r\n<\/pre>\n<p><strong>What if the class implements two interfaces and both those interfaces define a default method with the same signature?<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface UserDevices1 {\r\n    default void blackberrySupport(){\r\n       System.out.println(&quot;Support for Blackberry Devices1&quot;);\r\n    }\r\n}\r\n\r\npublic interface UserDevices2 {\r\n    default void blackberrySupport(){\r\n       System.out.println(&quot;Support for Blackberry Devices2&quot;);\r\n    }\r\n}\r\n\r\npublic class Device implements UserDevices1 , UserDevices2 {\r\n}\r\n<\/pre>\n<p>This code fails to compile with the following result:<\/p>\n<pre>\r\njava: class Device inherits unrelated defaults for blackberrySupport() from types UserDevices1 and UserDevices2 \r\n<\/pre>\n<p>In this case we have to resolve it manually by overriding the conflicting method<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Device implements UserDevices1, UserDevices2  {\r\n    public void blackberrySupport(){\r\n       UserDevices1.super.blackberrySupport();\r\n    }\r\n}\r\n<\/pre>\n<p>The Best Example of Default Method is addition of foreach method in java.util.List Interface.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method compareTo is used for comparison purpose Functional Interface is an interface which has one and only one abstract method. Apart from abstract method, it can have any number of default and static methods which have an implementation and&hellip; <a href=\"https:\/\/codethataint.com\/blog\/what-are-functional-interface-how-it-works\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[250],"tags":[],"class_list":["post-2854","post","type-post","status-publish","format-standard","hentry","category-lambdas"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2854"}],"version-history":[{"count":12,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2854\/revisions"}],"predecessor-version":[{"id":3249,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2854\/revisions\/3249"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}