This page was exported from TechnicalStack [ http://technicalstack.com ]
Export date: Wed Oct 17 8:41:41 2018 / +0000 GMT

Functional Interface



An Interface which has only one abstract method is known as Functional Interface.

It can have any number of default and static method but only one abstract method is allowed.

@FunctionalInterface is the annotation used to forcefully tell the compiler that the interface is a FunctionalInterface.

Sample code:

package com.technicalstack.basic;

@FunctionalInterface

public interface InterfaceFun {

public abstract void f1();

default void f2(){

       }

static void f3(){

       }

}

If we compile above code ,it compiles sucessfuly.

now if we add another function to the above one with @FunctionalInterface annotation then the compiler will show below error in eclipse:

Invalid '@FunctionalInterface' annotation; InterfaceFun is not a functional interface InterfaceFun.java 

If we try to compile the same class using command prompt,it will show below error:

cmd: >javac InterfaceFun.java

InterfaceFun.java:3: error: Unexpected @FunctionalInterface annotation

@FunctionalInterface

^

InterfaceFun is not a functional interface

multiple non-overriding abstract methods found in interface InterfaceFun

1 error

So here @FunctionalInterface annotation also performs work of validating the functional interface.

 

 


Post date: 2018-08-23 09:24:38
Post date GMT: 2018-08-23 09:24:38
Post modified date: 2018-08-23 09:37:33
Post modified date GMT: 2018-08-23 09:37:33

Powered by [ Universal Post Manager ] plugin. MS Word saving format developed by gVectors Team www.gVectors.com