{"id":125,"date":"2016-09-05T11:26:00","date_gmt":"2016-09-05T11:26:00","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=125"},"modified":"2021-01-26T18:48:13","modified_gmt":"2021-01-26T13:18:13","slug":"java-8-functional-interface-example","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-8-functional-interface-example\/","title":{"rendered":"Java 8 functional interface example"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">In this post , we are going to see about functional interface in java. It is closely related to <a href=\"https:\/\/java2blog.com\/lambda-expressions-in-java-8\/\" rel=\"noopener noreferrer\" target=\"_blank\">java lambda expressions<\/a>.<br \/>\n<b>Functional interfaces<\/b> are those interfaces which have only one abstract method, it can have default methods, static methods and it can also override java.lang.Object class method.<br \/>\nThere are many functional interfaces already present.<br \/>\n<b>For example:<\/b> Runnable , Comparable.<br \/>\nYou can implement functional interfaces using <a href=\"http:\/\/www.java2blog.com\/2014\/06\/lambda-expressions-in-java-8.html\" target=\"_blank\" rel=\"noopener noreferrer\">lambda expressions<\/a>.<br \/>\nLet&#8217;s understand with classic example of Runnable:<br \/>\nWhen we need to create a Thread and pass an anonymous <a href=\"https:\/\/java2blog.com\/java-runnable-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">Runnable interface<\/a>, we can do it as follow.<\/p>\n<pre name=\"code\">Thread t1=new Thread(new Runnable() { \n   @Override\n   public void run() {\n    System.out.println(\"In Run method\");\n   }\n  });<\/pre>\n<p>Above method is old method of creating thread. As we have single abstract method in Runnable interface , we can consider it as functional interface, hence we can make use of lambda expression.<\/p>\n<pre name=\"code\">\/\/ Using lambda expression\nThread t1=new Thread(\n()->System.out.println(\"In Run method\")\n);<\/pre>\n<p>Lets see an example of functional interface,<\/p>\n<pre name=\"code\">@FunctionalInterface\npublic interface Decorable {\n\n \/\/ one abstract method\n void decorateWithCurtains();\n\n \/\/ default method\n default void decorateWithPaints()\n {\n  System.out.println(\"Decorating using paints\");\n }\n\n \/\/ Overriding method of java.lang.Object\n @Override\n public int hashCode();\n\n}<\/pre>\n<div style=\"text-align: justify;\">\n<p>Java can itself identify Functional Interface but you can also denote interface as Functional Interface by annotating it with @FunctionalInterface. If you annotate @FunctionalInterface, you should have only one abstract method otherwise you will get compilation error.<\/p>\n<pre name=\"code\">package org.arpit.java2blog;\n\npublic class DecorableMain {\n\n    public static void main(String[] args) {\n                \/\/ Using lambada expression\n        Decorable dec=()->{System.out.println(\"Decorating with curtains\");};\n        dec.decorateWithCurtains();\n    }\n}<\/pre>\n<p>When you run above program, you will get below output:<\/p>\n<div class=\"content-box-green\">Decorating with curtains<\/div>\n<p>That&#8217;s all about Java 8 Functional interface example.\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this post , we are going to see about functional interface in java. It is closely related to java lambda expressions. Functional interfaces are those interfaces which have only one abstract method, it can have default methods, static methods and it can also override java.lang.Object class method. There are many functional interfaces already present. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12599,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[36],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/125"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/12599"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}