Contents | Prev | Next | Index
The declaration of a method within a class type corresponds to a forward declaration of that method. Somewhere after the class type declaration, and within the same program or unit, the method must be implemented by a defining declaration.
For procedure and function methods, the implementing declaration is like a normal procedure or function declaration. However, the procedure or function identifier in the procedure heading must be a qualified method identifier. A qualified method identifier consists of a class type identifier, which was introduced in the same program or unit. It is followed by a period "." and then by the method identifier itself. For constructors or destructors, the implementing declaration takes the form of a procedure method declaration, as described above, but the reserved word PROCEDURE is replaced by CONSTRUCTOR or DESTRUCTOR.
A method's implementing declaration can optionally repeat the formal parameter list of the method heading in the class type. If specified, the implementing declaration's method heading must match exactly the order, types and names of the parameters, and the type of the function result, if any.
Example: Given the class type declaration
TYPE
TFrameLabel = CLASS( TLabel )
PROTECTED
PROCEDURE Paint; OVERRIDE;
END;
the Paint method must be later implemented by a defining declaration, such as in the following example:
PROCEDURE TFrameLabel.Paint;
BEGIN
INHERITED Paint;
WITH Canvas DO BEGIN
Brush.Color := clWindowText;
Brush.Style := bsSolid;
FrameRect(ClientRect);
END;
END;
In the defining declaration of a method, there is always an implicit parameter with the identifier Self, corresponding to a formal parameter of the class type. Within the method body this implicit parameter represents the instance of the object for which the method was invoked. The scope of a member identifier of that object extends over any procedure, function, constructor, or destructor block that implements a method of the class type. This is the same as if the entire method block was embedded in a with-statement of the form:
WITH SELF DO BEGIN
:
END;
Within a method block, the reserved word INHERITED can be used to access redeclared and overridden member identifiers. In the above mentioned example for the implementation of the TFrameLabel.Paint method, INHERITED is used to invoke the inherited implementation of the Paint method. Using the keyword INHERITED causes the compiler to search for the identifier beginning with the immediate superclass of the enclosing method's class type.
Contents | Prev | Next | Index
Canterbury Pascal for JVM (Last documentation update
Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com
. All rights reserved.
Please send any comments or corrections to
neuhoff@mhccorp.com