Why private methods cant be virtual?

ShadoLight ettienne.gilbert at gmail.com
Tue Sep 22 11:29:44 UTC 2020


On Tuesday, 22 September 2020 at 10:23:08 UTC, Daniel Kozak wrote:
> On Tue, Sep 22, 2020 at 11:06 AM claptrap via 
> Digitalmars-d-learn < digitalmars-d-learn at puremagic.com> wrote:
>
>>
>> "Functions marked as final may not be overridden in a derived 
>> class, unless they are also private"
>>
>> So final private functions can be overriden? It seems not, but 
>> the sentence is definitely confusing if not just plain wrong.
>>
>> Yes they can, if you have class A in one module and class B in 
>> another
> module this will work:
>
> //a.d
> class A
> {
> private final void overrideFun()
> {
> import std.stdio : writeln;
> writeln("A::overrideFun");
> }
> }
>
> //b.d
> import a;
> class B : A
> {
> void overrideFun()
> {
> import std.stdio : writeln;
> writeln("B::overrideFun");
> }
> }
>
> // main.d
> import b;
>
> void main(string[] args)
> {
> B b = new B;
> b.overrideFun;
> }

This is not really "overriding", it is more akin to 
"overloading". It is also not polymorphic i.e. this will call  
A::overrideFun.

A b = new B;
b.overrideFun;



More information about the Digitalmars-d-learn mailing list