Java 8 lambda and extension of interfaces with abstract class Java 8 lambda and extension of interfaces with abstract class spring spring

Java 8 lambda and extension of interfaces with abstract class


Lambdas can't implement abstract classes. Here is why.

There is workaround mentioned in the article, but it won't work in your case as you trying to reference object field. What you can do, besides sticking to old-fashion anonymous classes, is to think how to "inject" A into lamda, for example:

  • instantiate A object inside lambda;
  • create A ouside of lambda scope;
  • make A singletone of some sort.


You can extend the functional interface and create one of your own:

@FunctionalInterfacepublic interface CustomRowMapper<T> implements RowMapper<T> {    static A a = new A();}

Then, you can pass a lambda, which is the implementation of CustomRowMapper#mapRow() method like this:

CustomRowMapper myCustomRowMapperLambda = (rs, rowNum) -> {    a.doSomething(rs, rowNum);    return new Object();};sqlProc.declareRowMapper(myCustomRowMapperLambda);


This should work:

sqlProc.declareRowMapper((rs, rowNum) -> {    new A().doSomething(rs, rowNum);    return new Object();});

You would have to instantiate your Object A. Given the code you show this would not change a thing.

To answer to the more general question not solely relying on example :Even with the cast you won't access to private variable A, I tend to think that is due to how types of lamdbas are infered.

And in fact I don't think it is whisful for : A lambda should be stateless. (You can make it stateful with the context but it is not something I would recommend).