Overriding of inherited class static methods?

ShadoLight ettienne.gilbert at gmail.com
Tue Jul 21 14:50:29 UTC 2026


On Tuesday, 21 July 2026 at 13:11:37 UTC, Steven Schveighoffer 
wrote:
> On Tuesday, 21 July 2026 at 10:51:30 UTC, Denis F wrote:
>> Hi!
>>
>> Can anyone remind me why D doesn't provides overriding of 
>> static methods?
>> Is there some fundamental problem with this?
>>
>> I think it would be great in itself and also intuitive.
>
> How does it work? Overriding of a function works because you 
> have a class instance. A static function has no instance.
>
> -Steve

Since he is specifically asking about static methods, maybe the 
OP is thinking of 'overloading', rather than 'overriding' - 
currently this already works:

```D
class B {
     public static int bar(int y) {
         return 20*y;
     }
}

class D : B {
     public static int bar(int y) {
         return 30*y;
     }
}

assert(B.bar(2)==40);
assert(D.bar(2)==60);
```

But this of course is not the same behavior as 'overriding' eg. 
if you do this...

```D
B d = new D;
```
... and call the static method through the instance, the function 
in class B will get called i.e. you will get...

```D
assert(d.bar(2)==40);
```
... which is not the polymorphic behavior you get when overriding.


More information about the Digitalmars-d mailing list