ABAP - Keyword Documentation →  ABAP - Reference →  Declarations →  Declaration Statements →  Classes and Interfaces →  CLASS →  CLASS - DEFINITION → 

CLASS - class_options

Quick Reference

Syntax

... [PUBLIC]
    [INHERITING FROM superclass]
    [ABSTRACT]
    [FINAL]
    [CREATE {PUBLIC|PROTECTED|PRIVATE}]
    [SHARED MEMORY ENABLED]
    [ FOR TESTING]
    [[GLOBAL] FRIENDS class1 class2 ...
                      intf1  intf2  ...].


Extras:

1. ... PUBLIC

2. ... INHERITING FROM superclass

3. ... ABSTRACT

4. ... FINAL

5. ... CREATE {PUBLIC|PROTECTED|PRIVATE}

6. ... SHARED MEMORY ENABLED

7. ... [GLOBAL] FRIENDS class1 class2 ... intf1  intf2 ...

Effect

Defines the properties of a class. A test class for ABAP Unit is created using the addition FOR TESTING.

Addition 1

... PUBLIC

Effect

The addition PUBLIC specifies that the class class is a global class in the class library. The addition PUBLIC can only be applied to one class in a class pool. This addition is created by Class Builder when a global class is created. Any class that does not have the addition PUBLIC applied to it is a local class in its program.

Notes

Example

Declaration of the public class CL_DEMO_SPFLI.

CLASS cl_demo_spfli DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.


Addition 2

... INHERITING FROM superclass

Effect

The addition INHERITING FROM specifies that the class class is derived from the superclass superclass and as such is a direct subclass of it. The superclass superclass can be any non-final class that is visible at this point.

Each class can only have one superclass, but can have multiple direct subclasses (this is known as single inheritance). Every class without the addition INHERITING FROM inherits implicitly from the predefined empty and abstract class object. All classes in ABAP Objects construct an inheritance tree in which there is a unique path from each class 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 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 the interface being modified.

Note

The public and protected components of all classes in a path in the inheritance tree are in the same namespace. New components in a subclass cannot have the same name as a public or protected component that has been inherited from the superclasses.

Example

A subclass cls2 inherits from the superclass cls1. cls1 is an implicit subclass of empty class object.

CLASS cls1 DEFINITION.
  ...
ENDCLASS.

CLASS cls1 DEFINITION.
           INHERITING FROM cls1.
  ...
ENDCLASS.

Addition 3

... ABSTRACT

Effect

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.

Example

Subclass cls2 inherits from an abstract superclass cls1.

CLASS cls1 DEFINITION ABSTRACT.
  ...
ENDCLASS.

CLASS cls1 DEFINITION.
           INHERITING FROM cls1.
  ...
ENDCLASS.

Addition 4

... FINAL

Effect

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 declared explicitly as final.

Notes

Example

In this example, an abstract class c1 and a final class c2 are defined, such that c2 inherits from c1. In c2, m1 can be accessed but not a1.

CLASS c1 DEFINITION ABSTRACT.
  PROTECTED SECTION.
    METHODS m1.
  PRIVATE SECTION.
    DATA a1 TYPE string VALUE `Attribute A1 of class C1`.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1 FINAL.
  PUBLIC SECTION.
    METHODS m2.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD m1.
    cl_demo_output=>display_text( a1 ).
  ENDMETHOD.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
  METHOD m2.
    m1( ).
  ENDMETHOD.
ENDCLASS.

DATA oref TYPE REF TO c2.

START-OF-SELECTION.

CREATE OBJECT oref.
oref->m2( ).

Addition 5

... CREATE {PUBLIC|PROTECTED|PRIVATE}

Effect

The addition CREATE specifies the context in which the class class is instantiated, namely 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.

The immediate superclass of subclass determines whether it can be instantiated:

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. For global classes, only a declaration in the public visibility section is feasible, for technical reasons.

Notes

Example

Class cls can only be privately instantiated. There is a separate method factory, which is allowed to perform an instantiation and returns a reference to the generated object.

CLASS cls DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS factory RETURNING VALUE(ref) TYPE REF TO cls.
    ...
ENDCLASS.

CLASS cls IMPLEMENTATION.
  METHOD factory.
    ref = NEW #( ).
  ENDMETHOD.
ENDCLASS.

Addition 6

... SHARED MEMORY ENABLED

Effect

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 applied to a subclass if all its superclasses have been defined with this addition. Subclasses do not necessarily inherit this addition from their superclasses.

Notes

Example

Declaration of the public class CL_DEMO_FLIGHT_LIST. 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.


Addition 7

... [GLOBAL] FRIENDS class1 class2 ... intf1 intf2 ...

Effect

The FRIENDS addition makes the classes class1 class2 ... or the interfaces intf1 intf2 ... into friends of the class class. At the same time, all subclasses of the classes class1 class2 ..., all classes that implement one of the interfaces intf1 intf2 ..., and all interfaces that include one of the interfaces intf1 intf2 ... as a component interface become friends of the class class. At least one class or interface must be specified.

The friends of a class have unrestricted access to all components of the class, regardless of the visibility section and the addition READ-ONLY. Friends 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 the friends of the other class.

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 from the class library are made friends, it should be noted that the local classes of 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 if the class is a global class of a class pool. Other global classes and interfaces from the class library can be specified after GLOBAL FRIENDS. This addition is created when a global class is created by Class Builder if friends are entered on the appropriate tab.

Note

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

Example

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.

INTERFACE i1.
  ...
ENDINTERFACE.

CLASS c1 DEFINITION CREATE PRIVATE FRIENDS i1.
  PRIVATE SECTION.
    DATA a1 TYPE c LENGTH 10 VALUE 'Class 1'.
ENDCLASS.

CLASS c2 DEFINITION.
  PUBLIC SECTION.
    INTERFACES i1.
    METHODS m2.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
  METHOD m2.
    DATA oref TYPE REF TO c1.
    CREATE OBJECT oref.
    cl_demo_output=>display_text( oref->a1 ).
  ENDMETHOD.
ENDCLASS.

Example

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.

Executable Example

Friendship