[fpc-pascal] interface inheritance implementation

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Mon Oct 12 12:13:23 CEST 2015

On 2015-10-11 20:29, David Emerson wrote:
> // and finally the implementing class for i_multiplier_and_adder
> t_multiplier_and_adder = class (tbase, i_multiplier_and_adder)

I haven't tried this for a while, but vaguely remember the following.
As far as I know Interface inheritance does not work in FPC - or it is
not implemented completely in and FPC version. Change your code as follows.

// this next interface inherits from i_adder, and thus does two things
i_multiplier = interface
   function multiply (a, b : longint) : longint;

// and finally the implementing class for i_multiplier and i_adder
t_multiplier_and_adder = class (tbase, i_adder, i_multiplier)
   f_adder : i_adder;
   function multiply (a, b : longint) : longint;
   property adder : i_adder read f_adder implements i_adder;

In FPC you can't inherit one Interface from another. Define them
separately, then mix them together in classes that implement them.

With the above code, you can have one class implement the i_adder
interface, and another class that implements the i_multiplier interface.
Them create a new t_multiplier_and_adder class that mixes in both
interfaces and uses interface delegation - that way you don't have to
repeat the actual interface implementations in each class and still get
code reuse. I hope this makes sense.

  - Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal

My public PGP key:  http://tinyurl.com/graeme-pgp

More information about the fpc-pascal mailing list