Show TOC

ABAP ObjectsLocate this document in the navigation structure

Concept

Object-oriented programming is based on the encapsulation of data and functions in classes. Instances (objects) are then created from these classes. ABAP supports polymorphism by the use of simple inheritance and interfaces. Classes and interfaces can be created locally in any ABAP program or globally in the class library using Class Builder. Local classes and interfaces can only be used in the program defining them, globally and across the system.

Classes

Classes are defined between the statements CLASS and ENDCLASS. A class definition consists of a declaration part for the declaration of its components and an implementation part for the implementation of its methods. A class can have attributes, methods, events, types, and constants as its components. The statements DATA, METHODS, and EVENTS declare instance components that are bound to objects. CLASS-DATA, CLASS-METHODS, and CLASS-EVENTS plus TYPES and CONSTANTS are used to declare non-instance-dependent static components.

Methods are declared using the statement METHODS and its additions IMPORTING, EXPORTING, CHANGING, and RETURNING for their parameter interface plus RAISING for the valid exceptions in the declaration part of a class. They are implemented between the statements METHOD and ENDMETHOD in the implementation part of the class. Methods are called by specifying the method name meth( ... ) statically or by using the statement CALL METHOD (meth) for dynamic calls The instance constructor has the predefined name constructor; the static constructor has the name class_constructor. Destructors cannot be implemented in ABAP. Methods with a return value ( RETURNING parameter) are called functional methods. Calls of functional methods can be specified directly at operand positions in logical expressions and calculation expressions.

Events are declared using the statement EVENTS and can be triggered in the methods of the class by using RAISE EVENT. These object-oriented events are not related to the events in the runtime environment in any way. The addition FOR EVENT OF is used to declare methods as event handlers.

The components in a class are declared in one of three visibility sections:

  • PUBLIC SECTION: Visible wherever the class is known

  • PROTECTED SECTION: Visible within the class and its subclasses

  • PRIVATE SECTION: Visible within the class only

The addition INHERITING FROM can be used to make classes inherit as subclasses from precisely one superclass. Methods inherited from the superclass can be redefined in subclasses. Inheritance can involve both abstract and final classes.

Classes with the addition FOR TESTING are special test classes for ABAP Unit.

Interfaces

Interfaces are defined between the statements INTERFACE and ENDINTERFACE. The same components can be declared as in classes. An interface does not have any visibility sections or an implementation part. Interfaces are included in the public visibility section of a class using the statement INTERFACES. In this way, there components are adopted by the class. The interface methods must be implemented in the class. Interface references can be used to access instances of classes that implement the corresponding interface.

Objects

Objects are created using the statement

CREATE OBJECT oref TYPE class.

or the constructor operator

oref = NEW class( ... ).

. Here, oref is an object reference variable (class reference or interface reference) that points to the object after it is created and class is the class of the object. The type of an object reference variable must always be more more general than or equal to the class of the object. If the type of the object reference variable is the same as the type of the new object, the addition TYPE can be omitted when the class is specified or the character # can be used after NEW. In instance methods, the predefined reference variable me contains a reference to the current object.

Example

Defines an interface if_counter for a counter and includes it in a class cl_counter. Creates an object and uses it using an interface reference counter.

               INTERFACE if_counter.
  METHODS: reset,
           increment,
           get_counts RETURNING value(count) TYPE i.
ENDINTERFACE.

CLASS cl_counter DEFINITION.
  PUBLIC SECTION.
    METHODS constructor IMPORTING offset TYPE i.
    INTERFACES if_counter.
  PRIVATE SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS cl_counter IMPLEMENTATION.
  METHOD constructor.
    count = offset.
  ENDMETHOD.
  METHOD if_counter~reset.
    count = 0.
  ENDMETHOD.
  METHOD if_counter~increment.
    count = count + 1.
  ENDMETHOD.
  METHOD if_counter~get_counts.
    count = me->count.
  ENDMETHOD.
ENDCLASS.

...

START-OF-SELECTION.
  DATA counter TYPE REF TO if_counter.

  counter = NEW cl_counter( 10 ).

  counter->increment( ).

  DATA(counts) = counter->get_counts( ).

  counter->reset( ).