Tuesday, April 2, 2013

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  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:

8 comments:

Bhavya Kumar said...

Thanks for the great information in your blog SAP Success FactorsTraining in Chennai

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

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

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

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

Priya Kannan said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

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

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

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

SAP TECH said...

Really good information.. Keep up the good work..

Jugul Donald said...

Visit our SAP-ABAP , OO-ABAP, HANA tutorial

Anonymous said...

Keep up the good work!!!

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD

Anonymous said...

Keep up the good work!!!