implement vs override

Peter C peterc at gmail.com
Mon Nov 3 09:32:45 UTC 2025


On Monday, 3 November 2025 at 08:34:02 UTC, Serg Gini wrote:
> On Monday, 3 November 2025 at 08:02:06 UTC, Peter C wrote:
>> But...
>>
>> .. what if I need to keep the semantics of the base class 
>> method (or override for some reason), but also implement the 
>> semantics of the interface contract?
>
> This is all theoretical - without code example doesn't mean 
> anything.
> Drop all prompt that was used before and provide the model name 
> that used for this text generation.

'the model name' ??

Anyway...it's solved now. It turns out, that composition was the 
answer here.

module myModule;
@safe:
private:

import std.stdio : writeln;

interface Isomething
{
     void someMethod();
}

class Base
{
     void someMethod()
     {
         writeln("Hello from Base");
     }
}

class Derived : Isomething
{
     Base base; // composition

     this()
     {
         base = new Base();
     }

     // Interface implementation
     void someMethod()
     {
         writeln("Hello from Isomething in Derived");
     }

     // Expose base behavior separately
     void callBase()
     {
         base.someMethod();
     }
}

void main()
{
     auto d = new Derived();

     Isomething i = d;
     i.someMethod(); // "Hello from Isomething in Derived"

     d.callBase();   // "Hello from Base"
}




More information about the Digitalmars-d mailing list