Allow lambdas to implement abstract classes

Howard Lovatt howard.lovatt at
Thu Feb 28 17:54:30 PST 2013

Playing round with a toy streams library I have come across an inefficiency
doe to lambdas only implementing interfaces, e.g.:

@FunctionalInterface public interface IntStream {
  int next() throws Break, Continue;

  default void forEach(final IntConsumer consumer) {
    try {
      for (;;) {
        try { consumer.accept(next()); }
        catch (final Continue notUsed) {}
    catch (final Break notUsed) {}

  default IntStream map(final IntUnaryOperator unary) { return () ->
unary.applyAsInt(next()); }

Where IntUnaryOperator is a functional interface with abstract method
applyAsInt. When the map method is called an instance of IntUnaryOperator
is passed in and then an instance of IntStream is created which calls
IntUnaryOperator's applyAsInt method each time its own next method is
called (i.e. map is lazy but requires two objects an IntUnaryOperator and
an IntStream).

If lambdas could implement abstract classes then only one object is needed,

 // 1. class not interface, 2. it is a IntStream, 3. @FunctionalInterface
becomes @Functional
@Functional public abstract class IntUnaryOperator implements IntStream {
  abstract public int applyAsInt(int in);

  public IntStream previous = null;

  public int next() { return applyAsInt(; }

Then the map method would become:

  default IntStream map(final IntUnaryOperator unary) {
    unary.previous = this;
    return unary;

This way only one instead of two objects are created, because
IntUnaryOperator is an IntStream.

Would it be useful in other cases if lambdas could implement abstract

  -- Howard.

More information about the lambda-dev mailing list