Skip to main content

Posts

Showing posts with the label Functional Interface

Java 8 - What are functional interfaces in Java?

Java 8 reincarnated SAM interfaces and termed them Functional interfaces. Functional interfaces have single abstract method and are eligible to be represented with Lambda expression . @FunctionalInterface annotation is introduced in Java 8 to mark an interface as functional. It ensures at compile-time that it has only single abstract method, otherwise it will throw compilation error. Let's define a functional interface. @FunctionalInterface public interface Spec<T> { boolean isSatisfiedBy(T t); } Functional interfaces can have default and static methods in them and still remains functional interface. @FunctionalInterface public interface Spec<T> { boolean isSatisfiedBy(T t); default Spec<T> not() { return (t) -> !isSatisfiedBy(t); } default Spec<T> and(Spec<T> other) { return (t) -> isSatisfiedBy(t) && other.isSatisfiedBy(t); } default Spec<T> or(Spec<T> other) { return (t) -> isSa