[ 
https://issues.apache.org/jira/browse/GROOVY-11548?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17910517#comment-17910517
 ] 

Paul King edited comment on GROOVY-11548 at 1/7/25 7:14 AM:
------------------------------------------------------------

This is kind of by design (well, an implementation detail in Groovy 4). Groovy 
5 compiles default interface methods natively. Groovy 4 emulates them with 
traits. So, your interface (because it has a default method) is converted to:
{code:groovy}
trait I0 {
    void func() { }
}
{code}
And the semantics of traits is to override rather than be a default. Groovy 5 
lets you pick between traits and default methods in interfaces so you get the 
best of both worlds depending on your needs. The workaround in Groovy 4 is to 
write just that one interface in Java.

Another workaround would be to use {{{}@Delegate{}}}:
{code:groovy}
class A {
    final void func() {
        println 'A::func'
    }
}

interface I0 {
    default void func() {
        println 'IO::func'
    }
}

class B implements I0 {
    @Delegate A a = new A()
}

new B().func() // => A::func
{code}


was (Author: paulk):
This is kind of by design (well, an implementation detail in Groovy 4). Groovy 
5 compiles default interface methods natively. Groovy 4 emulates them with 
traits. So, your interface (because it has a default method) is converted to:
{code:groovy}
trait I0 {
    void func() { }
}
{code}
And the semantics of traits is to override rather than be a default. Groovy 5 
lets you pick between traits and default methods in interfaces so you get the 
best of both worlds depending on your needs. The workaround in Groovy 4 is to 
write just that one interface in Java.

> Could not inherit class and interface where class has a final method 
> overridden by interface
> --------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-11548
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11548
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 4.0.24
>         Environment: Tried on Linux and Windows, the same error.
>            Reporter: Xiaotian Ma
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 5.0.0-alpha-11
>
>
> Compile following code.
> {code:groovy}
> class A {
>     final public void func() {
>     }
> }
> interface I0 {
>     public default void func() {
>     }
> }
> class B extends A implements I0 {
> }{code}
> Groovy 4.0.24:
> {code:java}
> A.groovy: -1: You are not allowed to override the final method func() from 
> class 'A'.
> @ line -1, column -1.
> 1 error{code}
> Groovy 5.0.0-alpha-11: passed



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to