Skip to content

Interfaces and multiple inheritance

Interfaces2 - GUI

By reading the introduction of the interfaces, you obtained a mechanism that makes a class really and completely abstract.

From the article we highlighted their total abstraction.
We noticed that it's contained methods must be overridden by the inherited class(es).
Furthermore a interface does not have any attributes.

Finally we wrote - just on the rush - that the Interfaces mechanism overpasses one compiler's weak point, about the inability to support the multiple inheritance.

In brief: the multiple inheritance in FreePascal grants that an inheriting class can inherits from more than one more abstract class.
It so expands its possibility to enrich itself, always  by keeping the order and the hierarchy achieved with the interfaces.

And that's very good... in theory.

Before any other consideration let's take a look at how to make a multiple inheritance.

We surely need at least two classes for the inheritance, and they will be enough.
If you remember we've always said how the multiple inheritance of normal classes is impossible with the FreePascal compiler, and it's still true.

Interfaces go over the obstacle so, that every class can inherit from all of them it's designed for.

We didn't specify it from the beginning to avoid any confusion or dispersion of attention, but now thanks to the step-by-step approach we finally got it.

We restart from the last example where we had the IHuman interface, and we add just a new interface we call ISocial to modeling not only the Sex of people but their Character too.

It's easy to imagine the ISocial's code, but for sake of clarity here it is:

unit Social;

{$mode objfpc}{$H+}



  ISocial = Interface //Social is an INTERFACE
  //Interface has no visibility modifiers (public private protected). All members are public !!!
  //Interface has no attributes!!!
  //Interface has only not implemented (virtual abstract) methods that must be redefined by the children...
  function GetCharacter():string;
  procedure SetCharacter(Character_:string);

//An interface has NO implementation !!!

Just the same as for IHuman.