Explain PUBLIC, PROTECTED and PRIVATE sections, the READ-ONLY addition and friends. How do you decide where a component goes?
Say your answer out loud first.
Practise another way
Public components are visible to every user of the class, protected ones to the class, its subclasses and friends, private ones only to the class itself and its friends. READ-ONLY lets outsiders read a public attribute but not change it.
Friends get full access, one way only. My default is private, protected only for hooks I want subclasses to redefine, and public only for the methods callers really need, ideally through an interface.
The three sections
| Visible for | PUBLIC | PROTECTED | PRIVATE |
|---|---|---|---|
| The class and its friends | yes | yes | yes |
| Subclasses | yes | yes | no |
| Everyone else | yes | no | no |
Interfaces implemented by a class are always added to its public section.
CLASS lcl_account_factory DEFINITION DEFERRED.
CLASS lcl_account DEFINITION CREATE PROTECTED FRIENDS lcl_account_factory.
PUBLIC SECTION.
DATA iban TYPE string READ-ONLY. " readable outside, not writable
METHODS deposit IMPORTING amount TYPE decfloat34.
PROTECTED SECTION.
METHODS check_limit IMPORTING amount TYPE decfloat34. " subclass hook
PRIVATE SECTION.
DATA balance TYPE decfloat34. " class and friends only
ENDCLASS.
READ-ONLY
- Allowed only in the public section.
- Outsiders can read the attribute; the class itself, its subclasses and its friends can still change it. It is not "write once" as in some other languages, which is why Clean ABAP says to use it sparingly.
- Good fit: immutable value objects, where all attributes are set in the constructor and exposed
READ-ONLYinstead of a set of getters.
Friends
FRIENDS(local classes),GLOBAL FRIENDS(global classes to other global classes or interfaces) and the separate statementCLASS ... DEFINITION LOCAL FRIENDS ...(a global class befriending local classes of its own pool, typically test classes).- A friend gets access to all components, ignores
READ-ONLY, and may create instances even if instantiation is restricted. - Friendship is one-way, and granting it is not inherited: a friend of the superclass is not automatically a friend of its subclasses. Subclasses of the friend, however, do become friends.
Instantiation and the constructor
CREATE PUBLIC | PROTECTED | PRIVATE decides who may instantiate the class. The constructor may not be less visible than this: a CREATE PUBLIC class needs a public constructor.
For global classes declared CREATE PRIVATE, Clean ABAP still recommends declaring the constructor in the public section, because the compiler requires it there.
How to decide
- Start private.
- Make it protected only if you deliberately design a hook for subclasses (and then the class is not
FINAL). - Make it public only for the operations callers need, and put those methods in an interface so callers can be tested with doubles.
- Changing a public attribute later breaks callers, so public data is a long-term promise.
Gives the visibility matrix, knows READ-ONLY still allows internal changes, explains friendship rules and states a 'private by default' design rule.
Defines the three sections and stops, or makes everything public 'to keep it simple'.
Assuming READ-ONLY makes an attribute immutable. Only outsiders are blocked; the class, its subclasses and friends can still change it.
- Can a subclass change a READ-ONLY attribute it inherited?
- Is friendship inherited by subclasses of the class that grants it?
- Where must the constructor go in a CREATE PRIVATE global class?
2 official pages and 2 prep guides. Confidence: high. Checked 1 Dec 2026.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Official page Backs the answer.
- Official page Backs the answer.