interface I
{
void foo();
}
abstract class A : I
{
}
class B : A
{
override void foo () {}
}
The above code fails to compile:
overrideBaseInterface.d(10): function overrideBaseInterface.B.foo does not override any function
It looks to me like B.foo is overriding I.foo and hence the override keyword should be valid.
Comment #2 by andrej.mitrovich — 2013-05-06T15:01:41Z
Technically you're not overriding, you're implementing. I think there is no bug here. I also think the following should be an error:
-----
interface I
{
void foo();
}
class C : I
{
override void foo () {} // no error here currently
}
----
Comment #3 by maximzms — 2013-05-06T21:37:50Z
I think `override` keyword is useful to mark the methods that are supposed to be declared elsewhere.
In the following example I'm also (technically) implementing:
--------------------
abstract class A
{
void foo();
}
class B : A
{
void foo () {}
}
--------------------
test.d(7): Deprecation: overriding base class function without using override attribute is deprecated (test.B.foo overrides test.A.foo)
--------------------
Comment #4 by public — 2013-08-07T12:38:20Z
(In reply to comment #2)
> Technically you're not overriding, you're implementing. I think there is no bug
> here. I also think the following should be an error:
>
> -----
> interface I
> {
> void foo();
> }
>
> class C : I
> {
> override void foo () {} // no error here currently
> }
> ----
Voting up this bug report.
Semantical difference between overriding and implementing is very subtle here. One can also say that it overrides function with empty implementation.
However, what does matter here is pragmatical usability it brings to the table.
http://dlang.org/attribute.html#override
> The override attribute applies to virtual functions. It means that the
> function must override a function with the same name and parameters in a base
> class. The override attribute is useful for catching errors when a base
> class's member function gets its parameters changed, and all derived classes
> need to have their overriding functions updated.
This description does miss one important use case - verifying that you do not introduce a new function symbol in the class hierarchy. Consider this example: in the process of wild refactoring interface signature changes and dependent classes get modified. A sloppy programmer works on new implementation of that functions and forgets that there is already existing one. However, code compiles and works, polluting sources with unused function body.
Comment #5 by andrej.mitrovich — 2013-08-07T13:17:16Z
(In reply to comment #4)
> A sloppy programmer works on new implementation of that
> functions and forgets that there is already existing one. However, code
> compiles and works, polluting sources with unused function body.
I agree 'override' adds to the readability, however D already protects you against function hiding and function hijacking. For example:
-----
class A
{
void foo() { }
}
class B : A
{
void foo() { }
}
-----
test.d(10): Deprecation: overriding base class function without using override attribute is deprecated (test.B.foo overrides test.A.foo)
I don't know why I wrote http://d.puremagic.com/issues/show_bug.cgi?id=2525#c2, I do believe override makes the code more readable. Ideally we would have an "implements" keyword, but you know, keyboard bloat and all that.
Comment #6 by verylonglogin.reg — 2014-07-18T11:51:11Z
*** Issue 10699 has been marked as a duplicate of this issue. ***
Comment #7 by verylonglogin.reg — 2014-07-18T11:52:29Z
*** Issue 12329 has been marked as a duplicate of this issue. ***
Comment #8 by andrej.mitrovich — 2014-11-25T21:10:39Z
Shame on you, Andrej Mitrovic!
I totally support this now, perhaps not so much 'override' as wanting a new 'implements' keyword. But the distinction is so pale, and the real benefit is really *knowing* that a method is actually part of a base class / interface API, so I'm now definitely in huge favor of allowing (and perhaps even *requiring*) override for implementing keywords.
Comment #9 by dlang — 2014-12-07T15:20:06Z
I just hit this bug too and I wasn't so fortunate as to realise that classes that implement an interface need override but classes that extend and abstract class which implements an interface must not have the "override" attribute. Please make it consistent!
Comment #10 by initrd.gz — 2015-11-13T18:49:34Z
Just ran into this, so I'll bump it.
Note that if, in the OP's code, you change the abstract class A to an empty interface, the code successfully compiles.
Comment #11 by b2.temp — 2016-08-12T14:13:07Z
*** Issue 16318 has been marked as a duplicate of this issue. ***
Comment #12 by lodovico — 2016-08-12T14:30:30Z
Any news on this? People periodically hits this issue and finds it very strange...
Comment #13 by code — 2017-07-09T12:27:09Z
See Issue 9978 for why we allow to override interface functions.
The given code should work.
Comment #14 by code — 2017-07-09T12:29:56Z
As a workaround one can redeclare the interface methods in the abstract class.
----
interface I
{
void foo();
}
abstract class A : I
{
override void foo();
}
class B : A
{
override void foo () {}
}
----
Note that this bug and the workaround also applies to implicit abstract classes.
----
interface I
{
void foo();
}
class A : I
{
abstract override void foo();
}
class B : A
{
override void foo () {}
}
----
Comment #15 by dlang-bot — 2021-02-25T22:20:54Z
@seeseemelk created dlang/dmd pull request #12232 "Fix 2525: check all parent interfaces for overriden methods" fixing this issue:
- Fix 2525: check all parent interfaces for overriden methods
https://github.com/dlang/dmd/pull/12232
Comment #16 by dlang-bot — 2023-01-29T12:05:57Z
@ntrel created dlang/dmd pull request #14853 "Fix 2525: check all parent interfaces for overriden methods" fixing this issue:
- Fix 2525: check all parent interfaces for overriden methods
Fix 2525: refactor in order to use Array!(BaseClass) instead of dynamic array and show deprecation
Fix 2525: test if function is implemented by a parent class before throwing an error
Fix 2525: make forAllInterfaces take a delegate
issue 2525: add unit tests
issue 2525: update documentation
https://github.com/dlang/dmd/pull/14853
Comment #17 by robert.schadek — 2024-12-13T17:49:09Z