Mentor SAP
2018-02-02 Submitted by:- Admin

Here we have a program where we have defined a class cls1. In the public section, we are declaring static data and method. Then we have implemented the class cls1 with the method. The method will write the static data. Next, we declare second class cls2 and third class cls3 inheriting from cls1. In the start of selection, we give value to the static data and call the static method. Here the point is that we give value to the cls3 class data and we call a method of cls2. In spite of this, we are getting the same value as an output because cls2 and cls3 are the subclasses of cls1.

Hence we can say that the static attributes exist only once in each Inheritance tree. We can change the value of static data from the outside only one time and that change will reflect all other subclasses. So they will be visible to other classes in the Inheritance tree. 

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA v_txt TYPE char50.
    CLASS-METHODS m_cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    WRITE / v_txt.
  ENDMETHOD.                                                "m_cls1
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION INHERITING FROM cls1.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls3 DEFINITION INHERITING FROM cls1.
ENDCLASS.                    "cls3 DEFINITION

START-OF-SELECTION.
  cls3=>v_txt = 'SAP ABAP Object Oriented'.
  CALL METHOD cls2=>m_cls1.

Below is the Output: