Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarative statemnts →  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.

Other versions: 7.31 | 7.40 | 7.54

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:

  • ... FRIENDS cif1 ... cifn

    This addition can be specified when defining any local class of a program. Friendship can be granted to all classes or interfaces of the same program and to the classes and interfaces in the class library. Note, in particular, that the local classes of a class pool can grant friendship to the global class of that class pool.
  • ... LOCAL FRIENDS cif1 ... cifn

    This addition is not specified when the class is declared, but instead defines its own statement. In a class pool, the global class can use this statement to grant friendship to the local classes and interfaces of its own class pool. While the Class Builder generates the CLASS statement for the actual class declaration, the statement

    CLASS ... DEFINITION LOCAL FRIENDS cif1 ... cifn.

    is entered directly in the include program for defining local classes and interfaces.