SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview →Friends - Friendship Between Classes
There is normally a strict division between external (PUBLIC) and internal (PROTECTED and PRIVATE) classes. A user can only access the public components of a class. This allows you to change the internal implementation of a class without invalidating its users.
In rare cases, however, classes have to work so closely together that they require access to each others' invisible components. The concept of friendship between classes has been developed so that these components do not need to be made available to all users at the same time.
Friends
A class can grant friendship to other classes and interfaces (and thus to all classes that implement this interface). To create this relationship, use the FRIENDS additions of the CLASS ... DEFINITION statement that includes all classes and interfaces to which you want to grant friendship. These friends are granted access to all components of the class offering the friendship, regardless of their visibility section or the addition READ-ONLY and can always create instances of this class regardless of the addition CREATE of the statement CLASS.
Friendship is unilateral
Friendship is a unilateral principle. A class that grants friendship is not automatically a friend of its friends. If the class that grants friendship wants to access the private or protected components of its friend, the latter must grant friendship explicitly.
Inheritance, Interfaces, and Friendship
Subclasses of friends and interfaces that are assigned a friend as a component interface also become friends. For this reason, you should be extremely careful when defining a friendship. The higher a friend is in the inheritance tree, the greater the number of subclasses that can access all of the components of the class granting friendship. A class that grants friendship to the root class object gains all of the ABAP Objects classes as friends and therefore has no privacy whatsoever. Conversely, it is relatively safe to grant friendship to a final class since this class alone is specified as a friend.
Friendship granted is not inherited, in contrast to the friend attribute. A friend of a superclass is, therefore, not automatically a friend of its subclasses.
The FRIENDS Additions
The statement CLASS ... DEFINITION has three FRIENDS additions: