Excerpted from the book 'ABAP Objects: ABAP Programming in SAP NetWeaver', ISBN 1592290795, Copyright 2007. Written
permission from SAP PRESS is required for all other uses. Copyright © 2007 SAP PRESS America. All rights reserved. Email: firstname.lastname@example.org. SAP PRESS America, 11300 Rockville Pike, Suite 1100, Rockville, MD 20852-3030, USA.
Download chapter 6: 'Advanced Concepts in ABAP Objects'
6.3.2 Creating Interfaces
With regard to their declaration, interfaces in ABAP Objects play the same role as classes. Just like classes, interfaces are object types that reside in the namespace of all types. While a class describes all aspects of a class, an interface only describes a partial aspect. As mentioned above, standalone interfaces can be regarded as special abstract classes without implementation that can be used in multiple classes.
Accordingly, the declaration of a standalone interface hardly varies from the declaration of a class. As with classes, we distinguish global and local interfaces in the same way that we do global and local classes. Therefore, the same rules apply regarding their usability. Global interfaces can be used in any program if the package assignment of the program permits it. Local interfaces can only be used in the same program.
The syntax for declaring a local interface is:
INTERFACE intf. DATA ... CLASS-DATA ... METHODS ... CLASS-METHODS ... ... ENDINTERFACE.
Basically, the declaration of an interface corresponds to the declaration part of a class, where instead of
CLASS—ENDCLASS, you simply use
INTERFACE—ENDINTERFACE. Interfaces can contain exactly the same components as classes. Unlike classes, however, interfaces don't need to be divided into different visibility sections because interface components are always integrated in the public visibility section of classes.
To create a global interface, use the Class Builder just as you would for global classes. In the Object Navigator, select Create · Class Library · Interface. In Transaction SE24, after selecting Create, select the Interface object type instead of Class.
Figure 6.7 shows the Class Builder for a global interface ZIF_DRIVE_ Class Builder OBJECT. You see the familiar user interface that you know from working with classes. When creating components, you need to specify the same input as you do for classes, except for the assignment to a visibility section. In the shown example, we created the same methods ACCELERATE and SHOW_SPEED as in ZCL_VEHICLE presented in Figure 4.7 in Chapter 4. The shown interface can therefore serve as an interface to objects that can be driven.
The Class Builder generates the corresponding ABAP statements in a program of the interface pool type, the source code of which can also be edited directly via Goto · Interface Section (see Figure 6.8). As in class pools, the addition
PUBLIC identifies the interface as a global interface that can be used in all programs. Apart from the declaration of the global interface, an interface pool cannot contain any local type declarations except for the publication of type groups.
The essential difference between interfaces and classes is that there is no implementation part for an interface. Therefore, it is not necessary to add
INTERFACE. The methods of an interface are all abstract. They are fully declared, including their parameter interface, but not implemented in the interface. Like the subclasses that implement the abstract methods of their abstract superclasses, all classes that want to use an interface must implement its methods.
Visit the SAP Press website for a detailed description and to learn how to purchase this title.