Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-08

Explicit Interface implementation and inheritance

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dmitry Boyarintsev

Hello,

Why is interface implementation not inherited, if explicit implementation is used.

Example.

Here are interfaces:
  IAAA = interface
    procedure MethodA;
  end;
  IBBB = interface(IAAA)
    procedure MethodB;
  end;

Interface IBBB inherits from IAAA

Here's an implicit interface implementation:

  TObjA = class(TInterfacedObject, IAAA)
    procedure MethodA;
  end;
  TObjB = class(TObjA, IBBB)
    procedure MethodB;
  end;

Class TObjB inherits from TObjA. Where TObjA implements IAAA. And so TObjB only needs to implement MethodB, and MethodA is implicitly mapped for the interface method.

Here's an explicit interface implementation:

  TImpA = class(TInterfacedObject, IAAA)
  public
    procedure CallOfA; virtual;
    procedure IAAA.MethodA = CAllOfA;
  end;
  // compiler error. No matching implementation for interface method
"MethodA"; found
  TImpB = class(TImpA, IBBB)
  public
    procedure CallOfB;
    procedure IBBB.MethodB = CallOfB;
  end;

Why is it happening? TImpB inherites from TImpA. In TImpA the MethodA is implemented by "CallOfA".

Is it a requirement to map all the methods of implemented interfaces? (no matter if they were implemented in parent classes).

thanks,
Dmitry

Reply by email

Jean SUZINEAU
to Dmitry Boyarintsev

Le 15/08/2021 à 00:43, Dmitry Boyarintsev via fpc-pascal a écrit :

Why is interface implementation not inherited, if explicit implementation is used.

I guess this is because you can implement an interface through delegation too:

https://www.freepascal.org/docs-html/ref/refse48.html

type
   IMyInterface = interface
     procedure P1;
   end;

  TMyClass = class(TInterfacedObject, IMyInterface)   private
    FMyInterface: IMyInterface; // interface type   public
    property MyInterface: IMyInterface
       read FMyInterface implements IMyInterface;   end;

Reply by email

Jean SUZINEAU
to Jean SUZINEAU

Ooops, may be I hit reply too fast.

It looks like when you write " TObjB = class(TObjA, IBBB)" you just implement IAAA twice.
If you comment out IBBB
  TImpB = class(TImpA{, IBBB})
  public
    procedure CallOfB;
    //procedure IBBB.MethodB = CallOfB;
  end;

then the "No matching implementation" error disappears, the inherited implementation is well taken in account.

Reply by email