Define constructors
- Last Updated: February 14, 2020
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
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 any access mode. PUBLIC is the default.
Where and how a constructor can be invoked depends on its access mode:
-
PRIVATE— 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. -
PACKAGE-PRIVATE— Can be called from the defining class or any class within its package. This is done via theSUPERstatement within another constructor or using theNEWstatement or function elsewhere. -
PROTECTED— 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 asPROTECTED, this class can only be instantiated indirectly when you directly instantiate a subclass of this class. PACKAGE-PROTECTED—Can be called from the defining class, from a subclass, or from any class within its package. This is done via the SUPER statement within another constructor or using the NEW statement or function elsewhere.PUBLIC— 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 Use static members of a class.
For information on the ABL to instantiate a class, see Create and destroy a class instance. For more information on defining constructors, see Define class constructors.