Static constructor of a class is called only one time in a program. In this matter when a sub class is accessed then its static constructor is called only once in that program. Here it needs to be remembered that whenever a static constructor is executed the system checks any of its super class constructor has been executed or not. Now if the super class constructor has not been executed then the system will execute that first then the sub class constructor will be executed.
Here we have a program where we have defined a parent class with a static constructor method. We have implemented it with a specific functionality. Next we have created a child class with having the same static constructor but we have implemented it differently. Similarly we have created a grandchild class with having the same static constructor but we have implemented it more differently. Now in the start of selection we have instantiated the grandchild class only. In the output we can get both the functions of parent class and child class and also grandchild class static constructor methods. The output will come with this order: Parent->Child->Grandchild. Here the object of parent and child class need not to be created to view the output.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
PUBLIC SECTION.
CLASS-DATA v_txt TYPE char50.
CLASS-METHODS class_constructor.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD class_constructor.
v_txt = 'Parent Class Static Constructor'.
WRITE / v_txt.
ENDMETHOD. "class_constructor
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD class_constructor.
v_txt = 'Child Class Static Constructor'.
WRITE / v_txt.
ENDMETHOD. "class_constructor
ENDCLASS. "child IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS grandchild DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild DEFINITION INHERITING FROM child.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
ENDCLASS. "grandchild DEFINITION
*----------------------------------------------------------------------*
* CLASS grandchild IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild IMPLEMENTATION.
METHOD class_constructor.
v_txt = 'Grandchild Class Static Constructor'.
WRITE / v_txt.
ENDMETHOD. "class_constructor
ENDCLASS. "grandchild IMPLEMENTATION
START-OF-SELECTION.
DATA obj_gchi TYPE REF TO grandchild.
CREATE OBJECT obj_gchi.
Below is the Output:
Here we have a program where we have defined a parent class with a static constructor method. We have implemented it with a specific functionality. Next we have created a child class with having the same static constructor but we have implemented it differently. Similarly we have created a grandchild class with having the same static constructor but we have implemented it more differently. Now in the start of selection we have instantiated the grandchild class only. In the output we can get both the functions of parent class and child class and also grandchild class static constructor methods. The output will come with this order: Parent->Child->Grandchild. Here the object of parent and child class need not to be created to view the output.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
PUBLIC SECTION.
CLASS-DATA v_txt TYPE char50.
CLASS-METHODS class_constructor.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD class_constructor.
v_txt = 'Parent Class Static Constructor'.
WRITE / v_txt.
ENDMETHOD. "class_constructor
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD class_constructor.
v_txt = 'Child Class Static Constructor'.
WRITE / v_txt.
ENDMETHOD. "class_constructor
ENDCLASS. "child IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS grandchild DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild DEFINITION INHERITING FROM child.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
ENDCLASS. "grandchild DEFINITION
*----------------------------------------------------------------------*
* CLASS grandchild IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild IMPLEMENTATION.
METHOD class_constructor.
v_txt = 'Grandchild Class Static Constructor'.
WRITE / v_txt.
ENDMETHOD. "class_constructor
ENDCLASS. "grandchild IMPLEMENTATION
START-OF-SELECTION.
DATA obj_gchi TYPE REF TO grandchild.
CREATE OBJECT obj_gchi.
Below is the Output:
No comments:
Post a Comment