OCJP Exam watch tip #14

Capture

For an interface to be treated as a functional interface, it should have only one abstract method. However, it may have any number of default or static methods defined in it. Let us see an example from the Java library to understand this.
Here is the definition of java.util.function.IntConsumer interface (without annotations and javadoc comments):

    public interface IntConsumer {
        void accept(int value);
        default IntConsumer andThen(IntConsumer after) {
                Objects.requireNonNull(after);
                return (int t) -> { accept(t); after.accept(t); };
        }
    }

    Though this interface has two members, andThen method is a default method and only accept method is an abstract method. Hence, IntConsumer interface is a functional interface.  Go through this article for further reading on Java 8 – Functional Interfaces https://dzone.com/articles/java-8-functional-interfaces-sam

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s