ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → CLASS → CLASS - DEFINITION
PROTECTED SECTION
Other versions: 7.31 | 7.40 | 7.54
Syntax
PROTECTED SECTION.
Effect
This statement can only be used in the declaration part of a class. It defines the protected
visibility section of the class class
.
All components of the class declared in the area behind the statement PROTECTED SECTION
can be addressed directly only in the subclasses of the class and in the class itself (plus its
friends), if allowed by the
package concept. In particular, protected methods in subclasses can be redefined.
Notes
- The class is the smallest encapsulation unit in ABAP Objects. This is the reason why a method cannot only use the protected components of the own class but also all instances of the same class.
- In subclasses, it is not possible to access the protected components of the superclasses using reference variables of the type of the superclass, because otherwise an attribute of an object of the dynamic type of the superclass or another subclass could be changed. In the last case, a warning is produced by the extended syntax check.
- A subclass cannot access the protected components of a subclass of a different branch of the inheritance hierarchy even though they are inherited from a shared superclass.
-
The declaration of attributes in the protected section does not prevent methods in subclasses or in the class itself from passing
references to these attributes in the form of
reference variables or
field symbols, so that the attributes become visible and modifiable outside the protected section.
Example
Declaration of a protected constant const
in a superclass cls1
and its use as a start value of a public attribute of a subclass cls2
.
CLASS cls1 DEFINITION.
PROTECTED SECTION.
CONSTANTS const TYPE string VALUE `I'm protected`.
ENDCLASS.
CLASS cls2 DEFINITION INHERITING FROM cls1.
PUBLIC SECTION.
CLASS-DATA attr TYPE string VALUE cls1=>const.
ENDCLASS.
START-OF-SELECTION.
cl_demo_output=>display( cls2=>attr ).