www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Abstract Classes

reply IM <3di gm.com> writes:
Assume the following:

interface IFace {
   void foo();
   void bar();
}

abstract class A : IFace {
   override void foo() {}
}

class B : A {
   override void bar() {}
}

Now why this fails to compiler with the following message:


--->>>
function bar does not override any function, did you mean to 
override 'IFace.bar()'?
<<<---


Obviously, I meant that, since the abstract class A implements 
IFace, and B derives from A.

Do I need to declare IFace's unimplemented methods in A as 
abstract? If yes, why? Isn't that already obvious enough (any 
unimplemented virtual function is abstract)?
Dec 05 2017
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/05/2017 11:23 PM, IM wrote:
 Assume the following:
 
 interface IFace {
    void foo();
    void bar();
 }
 
 abstract class A : IFace {
    override void foo() {}
 }
 
 class B : A {
    override void bar() {}
 }
 
 Now why this fails to compiler with the following message:
 
 
 --->>>
 function bar does not override any function, did you mean to override 
 'IFace.bar()'?
 <<<---
 
 
 Obviously, I meant that, since the abstract class A implements IFace, 
 and B derives from A.
 
 Do I need to declare IFace's unimplemented methods in A as abstract? If 
 yes, why? Isn't that already obvious enough (any unimplemented virtual 
 function is abstract)?
Just remove the override keywords in this case. No function is overriding any implementation here, they both implement an interface function. The fact that override can be used for A.foo can be seen as an inconsistency or a bug. Ali
Dec 05 2017
parent reply IM <3di gm.com> writes:
On Wednesday, 6 December 2017 at 07:54:21 UTC, Ali Çehreli wrote:
 On 12/05/2017 11:23 PM, IM wrote:
 [...]
Just remove the override keywords in this case. No function is overriding any implementation here, they both implement an interface function. The fact that override can be used for A.foo can be seen as an inconsistency or a bug. Ali
I believe this is a bug, and a confusing one to be honest. Can you please help file one against the right owners? Thanks!
Dec 06 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/06/2017 03:01 PM, IM wrote:
 On Wednesday, 6 December 2017 at 07:54:21 UTC, Ali Çehreli wrote:
 On 12/05/2017 11:23 PM, IM wrote:
 [...]
Just remove the override keywords in this case. No function is overriding any implementation here, they both implement an interface function. The fact that override can be used for A.foo can be seen as an inconsistency or a bug. Ali
I believe this is a bug, and a confusing one to be honest. Can you please help file one against the right owners? Thanks!
There is no owners field when opening an issue. To get you started on the bug tracking system, please create this one yourself: :) https://issues.dlang.org/ Thank you, Ali
Dec 06 2017
parent IM <3di gm.com> writes:
On Wednesday, 6 December 2017 at 23:16:54 UTC, Ali Çehreli wrote:
 On 12/06/2017 03:01 PM, IM wrote:
 On Wednesday, 6 December 2017 at 07:54:21 UTC, Ali Çehreli
wrote:
 On 12/05/2017 11:23 PM, IM wrote:
 [...]
Just remove the override keywords in this case. No function
is
 overriding any implementation here, they both implement an
interface
 function. The fact that override can be used for A.foo can
be seen as
 an inconsistency or a bug.

 Ali
I believe this is a bug, and a confusing one to be honest.
Can you
 please help file one against the right owners? Thanks!
There is no owners field when opening an issue. To get you started on the bug tracking system, please create this one yourself: :) https://issues.dlang.org/ Thank you, Ali
Done: https://issues.dlang.org/show_bug.cgi?id=18041. Thanks!
Dec 06 2017
prev sibling parent bauss <jj_1337 live.dk> writes:
On Wednesday, 6 December 2017 at 07:23:29 UTC, IM wrote:
 Assume the following:

 interface IFace {
   void foo();
   void bar();
 }

 abstract class A : IFace {
   override void foo() {}
 }

 class B : A {
   override void bar() {}
 }

 Now why this fails to compiler with the following message:


 --->>>
 function bar does not override any function, did you mean to 
 override 'IFace.bar()'?
 <<<---


 Obviously, I meant that, since the abstract class A implements 
 IFace, and B derives from A.

 Do I need to declare IFace's unimplemented methods in A as 
 abstract? If yes, why? Isn't that already obvious enough (any 
 unimplemented virtual function is abstract)?
bar() is not a virtual function, but is defined in the interface IFace and thus you don't need to override it in B. The same goes for foo() which I'd argue should have given same error. What you possibly wanted to do is this: interface IFace { void foo(); void bar(); } abstract class A : IFace { abstract void bar(); // All child classes must implement bar and override it abstract void foo(); // Since A implements IFace we must implement both bar() and foo() // However it's an abstract class, so we can leave implementation // up to the children. } class B : A { override void bar() {} override void foo() {} }
Dec 05 2017