CLASS - class_options

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. ... FOR TESTING

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


Effect

Defines the properties of a class.

Addition 1

... PUBLIC

Effect

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

Note

You can only make references to known data types in the public visibility section of a global class. In addition, before Release 6.40 it was impossible to declare your own data types and constants with the TYPES and CONSTANTS statements. As of Release 6.40, this is now possible; this concept replaces the type groups concept.

Addition 2

... INHERITING FROM superclass

Effect

The INHERITING FROM addition 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 several direct subclasses (single inheritance). Every class without the INHERITING FROM addition inherits implicitly from the predefined empty, abstract class object. All classes in ABAP Objects form an inheritance tree, in which there is a unique path from each class to the root object object.

The class classadopts all components of superclass, without changing their visibility sections. Only the components of public and protected visibility sections are visible in the subclass. You cannot change the properties of the adopted components. In the subclass, you can declare additional components and redefine inherited methods - that is, implement them without changing the interface.

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.

Addition 3

... ABSTRACT

Effect

The ABSTRACT addition defines an abstract class class. You cannot create object instances from an abstract class. To use the instance components of an abstract class, a concrete subclass of the class must be instantiated.

Addition 4

... FINAL

Effect:

The FINAL addition defines a final class class. You cannot derive subclasses from a final class. All methods of a final class are implicitly final and cannot be explicitly declared as final.

Note

In classes that are declared as both abstract and final, you can only use the static components. You can declare instance components but you cannot use them.

Example

In this example, an abstract class c1 and a final class c2 are defined, such that c2 inherits from c1. c1 is implicitly a subclass of the empty class object. In c2, you can access m1 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.
    WRITE / 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 CREATE addition specifies the context in which the class class is instantiated - that is, where the statement CREATE OBJECT can be executed for this class.

Where a class can be instantiated depends on its immediate superclass:

Immediate subclasses of classes with the CREATE PROTECTED addition implicitly inherit the CREATE PROTECTED addition. All CREATE additions that overwrite the inherited addition can be specified explicitly.

Immediate subclasses of classes with the CREATE PRIVATE addition that are not friends of the class implicitly receive the CREATE NONE addition. They cannot be instantiated and you cannot specify any explicit CREATE additions for them.

The statement METHODS constructor for the declaration of the instance constructor of a class declared with the adition CREATE PRIVATE, can not only be listed in the public, but also in the protected or private visible area. This allows the use of components, which have been declared there, in the interface of the constructor.

Note

We recommend that you make final all classes that can be instantiated as private, since their subclasses cannot be instantiated unless they are friends of the class.

Addition 6

... SHARED MEMORY ENABLED

Effect

The SHARED MEMORY ENABLED addition defines a shared memory-enabled class whose instances can be stored in shared memory as shared memory objects.

The SHARED MEMORY ENABLED addition 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

The class has static attributes, which contain information about all the instances as a whole - such as the total number of instances.
The class allocates its own memory internally - for example, using kernel methods.

Addition 7

... FOR TESTING

Effect

The FOR TESTING addition defines the class as a test class for the ABAP Unit tool. Test classes can contain test methods, which are called during a test run.

A test class is generally a local class. The source code in the test class does not belong to the production code of the program and is not generated in production systems. (This is controlled using the profile parameter abap/test_generation). This means that test classes and their components can only be addressed in other test classes, not in the production code of the program. In particular, subclasses of test classes must be test classes themselves, declared with the FOR TESTING addition.

As of release 7.0, test classes with the following pseudo comments after the CLASS ... FOR TESTING statement can be assigned test properties;

... "#AU Risk_Level Critical|Dangerous|Harmless

... "#AU Duration   Short|Medium|Long

The properties determine the risk level and the expected run duration of a test. For each program line, you can enter a pseudo comment. It is case-sensitive. Differences in the syntax lead to a warning when the test is run. During the test run, the test properties are checked. If a test has a higher risk level that is allowed for the system, then the test is not executed. Test that run longer than the expected run duration, are terminated.

A local test class can contain specific private methods that implement the fixture for the tests on the class. These methods have the following predefined names:

Notes A test class can also contain any number of other components, as well as the test methods and the special methods for the fixture. These components can be only be used in their own test class or in other test classes, depending on their visibility. In this way, you can define auxiliary methods for tests, for example.

Addition 8

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

Effect

The FRIENDS addition makes the classes class1 class2 ... or the interfaces intf1 intf2 ... friends of the class class. All subclasses of 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 are made friends of the class class.

The friends of a class have unlimited access to the protected and private components of the class and can create instances of the class without constraints.

The friends of class are not automatically made friends of its subclasses. The FRIENDSaddition does not make the class classfriends of the friends of the other class.

Without the GLOBAL addition, 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, make sure 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 GLOBAL addition is only allowed if the PUBLIC addition is also used - that is, where the class is a global class of a class pool. You can list other global classes and interfaces of the Class Library after GLOBAL FRIENDS. This addition is generated when a global class is created by the Class Builder if friends are entered on the appropriate tab.

Note

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

Example

In this example, the interface i1 and thus 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.
    WRITE  oref->a1.
  ENDMETHOD.
ENDCLASS.