banner ads

Sub Classes of Friend Class

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 ZTST_SWP_FRD_CL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztst_swp_frd_cl.

CLASS parent DEFINITION.

  
PUBLIC SECTION.
  
METHODs m_parent..

endclass.

CLASS child DEFINITION INHERITING FROM parent.
  
PUBLIC SECTION.
  
METHODs m_child.

ENDCLASS.

CLASS other DEFINITION FRIENDS parent.

  
PROTECTED SECTION.
    
DATA v_pro TYPE char50.

  
PRIVATE SECTION.
    
DATA v_pri TYPE char50.
    
METHODS m_other..


ENDCLASS.

CLASS other IMPLEMENTATION.

  
METHOD m_other.

  v_pro 
'Protected variable from main class'.
  v_pri 
'private variable from main class'.

  
WRITE 'Main class private method',
         / v_pro 
,
         / v_pri 
.


  
ENDMETHOD.

ENDCLASS.


CLASS parent IMPLEMENTATION.

  
METHOD m_parent.

    
DATA obj TYPE REF TO other.
    
CREATE OBJECT obj.

  
CALL METHOD obj->m_other.

skip.
    obj
->v_pro 'protected varaiable access by parent'.
    obj
->v_pri 'private varaiable access by parent'.

    
WRITE 'Parent class method',
      / obj
->v_pro ,
      / obj
->v_pri.

  
ENDMETHOD.

ENDCLASS.

CLASS child IMPLEMENTATION.


  
METHOD m_child .

   
DATA obj TYPE REF TO other.
    
CREATE OBJECT obj.

  
CALL METHOD m_parent.
  
skip.
    obj
->v_pro 'protected varaiable access by child'.
    obj
->v_pri 'private varaiable access by child'.

    
WRITE 'child class method',
      / obj
->v_pro ,
      / obj
->v_pri.

 
ENDMETHOD.

  
ENDCLASS.

START-OF-SELECTION.

DATA obj_ch TYPE REF TO child.
create OBJECT obj_ch.

call METHOD obj_ch->m_child.



The output is following:



No comments