Implements (keyword)
This is a Language topic about Oxygene
Language Topics Introduction | Structured Overview | Grammar | Keywords | Functions
Implements is a keyword that can be used after a type member, to define that the member implements an interface or part of an interface.
It can be used in two modes, either to implement an individual member of the interface, or to delegate the entire interface implementation.
Implements for Individual Members
By default, in order to implement an interface, a class must provide members (methods, properties or events) with matching names and signatures for each member defined in the original interface. For example, to implement an interface IFoo with a method called Bar, the Bar method must be provided by the class.
Sometimes, it is not possible or desired to use the same method names as specified for the interface - for example because they might conflict with members already present in the class, or because more than one interface requires a method of the same name. The implements keyword can be used to delegate implementation to a different member (matching in signature), as follows:
type MyFoo = class(IFoo) public method Bar; // Foo already had a Bar class that does something else than IFoo.Bar. method FoosBar; implements IFoo.Bar; // provides the implementation for IFoo.Bar end;
This feature is comparable to Delphi's "=" syntax, where "procedure IFoo.Bar = FoosBar; would have been used. See Minor Language Differences compared to Delphi for more details on language differences.
Implements to Delegate Entire Interfaces
Another use for implements is to delegate the entire implementation (i.e. all members) of a given interface to a property or field, essentially making it possible for another class to implement the interface logic.
The syntax for this is:
type MyFoo = class(IFoo) private property IntFoo: InternalFoo; implements IFoo; end;
All calls to members of IFoo are will be passed on to the InternalFoo class assigned to the IntFoo property. Of course, the MyFoo class must take care that a proper Instance of InternalFoo is created and returned from the property, or else calls to the IFoo methods will fail with a Null Reference Exception (NRE).
Delegating Several Interfaces at Once
The same syntax can also be used to delegate implementation of several interfaces to the same field or property:
type MyList = class(IList, ICollection, IEnumerable) private z: arraylist; implements IList, ICollection, IEnumerable; end;
Implements and Member Visibility
By default, interfaces implemented via the implements keyword are implemented privately, meaning the interface methods are not available on the main class, and accessible only by casting to the interface type.
The public keyword can be used to indicate that the interface members should also be public on the class itself, as shown below:
type MyClass = class(IMyInterface) protected property MyInterface: IMyInterface; implements public IMyInterface; end;
Note that the visibility of the interface methods is independent of the actual visibility of the field or property that implements is being applied to.