Contents | Prev | Next | Index
Constructors are special methods that control the construction of objects. They are used to initialize new objects. Constructors are defined and implemented like procedure methods, except for using the CONSTRUCTOR keyword instead of PROCEDURE. Otherwise, the constructor heading is like a method heading. The following grammar shows the constructor procedure heading, with Java-specific language extensions marked in red:
procedure_heading ::= constructor proc_id |
Typically, the initialization is based on values passed as parameters to the constructor. A constructor is always virtual because it operates on a newly created object. It should therefore be defined with a VIRTUAL or OVERRIDE keyword. Canterbury Pascal recognizes static constructor definitions, too, but treats them like virtual constructors. Static constructors are syntactically supported by this compiler in order to keep its language syntax compatible with that of Borland Object Pascal. All Pascal-constructors are mapped to Java-constructors. Java does not use names for constructor methods, the Pascal constructor names are ignored by Java. Defining different Pascal-constructors for a class type results in overloaded Java-constructors. For that reason, all Pascal-constructors must have different formal parameter lists. Example:
TYPE
TRectangle = CLASS( TFigure )
lenght : INTEGER;
width : INTEGER;
CONSTRUCTOR Create();
OVERRIDE;
CONSTRUCTOR Init( length, width : INTEGER );
VIRTUAL;
DESTRUCTOR;
VIRTUAL;
END;
Constructor methods can be invoked in the following ways:
Notice that the NEW procedure or function calls with embedded constructor invocation are not supported in Borland Object Pascal. Using the NEW procedure call with an embedded constructor invocation used to be way for creating and initializing objects in Borland Turbo Pascal. Also, unlike Borland Object Pascal, this compiler does not support constructor calls in object references.
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