Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarative statemnts →  Classes and Interfaces →  Components in Classes and Interfaces →  Methods →  METHODS 

METHODS - constructor

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


METHODS constructor [FINAL] 
  [IMPORTING parameters [PREFERRED PARAMETER p]]
  [{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}
  |{EXCEPTIONS exc1 exc2 ...}].

Extras

1. ... IMPORTING parameters

2. ... RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...

3. ... EXCEPTIONS exc1 exc2 ...
4. ... FINAL

Effect

This statement declares the instance constructor constructor of a class. In a local class, it can be specified in all visibility sections that have more general instantiability than or the same instantiability specified in the CREATE addition of the statement CLASS DEFINITION:

  CREATE PUBLIC CREATE PROTECTED CREATE PACKAGE CREATE PRIVATE
PUBLIC SECTION X X X X
PROTECTED SECTION - X X X
PACKAGE SECTION - - X X
PRIVATE SECTION - - - X

In a global class, the instance constructor must always be declared in the public visibility section, for technical reasons. In principle, the constructor can be declared in the same visibility sections as in the table above, however this can cause unexpected syntax errors when the class is used, due to the way it is organized internally.

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 super->constructor, even if the latter is not explicitly declared. The only exceptions to this are direct subclasses of the root node object. The following restrictions apply before the superclass constructor is called:

  • The instance constructor does not have access to the instance components of its class. The self-reference me-> cannot be used. The static components of its class can be accessed only directly.
  • Befor the superclass constructor is called, an instance constructor cannot be exited using statements such as RETURN or CHECK.

After the superclass constructure has been called, the self-reference me-> can be used and instance components can be accessed.

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 codes 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.

When an instance constructor is executed, the current instance temporarily assumes the type of the class in which the constructor is defined. This has the following consequences:

  • If methods are called when a superclass constructor is executed, the implementations of the superclass are executed and not the redefinitions of subclasses. Specifying me->, for addressing a redefined method in a subclass that has just been generated, has no effect.
  • Abstract methods of the same class cannot be called in an instance constructor.
  • When a superclass constructor is executed, attempts to access components of the subclass using a down cast lead to a runtime error.

Programming Guideline

Declare the instance constructor in the public visibility section


Notes

  • Instance constructors are an exception to the rule that all public components on a path in the inheritance hierarchy are in the same namespace. The instance constructor of each class has its own interface and its own implementation. An instance constructor cannot be redefined.
  • Instance constructors are declared in the public visibility section of a class purely for technical reasons. The actual visibility is controlled by the addition CREATE {PUBLIC|PROTECTED|PACKAGE|PRIVATE} of the statement CLASS DEFINITION. We recommend that the instance constructor of a local class is declared in the visibility section which matches its instantiability, because this allows the components declared there to be used in the constructor interface. For global classes, only the public section is feasible, however.
  • Before the superclass constructor is called, static components of the same class cannot be accessed using me-> in the instance constructor of a subclass. Access to the components without using me-> or using the class name and the class component selector => is always possible.

Addition 1

... IMPORTING parameters

Addition 2

... RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...

Addition 3

... EXCEPTIONS exc1 exc2 ...

Effect

The IMPORTING addition can be used to define input parameters 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.


Note

When the instance constructor is executed, the same applies for resumable exceptions as for all other methods. If processing can be resumed successfully, the creation of the object can also be resumed.

Addition 4

... FINAL

Effect

Instance constructors are implicitly final. The addition FINAL can be specified, but it is not necessary.


Example

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.