How to instantiate a Java abstract class with a constructor parameter with a groovy closure

abstract-class, groovy, java

Solution

You can instantiate it in classic Java style:

StackOverflowStr stack = new StackOverflowStr("javaish"){
    String answerMe() {"answer"}
}

Problem

I am trying to instantiate a Java abstract class from my Groovy code. Considering the following Java abstract class (non relevant processing is stripped out from the class): ``` public abstract class StackOverflow{ public abstract String answerMe(); } ``` I can easily instantiate it in Groovy this way, and the call to `answerMe()` will trigger the correct output: ``` StackOverflow stack = [answerMe : { "Answer" }] as StackOverflow ``` Now if I modify the `StackOverflow` class adding a String parameter in the constructor like this : ``` public abstract class StackOverflowStr{ public StackOverflowStr(String s){} public abstract String answerMe(); } ``` I don't really know how to instantiate my object, I tried a lot of thing, but I can't seem to find the right syntax, does someone got any clue ?

Original source