Friend Class in SAP ABAP

Introduction:

In the previous article, we have discussed the classes, its components and the visibility of components. As per the visibility section, only public methods or attributes of a class are allowed to be used by some other classes. Now, what if we want to access non-public (protected or private) components (methods or attributes) of a class, to solve this problem, OOABAP has a concept of ‘Friend Class’.

‘Friend’ in a real life is a person, with whom you can share your secrets. Just like that, in OOABAP, if a class is declared to be a friend of some other class, then it can access the non- public components of that class.

Definition:

The class to which friendship is granted can access all the components (irrespective of components visibility) of the friendship granting class.

This Friendship is unilateral though, that means the friendship granted class cannot access the private and protected components of the class, to which friendship is granted.

Example:

Suppose, there is one global class ‘CL_DEMO_TEST_FRND’ class, this class is having 2 private methods. Now, I want to create one test class (ABAP Unit Test Class) for this global class say, LTC_DEMO_TEST_FRND, the task of this class is to check the code coverage of a global class, that means this test class should have access of all the components of a global class. We will make this local test class ‘LTC_DEMO_TEST_FRND’ a local friend of global class ‘CL_DEMO_TEST_FRND’ so that its private methods can be tested.

Syntax: 

CLASS LTC_DEMO_TEST_FRND DEFINITION DEFERRED.
CLASS
CL_DEMO_TEST_FRND DEFINITION LOCAL FRIENDS LTC_DEMO_TEST_FRND.

NOTE: In the above example, keyword ‘Definition Deferred’ is used, it makes the class ‘LTC_DEMO_TEST_FRND’ known without having a definition.

Program:

Scenario: There are 2 classes, class ‘DEPARTMENT’  having private attribute ‘NAME’. The other class ‘EMPLOYEE’ having one method ‘SET_NAME’. Class ‘EMPLOYEE’ needs the private attribute ‘NAME’ to set the employee name.

In the first program, class ‘EMPLOYEE’ is trying to access the private attributes of class ‘DEPARTMENT’, without declaring ‘EMPLOYEE’ as a friend of ‘DEPARTMENT’, and hence it is giving an error.

ABAP Friend Class Example 1
ABAP Friend Class Example 1

After declaring ‘EMPLOYYE’ friend of ‘DEPARTMENT’. Now, ‘EMPLOYEE’ class can access the private attributes of class ‘DEPARTMENT’.

ABAP Friend Class Example 2
ABAP Friend Class Example 2

Advantages of Friend Class in SAP ABAP:

  1. Allows sharing non-public information of a class by a non-member function.
  2. The sub-classes and interfaces of a class (to which friendship is granted), can also access the non-public components of a class (friendship granting class).
  3. With the help of friend class, we can use the additional functionality, kept outside the class.
  4. We can use the data which is not being used by that class.

Disclaimer:  All the images here are taken from Google, and the Admin has no possession over it.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.