Global Data in OOPs Concept
Global Data in OOPs Concept
Global data is accessible from any class in a program. We know that the visibility depends on the public, protected or private section. The globally declared variable is visible from anywhere in the program. We have created a program where the global variable is v_globe and in the debugging mode we can see its value from any of classes.
REPORT zsr_test NO STANDARD PAGE HEADING.
DATA: v_globe TYPE char40 VALUE 'This is Global Data'.
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
DATA v_pub_1 TYPE char40 VALUE 'Class 1 public data'.
METHODS m_cls1.
PROTECTED SECTION.
DATA v_pro_1 TYPE char40 VALUE 'Class 1 protected data'.
PRIVATE SECTION.
DATA v_pri_1 TYPE char40 VALUE 'Class 1 private data'.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
PUBLIC SECTION.
DATA v_pub_2 TYPE char40 VALUE 'Class 2 public data'.
METHODS m_cls2.
PROTECTED SECTION.
DATA v_pro_2 TYPE char40 VALUE 'Class 2 protected data'.
PRIVATE SECTION.
DATA v_pri_2 TYPE char40 VALUE 'Class 2 private data'.
ENDCLASS. "cls2 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD m_cls1.
WRITE: / v_pub_1,
/ v_pro_1,
/ v_pri_1.
ENDMETHOD. "m_cls1
ENDCLASS. "cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
METHOD m_cls2.
WRITE: / v_pub_2,
/ v_pro_2,
/ v_pri_2.
ENDMETHOD. "m_cls2
ENDCLASS. "cls2 IMPLEMENTATION
START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1,
obj2 TYPE REF TO cls2.
CREATE OBJECT: obj1, obj2.
CALL METHOD: obj1->m_cls1,
obj2->m_cls2.
DATA: v_globe TYPE char40 VALUE 'This is Global Data'.
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
DATA v_pub_1 TYPE char40 VALUE 'Class 1 public data'.
METHODS m_cls1.
PROTECTED SECTION.
DATA v_pro_1 TYPE char40 VALUE 'Class 1 protected data'.
PRIVATE SECTION.
DATA v_pri_1 TYPE char40 VALUE 'Class 1 private data'.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
PUBLIC SECTION.
DATA v_pub_2 TYPE char40 VALUE 'Class 2 public data'.
METHODS m_cls2.
PROTECTED SECTION.
DATA v_pro_2 TYPE char40 VALUE 'Class 2 protected data'.
PRIVATE SECTION.
DATA v_pri_2 TYPE char40 VALUE 'Class 2 private data'.
ENDCLASS. "cls2 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD m_cls1.
WRITE: / v_pub_1,
/ v_pro_1,
/ v_pri_1.
ENDMETHOD. "m_cls1
ENDCLASS. "cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
METHOD m_cls2.
WRITE: / v_pub_2,
/ v_pro_2,
/ v_pri_2.
ENDMETHOD. "m_cls2
ENDCLASS. "cls2 IMPLEMENTATION
START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1,
obj2 TYPE REF TO cls2.
CREATE OBJECT: obj1, obj2.
CALL METHOD: obj1->m_cls1,
obj2->m_cls2.
Now we have set the debugger at the CALL METHOD. The method of first class can access public, protected & private variables and the global variable from that first class implementation.
Similarly when the program enters to the second class implementation, the method can access all variables of that class and the global variable.
When the program is not under any class then also global variable can be accessed.
The output is as follows.
Post a Comment