ABAPCLASS_OPTIONS - CLASS OPTIONS

CPI1466 during Backup   Vendor Master (General Section)  
This documentation is copyright by SAP AG.

CLASS - class_options

Short Reference



Syntax

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


Additions

1. ... PUBLIC

2. ... INHERITING FROM superclass

3. ... ABSTRACT

4. ... FINAL

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

6. ... SHARED MEMORY ENABLED

7. ... OPEN FOR PACKAGE

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

Effect

Definition of the properties of a class. A test class forABAPunit is created with addition FOR TESTING.

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 aclass pool. This addition is generated by theClassBuilder 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.

Notes

Addition 2

... INHERITING FROM superclass

Effect

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

The class class adopts all components of superclass, without changing their visibility sections. Only the components ofpublic andprotectedvisibility 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 samenamespace. 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, aconcrete 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 both abstract and final, only the static components can be used. Although instance components can be declared, they cannot be used. Specifying ABSTRACT and FINAL together therefore is useful only forstatic classes.

Example

In this example, an abstract class c1 and a final class c2 are defined, such that c2inherits 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|PACKAGE|PRIVATE}

Effect

The CREATE addition specifies the context in which the class class is instantiated # thatis, where the statement CREATE OBJECT can be executed for this class and in which visibility section theinstance constructor of the class can be declared.

  • A class with the CREATE PUBLIC addition can be instantiated anywhere where the class is visible within the framework of thepackage concept.

  • A class with the CREATE PROTECTED addition can only be instantiated in methods of the class itself and the subclasses permitted by thepackagecheck. If a class is defined with the OPENFOR PACKAGE addition, it can be instantiated by all the repository objects of the package of the class too.

  • A class with theCREATE PACKAGE addition can be instantiated within the package of the class only.A prerequisite for specifying CREATE PACKAGE is simultaneous use of the OPEN FOR PACKAGE addition.

  • A class with the CREATE PRIVATE addition can only be instantiated in methods of the class itself. This means, in particular, that it cannot be instantiated as an inherited component of subclasses.

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

  • Immediate subclasses of object, or classes with the CREATE PUBLIC addition implicitlyinherit the CREATE PUBLIC addition. All CREATE additions that then overwrite the inherited addition can be specified explicitly.

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

  • Immediate subclasses of a class with the CREATE PACKAGE addition inherit the CREATE PACKAGEaddition implicitly. All CREATE additions that then overwrite the inherited addition can be specified explicitly.

  • Immediate subclasses of classes with the CREATE PRIVATE addition that are notfriendsof the class implicitly receive the CREATE NONE addition. They cannot be instantiated and youcannot specify any explicit CREATE additions for them. Immediate subclasses that are friendsof the class implicitly inherit the CREATE PRIVATE addition. All CREATE additions can be specified for all superclasses that can instantiated as private using friends.

The statement METHODS constructor for the declaration of the instance constructor of a local class can be listed in allvisibilitysections which are of general instantiability or of the instantiability used in the CREATEaddition. For global classes, only a declaration in the public visibility section is feasible, for technical reasons.

Notes

  • We recommend that you make final a class that can be instantiated as private, since its subclasses cannot be instantiated unless they are friends of the class.
  • We recommend that you declare the instance constructor of local classes in the visibility sector ofthe class that matches its instantiability, because this allows you to use the components declared there in the constructor interface.


Addition 6

... SHARED MEMORY ENABLED

Effect

The SHARED MEMORY ENABLED addition defines ashared memory-enabled class whose instances can be stored inshared memory asshared objects.

The SHARED MEMORY ENABLED addition can only be applied to a subclass if all its superclasseshave been defined with this addition. Subclasses do not necessarily inherit this addition from their superclasses.

Notes

  • The static attributes of a Shared Memory-enabled class are handled in the same way as a normal class,that is they are created in internal mode of a program when the class is loaded. If different programsaccess the same shared objects, the static attributes of the corresponding classes exist multiple times and independently from each other in the programs.

  • At the moment 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 you assign the SHARED MEMORY ENABLED addition by choosing the shared memory-enabled attribute in theClass Builder. This applies in particular to thearea root class of anarea, which is always global.

  • You should only use this addition if you can do so without causing any problems. Problems occur withshared memory if:
  • 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

... OPEN FOR PACKAGE

Effect

This addition

  • is a prerequisite for the use of the PACKAGESECTION statement in the declaration part of the class, in order to create a package visibility area,
  • is a prerequisite for the use of the addition CREATE PACKAGE,
  • makes the components of the protected visibility area visible for repository objects of the same package.

The addition OPEN FOR PACKAGE is not passed from superclasses to subclasses.

Note

The addition READ-ONLYis only required in classes with the addition OPEN FOR PACKAGE for protected and package-visible attributes and can also only be specified in such classes for these visibility areas.

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. At the same time, all subclasses of the classes class1class2 ..., all classes that implement one of the interfaces intf1 intf2 ..., and all interfacesthat include 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 unlimited access to theprotected 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 the subclasses of class. The FRIENDS addition does not make the class class a friend of the friends of the other class.

Without the GLOBAL addition, all classes and interfaces that are visible at this point can bespecified for class1 class2... and intf1 intf2 .... If global classes and interfaces ofthe 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 aclasspool. 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 theClass 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.
    WRITE  oref->a1.
  ENDMETHOD.
ENDCLASS.


SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up   General Material Data  
This documentation is copyright by SAP AG.


Length: 20523 Date: 20120518 Time: 204959     triton ( 429 ms )