Try OpenEdge Now
skip to main content
Object-oriented Programming
Object-oriented Programming and ABL : Overview of class-based ABL : Defining constructors
 

Defining constructors

A class can define a special method called a constructor, which allows you to execute behavior and initialize data members and properties during the instantiation of a class. ABL allows you to define a constructor as a named block that always begins with the CONSTRUCTOR statement and always ends with the END CONSTRUCTOR statement. A constructor must have the same name as the name of the class in which it is defined and it can only execute when the class is instantiated. A constructor can be defined with an access mode of PRIVATE, PROTECTED, or PUBLIC, where PUBLIC is the default.
Where and how a constructor can be invoked depends on its access mode:
*PRIVATE — A private constructor can be invoked from another constructor of the class in which it is defined, and an instance can directly invoke a private constructor of its own class.
*PROTECTED — A protected constructor can be invoked by another constructor of the class or from a constructor of a subclass. An instance can invoke a protected constructor as long as the constructor is defined in the same class as the instance or a super class of the instance. If all constructors of the class are defined as PROTECTED, this class can only be instantiated indirectly when you directly instantiate a subclass of this class.
*PUBLIC — A public constructor can be invoked from inside or outside the class hierarchy.
A constructor can also have parameters, but no defined return type. You can define more than one constructor (overload constructors) in a class as long as the calling signature of each constructor is unique.
ABL also supports a static constructor that initializes static data for a class type instead of instance data (for a class instance) as with instance constructors. Unless otherwise specified, any reference to a constructor in this manual refers to an instance constructor. For more information on static constructors, see Using static members of a class.
For information on the ABL to instantiate a class, see Creating and destroying a class instance. For more information on defining constructors, see Defining class constructors.
* Comparison with procedure-based programming