Quick start manual
10-2
Delphi Language Guide
Interface types
• All members of an interface are public. Visibility specifiers and storage specifiers
are not allowed. (But an array property can be declared as default.)
• Interfaces have no constructors or destructors. They cannot be instantiated, except
through classes that implement their methods.
• Methods cannot be declared as virtual, dynamic, abstract, or override. Since
interfaces do not implement their own methods, these designations have no
meaning.
Here is an example of an interface declaration:
type
IMalloc = interface(IInterface)
['{00000002-0000-0000-C000-000000000046}']
function Alloc(Size: Integer): Pointer; stdcall;
function Realloc(P: Pointer; Size: Integer): Pointer; stdcall;
procedure Free(P: Pointer); stdcall;
function GetSize(P: Pointer): Integer; stdcall;
function DidAlloc(P: Pointer): Integer; stdcall;
procedure HeapMinimize; stdcall;
end;
In some interface declarations, the interface reserved word is replaced by
dispinterface. This construction (along with the dispid, read only, and write only
directives) is platform-specific and is not used in Linux programming.
IInterface and inheritance
An interface, like a class, inherits all of its ancestors’ methods. But interfaces, unlike
classes, do not implement methods. What an interface inherits is the obligation to
implement methods—an obligation that is passed onto any class supporting the
interface.
The declaration of an interface can specify an ancestor interface. If no ancestor is
specified, the interface is a direct descendant of IInterface, which is defined in the
System unit and is the ultimate ancestor of all other interfaces. IInterface declares three
methods: QueryInterface, _AddRef, and _Release.
Note
IInterface is equivalent to IUnknown. You should generally use IInterface for platform
independent applications and reserve the use of IUnknown for specific programs that
include Windows dependencies.
QueryInterface provides the means to obtain a reference to the different interfaces that
an object supports. _AddRef and _Release provide lifetime memory management for
interface references. The easiest way to implement these methods is to derive the
implementing class from the System unit’s TInterfacedObject. It is also possible to
dispense with any of these methods by implementing it as an empty function; COM
objects (Windows only), however, must be managed through _AddRef and _Release.