Interfaces

The components of a class are divided into visibility sections, and this forms the external point of contact between the class and its users. For example, the components of the public section form the public interface of the class, since all attributes and method parameters can be accessed by any user. The protected components form an interface between the class and those classes that inherit from it (subclasses).

Interfaces are independent structures that allow you to enhance the class-specific public points of contact by implementing them in classes. Different classes that implement the same interface can all be addressed in the same way. Interfaces are the basis for polymorphism in classes, because they allow a single interface method to behave differently in different classes. Interface references allow users to address different classes in the same manner. Interfaces can also be nested.

Interface Components

You can define exactly the same components in interfaces as in classes. Like classes, you define interfaces either globally in the R/3 Repository, or locally in an ABAP program.

Implementing Interfaces

Unlike classes, interfaces do not have instances. Instead, they are implemented by classes, using the INTERFACES statement in the declaration part of a class. This statement may only be included in the public visibility section of a class, that is after PUBLIC SECTION. You can adapt some interface components to suit the needs of the class. For example, you can characterize methods as abstract or final und you can assign initial values to attributes.

When you implement an interface in a class, the interface components are added to the class components in the public visibility section. A component comp of an interface intf, implemented in a class, becomes a fully-fledged member of that class, with the name intf~comp.

Classes that implement interfaces must implement all of their methods. The implementation part of the class must therefore contain the following statement block for an interface method meth:

METHOD intf~meth.
...
ENDMETHOD.

An interface can be implemented by more than one class. The same interface components are added to each of these classes. However, each class can implement the methods of the interface differently.

Interfaces allow you to use different classes in a uniform way (polymorphism). Interfaces that are implemented in more than one class add exactly the same components to each of those classes. If a class does not have any class-specific components in its public section, the interfaces form the only point of contact between the class and its users.

Interface References

You use reference variables to access objects. Instead of creating reference variables with reference to a class, you can create them by referring to an interface. Reference variables of this type can contain references to objects in any class that implements the interface. To declare a reference variable obj with reference to the interface intf, use the statement:
DATA obj TYPE REF TO intf. Using this reference variable, you can access any of the components defined in the interface, that is the object components that have been added to the class-specific components through implementation of the interface.

Accessing Objects Using Interface References

To generate an object for a class class, you must first declare a reference variable cref with reference to the class. If the class class implements an interface intf, you can assign the class reference variable cref to the interface reference variable iref as follows:

iref = cref

The interface reference in iref now points to the same object as the class reference in cref.

It is also possible to directly generate an object to which the interface reference variable points initially. In this case, the TYPE addition of the statement CREATE OBJECT must be used to specify a class that implements the interface.

CREATE OBJECT iref TYPE class.

If the interface intf contains an attribute attr and an instance method meth, you can address them as follows:

Using the class reference variable (not recommended):

Using the interface reference variable (recommended):

Accessing the Static Components of Interfaces

You can only use the name of the interface to access constants within an interface.

To access the other static components of an interface, use an object reference or the class class that is implementing the interface.