Tuesday, June 23, 2009

Definition of Classes and Interfaces

In ABAP Objects, you can define classes and interfaces either globally or locally. You define global classes and interfaces with the Class Builder tool in the ABAP Workbench; they are then stored centrally in the class library in the repository. All ABAP programs in an AS ABAP can access these global classes and interfaces. Conversely, you can only use local classes and interfaces in the program that defines them. When an ABAP program uses a class, the system searches first for a local class, then for a global class of the same name. Otherwise, there is no difference in the use of local and global classes or interfaces.

Since local classes are only used by one program, it is generally sufficient to define the public interface - that is, the outwardly visible components appropriate for this program. Conversely, each global class is available throughout the system, which means that its public interface can only be typed with reference to data types that are themselves visible throughout the system.

The following sections describe classes and interfaces using the syntax for the definition of local classes and interfaces in an ABAP program. For the definition of global classes and interfaces, all language elements have a counterpart in the operating elements of the Class Builder. For more information, see the documentation for the Class Builder.

Defining Local Classes

Local classes consist of ABAP source code enclosed between the CLASS - ENDCLASS statements. A complete class definition consists of a declaration part and, if necessary, an implementation part.

The declaration part of a class class consists of one statement block:

CLASS class DEFINITION.
...
ENDCLASS.

This declaration part contains the declaration of the components (attributes, methods, events) of a class. All the components of a class must be assigned explicitly to a visibility section (PUBLIC SECTION, PROTECTED SECTION, PRIVATE SECTION), which defines from where each component can be accessed. In global classes, the declaration part belongs to the global data declarations and should be at the start of the program.

If methods are declared in the declaration part of a class, the class needs an implementation part, which consists of one statement block:

CLASS class IMPLEMENTATION.
...
ENDCLASS.

The implementation part of a class contains the implementation of all the methods of this class. Methods are procedures - that is, processing blocks of an ABAP program. The position of the implementation part in the source code is thus unimportant. For clarity, however, you should either put all the implementation parts of local classes at the end (as in subroutines), or directly after the relevant definition part. If you do the latter, note that you must then assign subsequent non-declarative statements explicitly to a processing block, such as START-OF-SELECTION, so that they can be accessed.

Defining Local Interfaces

You define the local interface intf using the

INTERFACE intf. and
...
ENDINTERFACE. statements.

The definition contains all the components (attributes, methods, events) of the interface. In interfaces, you can define the same components as in classes. You cannot assign the components of a interface explicitly to a visibility section, because interface components always extend the public area of the class. There is no implementation part for interfaces, since the methods of an interface must be implemented in the same class that implements the interface.

No comments:

Blog Archive