Abstract Classes

bauss jj_1337 at live.dk
Wed Dec 6 07:56:28 UTC 2017


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() {}
}



More information about the Digitalmars-d-learn mailing list