Will JDK 8 support SAM that is Abstract Class ?

Brian Goetz brian.goetz at oracle.com
Mon Nov 5 09:31:57 PST 2012


And, many otherwise-SAM abstract classes could add constructors/factory methods that take a SAM interface, as we did for ThreadLocal:

class ThreadLocal<T> { 
    public ThreadLocal(Factory<T> factory) { ... }
}

So you can say 

  ThreadLocal<Bazooka> tl = new ThreadLocal(() -> new Bazooka(....));


On Nov 5, 2012, at 11:41 AM, Kevin Bourrillion wrote:

> I was initially pro-abstract-class-support, but after taking a hard look at
> just how often such a feature is really needed I changed my mind.
> 
> If abstract classes were supported, there would be a magical hidden
> invocation of user code here:
> 
>  SomeType x = () -> 1;
> 
> ... namely, the SomeType constructor. That constructor could do anything,
> including throw checked exceptions -- this is pretty surprising and goes
> against the "feel" of lambda expressions. And note there'd be no way to
> invoke a constructor that had parameters anyway.
> 
> When we really must extend a SAM abstract class, we'll just have to
> continue to use an anonymous class, and that's fine.
> 
> -- 
> Kevin Bourrillion | Java Librarian | Google, Inc. | kevinb at google.com
> 



More information about the lambda-dev mailing list