Sub classes of a friend class can access all of its class granted friendship. So sub classes can get all the protected and private elements of the class which is granting friendship of the super class. As an example if a class cls is granting friendship of parent class then child class will get access of all protected and private elements of class cls.
In the following program we have defined a class cls which is a friend of parent class. We have declared protected and private data and a private method m_cls here. Now we are implementing the cls class by displaying the data in the method m_cls.
Next we have defined parent class which holds a public method m_par. In the implementation part we have instantiated cls class and called method m_cls. Then we have modified the data and display those as well. Hence this method will display the previous version and also the current version of the output.
Next we have defined a child class inheriting from parent and declared a public method m_chi. In the implementation part we have instantiated cls class again and called the parent class method m_par. Now we have called parent method m_par and modified the data again accordingly. After that we are displaying the current data. Hence the child method contains the display of parent method and child method.
Finally in start of selection we just have instantiated the child object and then called the child method. The output will contain all previous versions of the protected and private data.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
CLASS parent DEFINITION DEFERRED.
*----------------------------------------------------------------------*
* CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION FRIENDS parent.
PROTECTED SECTION.
DATA v_pro TYPE char50.
PRIVATE SECTION.
DATA v_pri TYPE char50.
METHODS m_cls.
ENDCLASS. "cls DEFINITION
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD m_cls.
v_pro = 'Protected Data'.
v_pri = 'Private Date'.
WRITE: / 'Private Method:',
/ v_pro,
/ v_pri.
SKIP.
ENDMETHOD. "m_cls
ENDCLASS. "cls IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
PUBLIC SECTION.
METHODS m_par.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD m_par.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
CALL METHOD obj->m_cls.
obj->v_pro = 'Protected - Accessed by Parent'.
obj->v_pri = 'Private - Accessed by Parent'.
WRITE: / 'Parent Method:',
/ obj->v_pro,
/ obj->v_pri.
SKIP.
ENDMETHOD. "m_par
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
METHODS m_chi.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_chi.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
CALL METHOD m_par.
obj->v_pro = 'Protected - Accessed by Child'.
obj->v_pri = 'Private - Accessed by Child'.
WRITE: / 'Child Method:',
/ obj->v_pro,
/ obj->v_pri.
SKIP.
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.
The output is following:
3 comments:
Thanks for the great information in your blog SAP Success FactorsTraining in Chennai
Really good information.. Keep up the good work..
Visit our SAP-ABAP , OO-ABAP, HANA tutorial
Post a Comment