Instantiating a Class within another Class's Implementation
One class can be instantiated within the implementation of another class. Here the program contains two different classes cls1 and cls2. In the implementation of cls2 we are creating object obj1 ref to cls1 and then calling method of class 1 inside the 2nd class implementation.
REPORT zsr_test NO STANDARD PAGE
HEADING.
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
DATA v_cls1 TYPE char40 VALUE 'Class one data'.
METHODS m_cls1.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
PUBLIC SECTION.
DATA v_cls2 TYPE char40 VALUE 'Class two data'.
METHODS m_cls2.
ENDCLASS. "cls2 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD m_cls1.
WRITE: / v_cls1.
ENDMETHOD.
ENDCLASS.
*----------------------------------------------------------------------*
* CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
METHOD m_cls2.
DATA obj1 TYPE REF TO cls1.
CREATE OBJECT obj1.
CALL METHOD obj1->m_cls1.
WRITE: / v_cls2.
ENDMETHOD.
ENDCLASS.
DATA obj2 TYPE REF TO cls2.
START-OF-SELECTION.
CREATE OBJECT: obj2.
CALL METHOD obj2->m_cls2.
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
DATA v_cls1 TYPE char40 VALUE 'Class one data'.
METHODS m_cls1.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
PUBLIC SECTION.
DATA v_cls2 TYPE char40 VALUE 'Class two data'.
METHODS m_cls2.
ENDCLASS. "cls2 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD m_cls1.
WRITE: / v_cls1.
ENDMETHOD.
ENDCLASS.
*----------------------------------------------------------------------*
* CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
METHOD m_cls2.
DATA obj1 TYPE REF TO cls1.
CREATE OBJECT obj1.
CALL METHOD obj1->m_cls1.
WRITE: / v_cls2.
ENDMETHOD.
ENDCLASS.
DATA obj2 TYPE REF TO cls2.
START-OF-SELECTION.
CREATE OBJECT: obj2.
CALL METHOD obj2->m_cls2.
IF you go for debugging , then you can see that system first calls m_class2 method and it holds the value of v_cls2 inside m_cls2