Question about interface implementation

Theo Theo at gmail.com
Sun May 21 22:50:56 UTC 2023


On Sunday, 21 May 2023 at 11:20:30 UTC, ag0aep6g wrote:
> On 21.05.23 12:55, Theo wrote:
>> As for the other part, if I use an abstract base class, I 
>> *must* indicate when i'm overriding the base class method by 
>> explicately saying 'override'.
>
> I wouldn't mind if implementing interface methods required 
> `override` as well. I don't know if there is a rationale for 
> the inconsistency.

Consistency, which can also aid in self-documenting, might look 
something like this:

----- using interface -----

interface Ship
{
     void setSpeed(int speed); // must 'implement'
     int getSpeed(); // must 'implement'
}

class PirateShip : Ship
{
     private(this) int speed = 0;

     public void setSpeed(int speed) : implements Ship
     {
         this.speed = speed;
     }

     public int getSpeed() : implements Ship
     {
         return speed;
     }
}

----- using base class -----
abstract base class Ship
{
     abstract void setSpeed(int speed); // must 'implement'
     abstract int getSpeed(); // must 'implement'
     void someotherMethod(); // can 'override'.
}

class PirateShip : Ship
{
     private(this) int speed = 0;

     public void setSpeed(int speed) : implements Ship
     {
         this.speed = speed;
     }

     public int getSpeed() : implements Ship
     {
         return speed;
     }

     public void someotherMethod() : overrides Ship
     {

     }
}

-----------------



More information about the Digitalmars-d-learn mailing list