Monday 23 June 2014

Functional Interface

A Functional interface is the one which has only one abstract method. The method declared in functional interface must not a public member of Object class.

A simple example of a functional interface is:

interface Runnable {
void run();
}

The following interface is not functional because it declares nothing which is not already a member of Object.

interface NonFunc {
boolean equals(Object obj);
}

However, its subinterface can be functional by declaring an abstract method which is not a member of Object.

interface Func extends NonFunc {
int compare(String o1, String o2);
}

Similarly, the well known interface java.util.Comparator<T> is functional because it has one abstract non-Object method.

interface Comparator<T> {
boolean equals(Object obj);
int compare(T o1, T o2);
}

1. Both Interface1 and Interface2 are functional interfaces
@FunctionalInterface
interface Interface1{
Object getObject();
}

@FunctionalInterface
interface Interface2 extends Interface1{
String getObject();
}

Program compiles fine. Since return types are covariantly equivalent, Interface2 Overrides the method in Interface1.



Related Links




                                                             Home

No comments:

Post a Comment