SAP Virtual/Onsite Trainings

Document Categories:

What's New?

Contribute?

Sample Specs

What's Hot?

Web Dynpro for ABAP Tutorials 


Join the Mailing List
Enter name and email address below:
Name:
Email:
Subscribe  Unsubscribe 

Concept of Friends in ABAP classes

By Ankit Doshi, TCS

This is a simple tutorial to demonstrate the concept of Friends in ABAP classes.  

There are certain special cases where a class would want access to other classes private attributes and methods in such scenario we can make use of the friends concept in classes.  

If a class A declares itself as a friend to class B then class B will have access to all the private and protected attributes and methods of class A.  

Let us understand this via an Example.  

Example:

Three classes have been defined abc_org(the organization) abc_employes(for employes of the organization abc) and abc_shareholders(for the shareholders of the organization abc).  

Both employs class and shareholders class inherit from the organization class.

The organization class has defined itself as a friend of employs class this will allow the employs class to access the private data of the organization class.  

The Organization class Definition.

 

  Employ class implementation.  

 

 

 

 

 

   

Here the employs class is accessing the private method display_avg_ctc of the organization class and it is able to do so because the organization class is friend with employ class.

Shareholder class implementation.

Here the class shareholder will not be able to access the private method of the parent class organization, if it tries to access the complier will throw the below error:  

 

Code:  

*&---------------------------------------------------------------------*
*& Report  YCL_FRIENDS
*&
*&---------------------------------------------------------------------*


REPORT  ycl_friends.

CLASS abc_employs DEFINITION DEFERRED.

*----------------------------------------------------------------------*
*       CLASS abc_org DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc_org DEFINITION FRIENDS abc_employs.

  PUBLIC SECTION.

    DATA : stock_price TYPE char10 VALUE 100.

    METHODS display_stock_price.

  PRIVATE SECTION.

    DATA : avg_ctc TYPE char10 VALUE 200.

    METHODS display_avg_ctc.

ENDCLASS.                    "abc_org DEFINITION

*----------------------------------------------------------------------*
*       CLASS abc_org IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc_org IMPLEMENTATION.

  METHOD display_stock_price.

    WRITE / stock_price.

    SKIP.

  ENDMETHOD.                    "display_stock_price

  METHOD display_avg_ctc.

    WRITE  / avg_ctc.

    SKIP.

  ENDMETHOD.                    "display_avg_ctc

ENDCLASS.                    "abc_org IMPLEMENTATION


*----------------------------------------------------------------------*
*       CLASS abc_employs DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc_employs DEFINITION INHERITING FROM abc_org.

  PUBLIC SECTION.

    METHODS : display_emp.

ENDCLASS.                    "abc_employs DEFINITION


*----------------------------------------------------------------------*
*       CLASS abc_employs IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc_employs IMPLEMENTATION.

  METHOD display_emp.

    DATA ref_empl TYPE REF TO abc_org.

    CREATE OBJECT ref_empl.

    CALL METHOD ref_empl->display_stock_price.
    CALL METHOD ref_empl->display_avg_ctc.

  ENDMETHOD.                    "display_emp

ENDCLASS.                    "abc_employs IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS abc_shareholder DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc_shareholder DEFINITION INHERITING FROM abc_org.

  PUBLIC SECTION.

    METHODS display_shr.

ENDCLASS.                    "abc_shareholder DEFINITION

*----------------------------------------------------------------------*
*       CLASS abc_shareholder IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc_shareholder IMPLEMENTATION.

  METHOD display_shr.

    DATA ref_shr TYPE REF TO abc_org.

    CREATE OBJECT ref_shr.

    CALL METHOD ref_shr->display_stock_price.
*    CALL METHOD ref_shr->display_avg_ctc. " This will throw an error.

  ENDMETHOD.                    "display_emp

ENDCLASS.                    "abc_shareholder IMPLEMENTATION

START-OF-SELECTION.

  DATA : ref_emp TYPE REF TO abc_employs,
         ref_shr TYPE REF TO abc_shareholder.

  CREATE OBJECT ref_emp.
  CREATE OBJECT ref_shr.

  CALL METHOD ref_emp->display_emp.

  CALL METHOD ref_shr->display_shr.
 

Output:  

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image