ABAP Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview
Definition of Classes and Interfaces
Classes and interfaces in ABAP Objects can be declared either globally or locally.
- You define global classes and interfaces with the Class Builder in ABAP Workbench. They are stored centrally in the class library in the repository. From a technical perspective, global classes and interfaces are defined in class pools or interface pools. All ABAP programs in an AS ABAP can access these global classes and interfaces. Access is managed by the package check. Global classes and interfaces are stored in a namespace along with the data types of ABAP Dictionary.
- You can define local classes and interfaces in all programs except interface pools and type groups. They can be used statically only in the defining program. Dynamic access from other programs is possible but not advisable. When you use a class in an ABAP program, the system first searches for a local class with the specified name. If it does not find one, it then looks for a global class. Otherwise, there is no difference between using global and local classes or interfaces.
If you are defining a local class that is only used in a single program, it is usually sufficient to define the outwardly visible components so that it fits into that program. Conversely, each global class is available throughout the system, which means that its public interface can only be specified with reference to data types that are themselves visible throughout the system.
The syntax for defining classes and interfaces is essentially the same for local and global classes
and interfaces. The only difference is in the PUBLIC
addition, which makes a distinction between the global classes and interfaces and local declarations.
Global classes and interfaces can be edited in the Class Builder, either in form-based or source-code based mode. In form-based mode, the Class Builder generates the relevant source code that can be accessed directly in source-code based mode.
Other versions: 7.31 | 7.40 | 7.54
Defining Classes
Classes consist of ABAP source code, enclosed in the ABAP statements CLASS ... ENDCLASS. A complete class definition consists of a declaration part and, if required, an implementation part.
The declaration part of a class named class
consists of a statement block:
CLASS class DEFINITION.
...
ENDCLASS.
It contains the declaration for all components (attributes, methods, events) of the 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. When you define local classes,
the declaration part belongs to the global program data. You should therefore place it at the beginning of the program.
If you declare methods in the declaration part of a class, you must also write an implementation part for it. This consists of a further statement block:
CLASS class IMPLEMENTATION.
...
ENDCLASS.
The implementation part of a class contains the implementation of all
methods of the 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
of the program, 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 Interfaces
The definition of an intf
interface is enclosed in the statements:
INTERFACE intf.
...
ENDINTERFACE.
The The definition contains the declaration for all components (attributes, methods, events) of the interface. In interfaces, you can define the same components as in classes. You cannot assign the components of an interface explicitly to a visibility section, because interface components always extend the public area of a class when they are implemented in it. Interfaces do not have an implementation part, since their methods are implemented in the class that implements the interface.