Name

Implements Directive

Syntax

Property declaration read Getter implements Interfaces...;

Description

A class can delegate the implementation of one or more interfaces to a property with the implements directive. The property’s Getter must be a field or a simple method (no array or indexed properties allowed). The method can be virtual, but not dynamic or a message handler. The Getter field or method must have a class or interface type. If the property implements more than one interface, separate the interface identifiers with commas.

The listed interfaces must appear in the class declaration.

Tips and Tricks

  • The implements directive is often used to implement COM-style aggregation. The ComObj unit declares the TAggregatedObject class as the base class for the inner object.

  • If the Getter is of class type, you must be careful about the lifetime of the object. Once Delphi casts the object reference to an interface, Delphi’s automatic reference counting manages the object’s lifetime. Once the last interface is out of scope, the object will be freed automatically. The simplest way to manage this situation is for the Getter method to create a new object every time it is called.

  • If the Getter is of class type, you can use method resolution clauses to redirect some of the interface methods to the containing class.

  • If the Getter is of interface type, the returned interface must implement all the listed interfaces. You cannot use method resolution to alter the interfaces.

Example

interface // ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.