Thursday, March 28, 2013

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 here v_txt holds the data of parent class, 'Private Data from Parent Class'.

Below is the Output:

1 comment:

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!