[go: up one dir, main page]

0% found this document useful (0 votes)
67 views2 pages

Friends Example

The document explains the friend concept in ABAP Objects, which allows one class to access the protected and private members of another class. It illustrates this with two classes, lcl_abc and lcl_xyz, where lcl_xyz is declared as a friend of lcl_abc, enabling it to access all methods including protected and private ones. The document includes code examples demonstrating the implementation and access levels of methods in both classes.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views2 pages

Friends Example

The document explains the friend concept in ABAP Objects, which allows one class to access the protected and private members of another class. It illustrates this with two classes, lcl_abc and lcl_xyz, where lcl_xyz is declared as a friend of lcl_abc, enabling it to access all methods including protected and private ones. The document includes code examples demonstrating the implementation and access levels of methods in both classes.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

*ABAP OOP: Friend Concept - Training Notes

*🎯 Objective:
*Understand how a class can access protected and private members of another class
using the friend concept in ABAP Objects.
*📌 Standard Access Levels Recap:
*Access Level Accessible Within Accessible From Subclass Accessible Outside
Class
*PUBLIC Yes Yes Yes
*PROTECTED Yes Yes No
*PRIVATE Yes No No
*🔍 Problem Scenario:
*You have two classes:

*- lcl_abc: Contains methods m1 (public), m2 (protected), and m3 (private).


*- lcl_xyz: Needs to access all methods of lcl_abc, including protected and private
ones.

*Without friend declaration:


*- You can call only m1 from lcl_xyz.
*- Calling m2 or m3 will cause syntax errors.
REPORT zfriends_concept.

CLASS lcl_xyz DEFINITION DEFERRED. "Must be deferred before lcl_abc uses it

CLASS lcl_abc DEFINITION


FRIENDS lcl_xyz. "<<< This line makes lcl_xyz a friend class

PUBLIC SECTION.
METHODS m1.
PROTECTED SECTION.
METHODS m2.
PRIVATE SECTION.
METHODS m3.
ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.


METHOD m1.
WRITE :/ 'Inside Instance Public method m1 of class lcl_abc....'.
ENDMETHOD.

METHOD m2.
WRITE :/ 'Inside Instance Protected method m2 of class lcl_abc....'.
ENDMETHOD.

METHOD m3.
WRITE :/ 'Inside Instance Private method m3 of class lcl_abc....'.
ENDMETHOD.
ENDCLASS.

CLASS lcl_xyz DEFINITION.


PUBLIC SECTION.
METHODS m4.
ENDCLASS.

CLASS lcl_xyz IMPLEMENTATION.


METHOD m4.
ULINE.
FORMAT COLOR 2.
WRITE :/ 'Object r of class lcl_abc created in lcl_xyz method m4...'.
DATA r TYPE REF TO lcl_abc.
CREATE OBJECT r.

CALL METHOD r->m1. "✅ Allowed


CALL METHOD r->m2. "✅ Now allowed due to FRIENDS
CALL METHOD r->m3. "✅ Now allowed due to FRIENDS

FORMAT COLOR OFF.


WRITE :/ 'Inside Instance Public method m4 of class lcl_xyz....'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

FORMAT COLOR 1.
WRITE :/ 'Object ob1 of class lcl_abc created outside...'.

DATA ob1 TYPE REF TO lcl_abc.


CREATE OBJECT ob1.

CALL METHOD ob1->m1. "✅ Allowed


* CALL METHOD ob1->m2. "❌ Still Not Allowed Outside
* CALL METHOD ob1->m3. "❌ Still Not Allowed Outside
FORMAT COLOR OFF.

ULINE.

FORMAT COLOR 7.
WRITE :/ 'Object ob2 of class lcl_xyz...'.

DATA ob2 TYPE REF TO lcl_xyz.


CREATE OBJECT ob2.

CALL METHOD ob2->m4. "✅ Internally accesses all methods of lcl_abc

You might also like