Contents | Prev | Next | Index


Constructors

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
        ::= constructor proc_id formal_parameters constructor ::= modifier CONSTRUCTOR destructor ::= modifier DESTRUCTOR modifier ::= java_modifiers ::= <empty> proc_id ::= ident ::= ident . member_id ::= ident [ <string> ] ::= ident . member_id [ <string> ] ::= ident java_name ::= ident . member_id java_name

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:

NEW procedure call:
This procedure call causes a class variable, which is its first parameter, to be assigned to a newly created instance of the class. The second actual parameter of the NEW procedure call is a constructor call, which initializes the new object. Example:
  NEW( Rectangle, Init( 10,12 ) );

It is also possible to specify the additional constructor parameters for the NEW procedure call directly. Example:
  NEW( Rectangle, 10, 12 );
NEW function call:
This function call returns a new instance of a class. The class type is specified as the first parameter. The second actual parameter of the NEW function call is a constructor call, which initializes the new object. Example:
  Figure := NEW( TRectangle, Init( 10,12 ) );
It is also possible to specify the additional constructor parameters for the NEW function call directly. Example:
  Figure := NEW( TRectangle, 10, 12 );
Direct constructor call:
This form of a constructor call is only allowed inside an implementing  constructor body, and it must be the first statement. It is used for intializing a new object. Example:
  CONSTRUCTOR TRectangle.Create;
  BEGIN
    Init( 0,0 );
    :
  END;
Inherited constructor call:
Again, this form of a constructor call is only allowed inside an implementing constructor body, and it must be the first statement. It is used for intializing the inherited portion of a new object. Example:
  CONSTRUCTOR TRectangle.Init
    ( length, width : INTEGER );
  BEGIN
    INHERITED Create;
    Self.length := length;
    Self.width := width;
    :
  END;
Constructor call via a class reference:
This form of a constructor call is like a function call, returning a new instance of the specified class. The new object is initialized by the constructor. Example:
  VAR
    Figure : TFigure;
    :
  BEGIN
    Figure := TFigure.Create( 0, 0 );
    :
  END;

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