Lennart
Lennart

Reputation: 10322

How can I use Groovy's properties in Interfaces?

Let's say I have defined an Interface in Groovy (with properties) like this:

public interface GroovyInterface
{       
    int intProperty;       
    boolean boolProperty;
}

Then I implement the Interface:

public class GroovyImplementation implements GroovyInterface
{
    // How do I implement the properties here?        
}

How would I now implement the properties in the concrete class? @Override doesn't work, since IntelliJ complains that I can't override a field. I've read this similar question, but it only says that it is possible to use properties in an Interface, but not how.

Upvotes: 4

Views: 2002

Answers (1)

Roland
Roland

Reputation: 487

Like in Java you cannot specify properties in an interface. But with Groovy you can use a trait for that:

trait GroovyInterface
{
    int intProperty
    boolean boolProperty
}

You can then use it exactly like an interface with "implements GroovyInterface".

For more information on traits consult http://docs.groovy-lang.org/next/html/documentation/core-traits.html

Upvotes: 7

Related Questions