Calling parent trait method from child class in Groovy

Viewed 238

I have the following:

trait Trait {
    def Method() {
        // do stuff
    }
}
class Class implements Trait {
    def Method() {
        // do other stuff
        super.Method()
    }
}

This compiles but doesn't run as Groovy cannot resolve super.Method(). Calling just Method() results in a stack overflow.

Is it possible to override a Groovy method in this way?

1 Answers

You can do this (using the naming conventions mentioned, but that wasn't the issue)

trait Trait {
    def method() {
        println "yay"
    }
}

class MyClass implements Trait {
    def method() {
        // do other stuff
        println "woo"
        Trait.super.method()
    }
}

new MyClass().method() 
Related