METHODS constructor [FINAL]
[IMPORTING parameters [PREFERRED PARAMETER p]]
[{RAISING|EXCEPTIONS} exc1 exc2 ...].
1. ... IMPORTING parameters
2. ... RAISING exc1 exc2 ...
3. ... EXCEPTIONS exc1 exc2 ...
4. ... FINAL
: This statement declares the instance constructor constructor of the class. For public and protected instantiatable classes, this is only possible in the public visibility section of the declaration section of a class. If you use the addition CREATE PRIVATE in the CLASS DEFINITION statement, this is possible in all visibility sections.
Each class has a predefined method called constructor. By declaring this explicitly, the interface of the method constructor can be defined specifically for a class, and its functions can be implemented. Without explicit declaration, the instance constructor assumes the parameter interface of the direct superclass, and calls it implicitly.
If the instance constructor is implemented in a subclass, the instance constructor of the superclass must be called explicitly using the pseudo reference super->constructor, even if the latter is not explicitly declared. Exceptions to this are direct subclasses of the root node object. Before the superclass contructor is called, an instance constructor only has access to the static components of its class. After the superclass constructor is called, it can also access instance components.
For each instance of a class, the instance constructor is called only once using the statement CREATE OBJECT immediately after it has been generated. For the call, appropriate actual parameters must be assigned to all non-optional input parameters, return values can be assigned to non-class-based exceptions, and class-based exceptions can be declared. It is not possible to call the instance constructor using CALL METHOD, except when calling the superclass constructors using super->constructor in the redefined constructor of a subclass.
During execution of an instance constructor, the current instance temporarily assumes the type of the class in which the constructor is defined. This has the following consequences:
... IMPORTING parameters
... RAISING exc1 exc2 ...
... EXCEPTIONS exc1 exc2 ...
: Using the IMPORTING addition, input parameters can be
defined according to the same rules as for general methods. The additions
RAISING and EXCEPTIONS for the declaration
of class-based exceptions or the definition of non-class-based exceptions also have the same meaning as for general methods.
... FINAL
: Instance constructors are implicitly final. The addition FINAL can be specified, but it is not necessary.
In this example, the class c2 inherits from the class c1. In both classes, the instance constructor constructor is declared explicitly. It must therefore be implemented in both classes, whereby the implementation in c2 must include the call of the superclass constructor.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING p1 TYPE any.
...
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS constructor IMPORTING p2 TYPE any.
...
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD constructor.
...
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD constructor.
...
super->constructor( p2 ).
...
ENDMETHOD.
ENDCLASS.