How to use Groovy properties in interfaces?

Say I defined an interface in Groovy (with properties) as follows:

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 do I now implement properties in a particular class? @Overridedoes not work because IntelliJ complains that I cannot override the field. I read this similar question , but it only says that you can use properties in the interface, but not how.

+4
source share
1 answer

As in Java, you cannot specify properties in an interface. But with Groovy you can use the trait for this:

trait GroovyInterface
{
    int intProperty
    boolean boolProperty
}

Then you can use it just like the interface with "implements GroovyInterface".

http://docs.groovy-lang.org/next/html/documentation/core-traits.html

+5

Source: https://habr.com/ru/post/1615709/


All Articles