Methods Hold Value of its Declared Class

Methods Hold Value of its Declared Class

Method of a super class always holds super class attributes declared in public/protected/private sections. If the method is redefined in any of its sub class then only it can modify the value of its attributes. Otherwise the method always holds the value of that class where it has been declared.

Here is a program where we have defined parent class where we have defined a method m_par in public and an attribute with value in private section. In the implementation of the method we are writing the private data. Then we have defined a child class inheriting from the parent class. There we have defined an attribute with value and a method in public section. Now at the time of implementation of the method we just call the parent class method. Finally we are creating the object of child class and calling the child class method. Here we are finding the output which holds the value of the parent class attribute, not the child class attribute. Hence we can say that the super class method holds super class attribute if the method is not redefined in the sub class.


*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
  PUBLIC SECTION.
    METHODS m_par.
  PRIVATE SECTION.
    DATA v_txt TYPE char50 VALUE 'Private Data from Parent Class'.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    DATA v_txt TYPE char50 VALUE 'SAP ABAP OOPs'.
    METHODS m_chi.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_par.
    WRITE / v_txt.
  ENDMETHOD.                    "m_par
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_chi.
    CALL METHOD m_par.
  ENDMETHOD.                    "m_chi
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA obj_chi TYPE REF TO child.
  CREATE OBJECT obj_chi.
  CALL METHOD obj_chi->m_chi.


In debugging mode we can see whenever the control goes to CALL METHOD obj_chi->m_chi then it goes to the child class method m_chi. At this point v_txt holds the data of child class, 'SAP ABAP OOPs'

But whenever it goes to CALL METHOD m_par inside the m_chi then the control goes to the parent class method m_par. Now herev_txt holds the data of parent class, 'Private Data from Parent Class'.

Below is the Output:

Share this

Related Posts