Define class constructors
- Last Updated: March 19, 2024
- 3 minute read
- OpenEdge
- Version 12.8
- Documentation
A constructor of a class is a special method that gets
called when a class is instantiated using the NEW function.
A constructor for a class has the same name as the class name. Unlike
ordinary methods, a constructor definition is identified by the CONSTRUCTOR statement.
In addition, constructors cannot have a return type.
You are not required to define a constructor. ABL provides a default instance constructor with no parameters for any ABL class that does not explicitly define one. You can also define multiple instance constructors for a class that are overloaded with different parameter signatures. If you choose to define an instance constructor without parameters, that constructor becomes the default instance constructor for the class. You can only define one static constructor for a class. ABL also provides a default static constructor for a class if it needs one and you do not define one. For more information on static constructors, see Use static members of a class. For more information on instance constructor overloading, see Overload methods and constructors.
In a class, it is an instance constructor that executes when the class is instantiated. Remember that the main block of a class is not allowed to have any executable statements. This restriction assures that all initialization of the class is done inside its instantiating constructor. The presence of a constructor as an explicitly-defined, special-purpose method allows you to take advantage of instantiation mechanisms, such as invoking a specific chain of constructors in a class hierarchy when the class is instantiated.
The access mode for an instance constructor can be PRIVATE,
PROTECTED, or PUBLIC. PUBLIC is the
default. In order for a class to be instantiated using the NEW function, the
class must support either an explicitly defined PUBLIC constructor or rely on
the built-in default constructor. When a class or procedure creates an instance of a class
with the NEW function, it is effectively telling the AVM to run a particular
constructor in the other class, as specified by its unique signature in the class. (For more
information, see Create a class instance.)
If no instance constructor of a class is PUBLIC,
the class cannot be referenced by the NEW function
outside of its own definition, or the definition of one of its subclasses,
because it has not defined itself as publicly accessible. You might
want to define a PROTECTED constructor if the class
can only be created as part of an object hierarchy. (For an example
of a PROTECTED constructor, see Construct an object.) When you define every constructor
of a class as PROTECTED, that class cannot be directly
instantiated by another class. It can only be instantiated by one
of its subclasses. A PROTECTED constructor is appropriate
where a class defines standard behavior to be inherited by one or
more other classes, but where the super class, by itself, does not
provide a complete class definition.
Any class constructor that is PRIVATE can only
be invoked from another constructor of the defining class (using
the THIS-OBJECT statement) or from an instance
of the defining class. You might define a PRIVATE constructor where
you want to encapsulate behavior shared and accessed only by other constructors
during class instantiation. From outside the class hierarchy, you can
never explicitly invoke a constructor. When you instantiate a class,
a PUBLIC constructor is invoked implicitly, as
specified by its signature. It is illegal to explicitly invoke a
constructor from any method other than from another constructor
in the same class (using the THIS-OBJECT statement)
or from a constructor of a subclass (using the SUPER statement).
Like any method, you can raise ERROR on the
statement that invokes a constructor. Any instance constructor that
executes a RETURN ERROR or an UNDO, THROW that
returns ERROR from the constructor block immediately
halts class instantiation, destroys any class instances that have
already completed construction in the class hierarchy, and raises
the ERROR condition on the statement that instantiated
the class. After the statement that instantiated the class returns
control to the instantiating block, any RETURN-VALUE setting
or specified error object is available for access. Constructors
can also raise ERROR when the AVM cannot instantiate
the class because of run-time failures such as missing class files
or other broken references. For more information on handling errors
returned from constructors, see Raise and handle error conditions.