tschumann
tschumann

Reputation: 3246

Calling parent trait method from child class in Groovy

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?

Upvotes: 3

Views: 597

Answers (1)

tim_yates
tim_yates

Reputation: 171114

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() 

Upvotes: 2

Related Questions