What is a functional interface in Java8? Quick explanation and sample.

Any interface with a single abstract method is a functional interface and its implementation may be treated as lambda expression. It can have only a single functionality. However a functional interface can have unlimited default methods. Note, that default methods have implementation and abstract methods – not. The latter can be only one in functional interface.

When defining your own functional interfaces it is recommended to put an annotation @FunctionalInterface. It helps compiler to identify any inconsistencies and show an error during compilation.

There is a number of out of the box functional interfaces defined in Java8. You can check them listed here.

Here is a sample with custom created functional interface and lambda expression used with it:

<pre>public class FunctionalInterfaceSample {

    public static void main(String[] args) {

        printResult(5,x -> x * x);
        printResult(5,x -> x * x * x);
        printResult(6,x -> x * x);


    public static void printResult(Integer s, MyFunction<Integer, Integer> f) {


    public interface MyFunction<T, K> {
        public K getResult(T a);


For more information about Java functional interface please check documentation.


About Danas Tarnauskas

Leave a Reply

Your email address will not be published.