Inherited The reserved word inherited plays a special role in implementing polymorphic behavior. It can occur in method
definitions, with or without an identifier after it.
If inherited is followed by the name of a member, it represents a normal method call or reference to a property or
field - except that the search for the referenced member begins with the immediate ancestor of the enclosing
method's class. For example, when
inherited Create(...);
occurs in the definition of a method, it calls the inherited
Create
.
When inherited has no identifier after it, it refers to the inherited method with the same name as the enclosing method
or, if the enclosing method is a message handler, to the inherited message handler for the same message. In this
case, inherited takes no explicit parameters, but passes to the inherited method the same parameters with which
the enclosing method was called. For example,
inherited;
occurs frequently in the implementation of constructors. It calls the inherited constructor with the same parameters
that were passed to the descendant.
Self Within the implementation of a method, the identifier Self references the object in which the method is called. For
example, here is the implementation of TCollection's
Add
method in the
Classes
unit.
function TCollection.Add: TCollectionItem;
begin
Result := FItemClass.Create(Self);
end;
The
Add
method calls the
Create
method in the class referenced by the
FItemClass
field, which is always a
TCollectionItem descendant.
TCollectionItem.Create
takes a single parameter of type TCollection, so
Add
passes it the TCollection instance object where
Add
is called. This is illustrated in the following code.
var MyCollection: TCollection;
...
MyCollection.Add // MyCollection is passed to the TCollectionItem.Create method
Self is useful for a variety of reasons. For example, a member identifier declared in a class type might be redeclared
in the block of one of the class' methods. In this case, you can access the original member identifier as
Self.Identifier
.
For information about Self in class methods, see Class methods.