0

The functional interface Function in Java 1.8 implements compose() as shown:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    Objects.requireNonNull(before);
    return (V v) -> apply(before.apply(v));
}

My understanding of lambda expressions (possibly incorrect) is that the above return statement is syntactic sugar for:

    return new Function<V, R>() {
        R apply(V v) {
            return apply(before.apply(v));
        }};

This statement would be illegal because, as an interface, Function<V, R> should not be instantiable. So how does the above method work?

1 Answer 1

2

No, it's not syntactic sugar for an anonymous class.

And even if it was, the code you posted doesn't instantiate an interface. It instantiates an anonymous class which implements that interface.

Read the Java tutorial about anonymous inner classes.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, right. We're providing the one non-default function. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.