4

This question is very like the other one discussing Explicitly calling a default method in Java, but in Groovy code(ver. 2.4.16)

public interface Interface { // a Java interface
    default void call() {}
}

public class Test implements Interface { // a Groovy class
    @Override
    public void call() {
        Interface.super.call() // compile error: Groovy:The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.
    }
}

On the other hand, default methods are very like Trait in Groovy, and the following code compiles OK

public trait Trait { // a Groovy trait
    void call() {}
}

public class Test implements Trait { // a Groovy class
    @Override
    public void call() {
        Trait.super.call() // compiles OK
    }
}

So, how to explicitly call a default method in a Groovy subclass?

Jay
  • 326
  • 2
  • 8
  • In Groovy 2.4 you can't specify default methods on interfaces. However if you have a Java interface implemented by a Groovy class and its override method wishes to invoke the default method, would it be "super.whatever()" – emilles Feb 23 '19 at 22:03
  • If the super method comes from a trait, you can use the Type.super.whatever() syntax. – emilles Feb 23 '19 at 22:04
  • @emilles In Groovy 2.5 it seems that neither `Interface.super.whatever()` nor `super.whatever()` works... It searches for the super implementation in the parent class, but not in the implemented interface. Any suggestion? – Mauro Molinari May 16 '19 at 13:31
  • Seems to be fixed only in Groovy 4.0: https://issues.apache.org/jira/browse/GROOVY-9909 – Mauro Molinari Feb 22 '22 at 17:14

0 Answers0