Tuesday, April 2, 2013

Private Friends of a Class

A class can be defined as a private friend of another class. The statement is like this: CLASS cls2 DEFINITION CREATE PRIVATE FRIENDS cls1. Here cls2 is the private friend class of cls1. Now the requisites are:

1. cls1 can use all protected and private components of cls2. 
2. cls1 can instantiate cls2 in its method.

Here we want to create a friend class and data and method in protected and private section. This friend class will be Instantiated from another class method and it will access all of its data and methods.

The following program contains a class cls2 which is a private friend class of cls1. Now we have defined protected data and private data in class cls2. We also have defined a private method m_cls2 here. The method m_cls2 displays the protected and private data. 

Now we have a public method m_cls1 of class cls1. In the implementation of m_cls1 we have instantiated friend class and called method m_cls2. Then we have changed the value of protected and private data and display those again.

Finally in the start of selection we just have instantiated the class cls1 and called the method m_cls1. Hence we don't have to instantiate the friend class again as it has been instantiated before.


*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    METHODS m_cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION CREATE PRIVATE FRIENDS cls1.
  PROTECTED SECTION.
    DATA v_pro TYPE char50.
  PRIVATE SECTION.
    DATA v_txt TYPE char50.
    METHODS m_cls2.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    DATA obj2 TYPE REF TO cls2.
    CREATE OBJECT obj2.
    CALL METHOD obj2->m_cls2.

    obj2->v_pro = 'Protected Data of First Class'.
    obj2->v_txt = 'Private Data of First Class'.
    WRITE: / obj2->v_pro,
           / obj2->v_txt.
  ENDMETHOD.                                                "m_cls1
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD m_cls2.
    v_pro = 'Protected Data of Friend Class'.
    v_txt = 'Private Data of Friend Class'.
    WRITE: / 'Private Method:',
           / v_pro,
           / v_txt.
    SKIP.
  ENDMETHOD.                                                "m_cls2
ENDCLASS.                    "cls2 IMPLEMENTATION

START-OF-SELECTION.
  DATA obj1 TYPE REF TO cls1.
  CREATE OBJECT obj1.
  CALL METHOD obj1->m_cls1.


The output is as following:



Below is another version of the previous program and that will produce the same output. Here Definition Deferred has been used for defining cls1 class. The logic is same and we can use this procedure also. Here we have defined the friend class in another way like CLASS cls2 DEFINITION FRIENDS cls1.

*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

CLASS cls1 DEFINITION DEFERRED.

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION FRIENDS cls1.
  PROTECTED SECTION.
    DATA v_pro TYPE char50.
  PRIVATE SECTION.
    DATA v_txt TYPE char50.
    METHODS m_cls2.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD m_cls2.
    v_pro = 'Protected Data of Friend Class'.
    v_txt = 'Private Data of Friend Class'.
    WRITE: / 'Private Method:',
           / v_pro,
           / v_txt.
    SKIP.
  ENDMETHOD.                                                "m_cls2
ENDCLASS.                    "cls2 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    METHODS m_cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    DATA obj2 TYPE REF TO cls2.
    CREATE OBJECT obj2.
    CALL METHOD obj2->m_cls2.

    obj2->v_pro = 'Protected Data of First Class'.
    obj2->v_txt = 'Private Data of First Class'.
    WRITE: / obj2->v_pro,
           / obj2->v_txt.
  ENDMETHOD.                                                "m_cls1
ENDCLASS.                    "cls1 IMPLEMENTATION

START-OF-SELECTION.
  DATA obj1 TYPE REF TO cls1.
  CREATE OBJECT obj1.
  CALL METHOD obj1->m_cls1.

11 comments:

yektek training said...

nice post thanks for sharing from Swathi

siva sankar Sankar said...

excellent post from Sankar

Ramya k said...

Thanks for the great information in your blog SAP SF Training in Chennai

Bhavya Kumar said...

Very Informative blogSAP Success FactorsTraining in Chennai

Unknown said...

nice

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

sameera sam said...


It's so nice article thank you for sharing a valuable content
Sap Online Access

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

calfre services said...

very usefull content thanks for sharing sap hr abap online classes

SieveSoftware said...

Great Post. The information provided is of great use as I got to learn new things. Keep Blogging.
SAP ABAP TRAINING IN HYDERABAD