Subclasses of a friend class can access all of its class granted friendship. So subclasses 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 ABC is granting friendship of XYZ class then child class will get access to all protected and private elements of class ABC.
In the following program, we have defined a class ABC which is a friend of XYZ class. We have declared protected and private data and a private method M_ABC here. Now we are implementing the ABC class by displaying the data in the method M_ABC.
Next, we have defined XYZ class which holds a public method M_XYZ. In the implementation part, we have instantiated ABC class and called method M_ABC. 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 XYZ and declared a public method M_CHILD. In the implementation part, we have instantiated ABC class again and called the XYZ class method M_XYZ. Now we have called XYZ method M_XYZ and modified the data again accordingly. After that, we are displaying the current data. Hence the child method contains the display of XYZ method and child method.
Finally, in a 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 zprivate_friendsubclass_access.
CLASS xyz DEFINITION DEFERRED.
*----------------------------------------------------------------------*
* CLASS ABC DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc DEFINITION FRIENDS xyz.
PROTECTED SECTION.
DATA v_pro TYPE char50.
PRIVATE SECTION.
DATA v_pri TYPE char50.
METHODS m_abc.
ENDCLASS. "ABC DEFINITION
*----------------------------------------------------------------------*
* CLASS ABC IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc IMPLEMENTATION.
METHOD m_abc.
v_pro = 'Protected Data'.
v_pri = 'Private Date'.
WRITE: / 'Private Method:',
/ v_pro,
/ v_pri.
SKIP.
ENDMETHOD. "m_ABC
ENDCLASS. "ABC IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS XYZ DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS xyz DEFINITION.
PUBLIC SECTION.
METHODS m_par.
ENDCLASS. "XYZ DEFINITION
*----------------------------------------------------------------------*
* CLASS XYZ IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS xyz IMPLEMENTATION.
METHOD m_par.
DATA obj TYPE REF TO abc.
CREATE OBJECT obj.
CALL METHOD obj->m_abc.
obj->v_pro = 'Protected - Accessed by XYZ'.
obj->v_pri = 'Private - Accessed by XYZ'.
WRITE: / 'XYZ Method:',
/ obj->v_pro,
/ obj->v_pri.
SKIP.
ENDMETHOD. "m_par
ENDCLASS. "XYZ IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM xyz.
PUBLIC SECTION.
METHODS m_child.
ENDCLASS. "child DEFINITION