SAP CLASS OPTIONS ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• PUBLIC CLASS
• INHERITING FROM CLASS
• ABSTRACT CLASS
• FINAL CLASS
• CREATE PUBLIC CLASS
• CREATE PROTECTED CLASS
• CREATE PRIVATE CLASS
BEGIN_SECTION VERSION 5 OUT
• SHARED MEMORY ENABLED CLASS
END_SECTION VERSION 5 OUT
• GLOBAL FRIENDS CLASS
• FRIENDS CLASS

CLASS, class_options
Short Reference

ABAP_SYNTAX
... $[PUBLIC$]
$[INHERITING FROM superclass$]
$[ABSTRACT$]
$[FINAL$]
$[CREATE ${PUBLIC$|PROTECTED$|PRIVATE$}$]
BEGIN_SECTION VERSION 5 OUT
$[SHARED MEMORY ENABLED$]
END_SECTION VERSION 5 OUT
$[FOR TESTING$]
$[FOR BEHAVIOR OF$]
$[$[GLOBAL$] FRIENDS class1 class2 ...
intf1 intf2 ...$].

ABAP Addition
1 ... PUBLIC
2 ... INHERITING FROM superclass
3 ... ABSTRACT
4 ... FINAL
5 ... CREATE ${PUBLIC$|PROTECTED$|PRIVATE$}
BEGIN_SECTION VERSION 5 OUT
6 ... SHARED MEMORY ENABLED
END_SECTION VERSION 5 OUT
7 ... $[GLOBAL$] FRIENDS class1 class2 ... intf1 intf2 ...

What does it do?
Definition of the properties of a class. The following additions define special classes:
FOR TESTING defines a test class for ABAP Unit.
FOR BEHAVIOR OF defines the global class of an ABAP behavior pool.

ABAP Addition

What does it do?
The addition PUBLIC makes the class class a global class in the class library. The addition PUBLIC can only be applied to one class of a class pool.
BEGIN_SECTION VERSION 5 OUT The Class Builder creates it when a global class is created.
END_SECTION VERSION 5 OUT Any class without the addition PUBLIC applied to it is a local class in its program.



Latest notes:

References to public data types can only be made in the public visibility section of a global class.
Data types and constants declared in the public visibility section of global classes using the statements TYPES and CONSTANTS replace their declarations in type pools.
A global class is a global object type and is in the same namespace as all global types of an AS ABAP.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Declaration of the global class CL_DEMO_SPFLI. CLASS cl_demo_spfli DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
The addition INHERITING FROM is used to derive the class class by inheriting from the superclass superclass and thus to its direct subclass. The superclass superclass can be any non-final class that is visible at this point.
Each class can only have one superclass, but multiple direct subclasses (single inheritance). Every class without the addition INHERITING FROM inherits implicitly from the predefined empty and abstract class object. All classes in ABAP Objects form an inheritance tree in which each class has a unique path to the root object object.
The class class inherits all components of superclass, without changing their visibility sections. Only the components of public and protected visibility sections of the superclass are visible in the subclass. The properties of the inherited components cannot be changed. In the subclass, additional components can be declared and inherited methods redefined, which means that they can be reimplemented without interface modification.



Latest notes:

The public and protected components of all classes within a path of the inheritance tree are in the same namespace. New components in a subclass must not have the same name as a public or protected component that was inherited from the superclasses.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Subclass cls2 inherits from a superclass cls1. cls1 is an implicit subclass of the empty class object.
ABEXA 00102
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
The addition ABSTRACT defines an abstract class class. No instances can be created from an abstract class. To use the instance components of an abstract class, a concrete subclass of the class must be instantiated.



Latest notes:

Abstract classes are not supported by the ABAP Test Double Framework.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Subclass cls2 inherits from an abstract superclass cls1.
ABEXA 00103
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
The addition FINAL defines a final class class . No subclasses can be derived from a final class. All methods of a final class are implicitly final and cannot be explicitly declared as final.



Latest notes:

Final classes complete an inheritance tree.
In classes that are both abstract and final, only the static components can be used. Although instance components can be declared, t hey cannot be used. Specifying ABSTRACT and FINAL together, therefore, is useful only for static classes.
Final classes are not supported by the ABAP Test Double Framework.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
In this example, an abstract class c1 and a final class c2 are defined, whereby c2 inherits from c1 . In c2, m1 can be accessed but not a1.
ABEXA 00104
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
The addition CREATE specifies the context in which the class class can be instantiated, that is, where the statement CREATE OBJECT can be executed for this class and in which visibility section the instance constructor of the class can be declared.
A class with the addition CREATE PUBLIC can be instantiated anywhere where the class is visible within the framework of the package concept.
A class with the addition CREATE PROTECTED can only be instantiated in methods of its subclasses, of the class itself, and of its friends.
A class with the addition CREATE PRIVATE can only be instantiated in methods of the class itself or of its friends . This means, in particular, that it cannot be instantiated as an inherited component of subclasses.
The instantiability of a subclass depends on the direct superclass as follows:
Immediate subclasses of object, or classes with the addition CREATE PUBLIC inherit the addition CREATE PUBLIC implicitly. All CREATE additions that then overwrite the inherited addition can be specified explicitly.
Immediate subclasses of classes with the addition CREATE PROTECTED inherit the addition CREATE PROTECTED implicitly. All CREATE additions that then overwrite the inherited addition can be specified explicitly.
Immediate subclasses of classes with the addition CREATE PRIVATE that are not friends of the class inherit the addition CREATE <(>NONE<)> implicitly. They cannot be instantiated and no explicit CREATE additions can be specified. Immediate subclasses that are friends of the class inherit the addition CREATE PRIVATE implicitly. All CREATE additions can be specified explicitly for friends of superclasses that can be instantiated privately.
The statement METHODS constructor used to declare the instance constructor of a local class can be specified in all visibility sections that are more general than or the same as the instantiability specified in the addition CREATE. In global classes, the declaration is only possible in the public visibility section for technical reasons.

ABAP_PGL
Declare the instance constructor in the public visibility section.
ABAP_PGL_END



Latest notes:

It is recommended that a class that can be instantiated privately is specified as a final class, since its subclasses cannot be instantiated unless they are friends of the class.
it is further recommended that the instance constructor of local classes is declared in the visibility section of the class that matches its instantiability, since this enables the components declared there to be used in the constructor interface.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The class cls can only be privately instantiated. The separate method factory is allowed to perform an instantiation and returns a reference to the generated object.
ABEXA 00105
ABAP_EXAMPLE_END
BEGIN_SECTION VERSION 5 OUT

ABAP Addition

What does it do?
The addition SHARED MEMORY ENABLED defines a shared-memory-enabled class whose instances can be stored in shared memory as shared objects.
The addition SHARED MEMORY ENABLED can only be specified for a subclass if all explicit superclasses are defined with this addition. Subclasses do not automatically inherit this addition from their superclasses.



Latest notes:

The static attributes of a shared-memory-enabled class are handled in the same way as those of a regular class, which means they are created in the ABAP_ISESS of a program when the class is loaded. If different programs access the same shared objects, the static attributes of the associated classes exist multiple times and independently from each other in the programs.
No events can be declared or handled in a shared-memory-enabled class. The statements $[CLASS-$] EVENTS and the addition FOR EVENT cannot be specified in the declaration part.
For global shared-memory-enabled classes, the addition SHARED MEMORY ENABLED must be created by choosing the shared-memory-enabled attribute in the Class Builder. This applies in particular to the area root class of an area, which is always global.
This addition should be specified only if it does not cause any problems. Problems occur with shared memory in the following cases:
The class has static attributes that store information about all the instances as a whole, such as the total number of instances.
The class allocates its own memory internally, for example by using kernel methods.
NON_V5_HINTS
ABAP_HINT_END



Example ABAP Coding

Declaration of the global class CL_DEMO_FLIGHT_LIST. The class is shared-memory-enabled and root class of the area CL_DEMO_FLIGHTS, which is managed in transaction SHMA. CLASS cl_demo_flight_list DEFINITION
PUBLIC
FINAL
CREATE PUBLIC
SHARED MEMORY ENABLED.
ABAP_EXAMPLE_END
END_SECTION VERSION 5 OUT

ABAP Addition intf2 ...

What does it do?
The addition FRIENDS makes the classes class1 class2 ... or the interfaces intf1 intf2 ... into friends of the class class. At the same time, all subclasses of class1 class2 ..., all classes that implement one of the interfaces intf1 intf2 ..., and all interfaces that contain one of the interfaces intf1 intf2 ... as a component interface become friends of the class class. At least one class or one interface must be specified.
The friends of a class have unrestricted access to all components of the class, regardless of the visibility section or the addition READ-ONLY, and can create instances of the class without any restrictions.
The friends of class are not automatically made friends of the subclasses of class. The addition FRIENDS does not make the class class a friend of its friends.
Without the addition GLOBAL, all classes and interfaces that are visible at this point can be specified for class1 class2... and intf1 intf2 ... If global classes and interfaces of the Class Library are made friends, it should be ensured that the local classes of other ABAP programs are not visible in these global classes. The components of a local class class cannot be accessed statically by these friends.
The addition GLOBAL is only allowed if the addition PUBLIC is also used, namely for a global class of a class pool. Other global classes and interfaces from the class library can be specified after GLOBAL FRIENDS.
BEGIN_SECTION VERSION 5 OUT This addition is created when a global class is created by Class Builder if friends were entered on the appropriate tab.
END_SECTION VERSION 5 OUT



Latest notes:

The addition FRIENDS must be specified as the last addition after the other additions.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
In this example, the interface i1 and therefore the implementing class c2 are friends of the class c1. The class c2 can instantiate c1 and access its private component a1.
ABEXA 00106
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The generated area class CL_DEMO_FLIGHTS inherits from system class CL_SHM_AREA and make this class a friend. CLASS cl_demo_flights DEFINITION
PUBLIC
INHERITING FROM cl_shm_area
FINAL
CREATE PRIVATE
GLOBAL FRIENDS cl_shm_area.
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Friendship
ABAP_EXAMPLE_END

Return to menu