Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 836
Lennart Avatar asked Mar 14 '23 04:03

Lennart


1 Answers

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

like image 179
Roland Avatar answered Mar 24 '23 17:03

Roland