OOPS ABAP: Constructors With Example
Constructors:

- Constructor is a special type of method which is executed automatically when a object is created.
- It cannot be called using CALL METHOD.
- Constructors are methods with a predefined name.
- Multiple CONSTRUCTOR and CLASS_CONSTRUCTOR are not allowed with in a class.
Types of constructors:
CONSTRUCTOR (Instance Constructor) |
CLASS_CONSTRUCTOR (Static Constructor) |
Method name: CONSTRUCTOR |
Method name: CLASS_CONSTRUCTOR |
It contains import parameters only |
It does not contain any parameters |
static and instance attributes can be accessed |
Only static attributes can be accessed |
It is called every time when a object is created |
It is called first time when a object is created |
Used to set a default value for particular instance |
|
Example:
*&---------------------------------------------------------------------*
*& Report ZVB_CONSTRUCTOR
*&---------------------------------------------------------------------*
REPORT zvb_constructor.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING num TYPE i
EXCEPTIONS e1.
CLASS-METHODS: class_constructor.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Class (Implementation) C1
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD constructor.
IF num LT 1.
RAISE e1.
ELSE.
WRITE:/ num.
ENDIF.
ENDMETHOD.
METHOD class_constructor.
WRITE: 'Static constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: lo_obj TYPE REF TO c1.
CREATE OBJECT lo_obj
EXPORTING
num = 21
EXCEPTIONS
e1 = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'Num can not be zero'.
ENDIF.
*& Report ZVB_CONSTRUCTOR
*&---------------------------------------------------------------------*
REPORT zvb_constructor.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING num TYPE i
EXCEPTIONS e1.
CLASS-METHODS: class_constructor.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Class (Implementation) C1
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD constructor.
IF num LT 1.
RAISE e1.
ELSE.
WRITE:/ num.
ENDIF.
ENDMETHOD.
METHOD class_constructor.
WRITE: 'Static constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: lo_obj TYPE REF TO c1.
CREATE OBJECT lo_obj
EXPORTING
num = 21
EXCEPTIONS
e1 = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'Num can not be zero'.
ENDIF.
Output: Static constructor
21
Static Constructor (Class_Constructor):
Static or Class constructors are triggered in following cases:
•When the object is instantiated for the class.
•When static attributes (class=>a) or methods (class=>method) are accessed from the class.
•Registering a static event handler method using SET HANDLER class=>meth for obj.
•Registering an event handler method of static event of the class.
Static or Class constructors are triggered in following cases:
•When the object is instantiated for the class.
•When static attributes (class=>a) or methods (class=>method) are accessed from the class.
•Registering a static event handler method using SET HANDLER class=>meth for obj.
•Registering an event handler method of static event of the class.
You can create instance and static constructors by clicking the 'Constructor' and 'Class constructor' buttons in SE24.
Comments
Post a Comment