Skip to content

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 consumer can only access the public components of a class. This allows the internal implementation of a class to be changed without invalidating its consumers.

In rare cases, however, classes have to work so closely together that they require access to their mutually invisible components. The concept of friendship between classes was developed so that these components do not need to be made available to all consumers 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, which lists all of the classes and interfaces to be treated as friends. 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.


Note

We advise caution when granting a global interface friendship. Each class that implements the interface becomes a friend of the class granting the friendship. If a global interface is used, the extent of this friendship should be limited using the package concept, for example.

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, extreme care should be taken 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 CLASS ... DEFINITION statement has three different 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 Class Builder generates the CLASS statement for the actual class declaration for the full class, the statement

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

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

Executable Example

Friendship