JavaRush /Java Blog /Random EN /Introduction to Functional Interfaces
minuteman
Level 32

Introduction to Functional Interfaces

Published in the Random EN group
Please don’t troll too much, I’m just starting to translate articles Introduction to Functional Interfaces - 1

Introduction to Functional Interfaces - Concepts Recreated in Java 8

Every Java developer in the world has used one of the following interfaces at least once: java.lang.Runnable, java.awt.event.ActionListener, java.util.Comparator, java.util.concurrent.Callable. They all have one thing in common, and that is that they all have only one method. There are many other such interfaces in the JDK, as well as others created by Java developers. These interfaces are also known as Single Abstract Method interfaces(SAM interfaces). One of the most popular ways to use these interfaces is to create anonymous inner classes that use these interfaces, as in the following example:
public class AnonymousInnerClassTest {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("A thread created and running...");
            }
        }).start();
    }
}
In Java 8, the SAM concept is recreated and called functional interfaces. They can be represented using lambda expressions, method references, and reference constructors. A new @FunctionalInterface annotation has been created which is used to throw errors at the compiler level when the interface you annotated does not work at the functional level. Let's look at a simple functional interface with one abstract method:
@FunctionalInterface
public interface SimpleFunInterface {
    public void doWork();
}
An interface can also declare abstract methods from a class java.lang.Object, but in this case the interface can also be declared functional:
@FunctionalInterface
public interface SimpleFuncInterface {
    public void doWork();
    public String toString();
    public Boolean equals(Object o);
}
As soon as you have added another abstract method to the interface, the IDE will mark it as erroneous as in the picture: Introduction to Functional Interfaces - 2 An interface can inherit from another interface; if the interface inherits from a functional interface and does not contain new abstract methods, then this interface is also functional. But an interface can only contain one abstract method and many default methods, and it will still be considered functional.
@FunctionalInterface
public interface ComplexFunctionalInterface extends SimpleFuncInterface {
    default public void doSomeWork() {
        System.out.println("Doing some work in interface impl...");
    }
    default public void doSomeWork() {
        System.out.println("Doing some other work in interface impl...");
    }
}
The top example is still a functional interface. Now let's look at how we can use lambda expressions to replace an anonymous inner class to implement functional interfaces:
/*
*Implementation the interface by creating an
*anonymoous inner class versus using
*lambda expression.
*/
public class SimpleFunInterfaceTest {
    public static void main(String[] args) {
        carryOutWork(new SimpleFunInterface() {
            @Override
            public void doWork() {
                System.out.println("Do work in SimpleFun impl...");
            }
        });
        carryOutWork(() -> System.out.println("Do work in lambda exp impl..."));
    }
    public static void carryOutWork(SimpleFuncInterface sfi) {
        sfi.work();
    }
}
The result of the program will be as follows:
Do work in SimpleFun impl...
Do work in lambda exp impl...
In case you are using an IDE that supports Java lambda expression syntax (Netbeans 8 Nightly builds) - You get a hint when using anonymous inner classes: Introduction to Functional Interfaces - 3 This was a brief introduction to the concept of functional interfaces in Java 8 and how they can be implemented using lambda expressions.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION