SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
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.
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.
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:
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:
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:
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.