Quick start manual
Object interfaces
10-5
Implementing interfaces
declares a class called TMemoryManager that implements the IMalloc and IErrorInfo
interfaces. When a class implements an interface, it must implement (or inherit an
implementation of) each method declared in the interface.
Here is the declaration of TInterfacedObject in the System unit.
type
TInterfacedObject = class(TObject, IInterface)
protected
FRefCount: Integer;
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
property RefCount: Integer read FRefCount;
end;
TInterfacedObject implements the IInterface interface. Hence TInterfacedObject declares
and implements each of IInterface’s three methods.
Classes that implement interfaces can also be used as base classes. (The first example
above declares TMemoryManager as a direct descendent of TInterfacedObject.) Since
every interface inherits from IInterface, a class that implements interfaces must
implement the QueryInterface, _AddRef, and _Release methods. The System unit’s
TInterfacedObject implements these methods and is thus a convenient base from
which to derive other classes that implement interfaces.
When an interface is implemented, each of its methods is mapped onto a method in
the implementing class that has the same result type, the same calling convention, the
same number of parameters, and identically typed parameters in each position. By
default, each interface method is mapped to a method of the same name in the
implementing class.
Method resolution clauses
You can override the default name-based mappings by including method resolution
clauses in a class declaration. When a class implements two or more interfaces that
have identically named methods, use method resolution clauses to resolve the
naming conflicts.
A method resolution clause has the form
procedure interface.interfaceMethod = implementingMethod;
or
function interface.interfaceMethod = implementingMethod;
where implementingMethod is a method declared in the class or one of its ancestors.
The implementingMethod can be a method declared later in the class declaration, but
cannot be a private method of an ancestor class declared in another module.