Skip to content

Latest commit

 

History

History
1503 lines (1181 loc) · 76.6 KB

04_ABAP_Object_Orientation.md

File metadata and controls

1503 lines (1181 loc) · 76.6 KB

ABAP Object Orientation

💡 Note
This ABAP cheat sheet provides an overview on selected syntax options and concepts related to ABAP object orientation. It is supported by code snippets and an executable example. They are not suitable as role models for object-oriented design. Their primary focus is on the syntax and functionality. For more details, refer to the respective topics in the ABAP Keyword Documentation. Find an overview in the topic ABAP Objects - Overview.

Classes and Objects

Object-oriented programming in ABAP means dealing with classes and objects.

Objects ...

Classes ...

  • are templates for objects, i. e. they determine how all instances of a class are set up. All instances are created (i.e they are instantiated) based on this template and, thus, have the same setup.
    • To give an example: If, for example, a vehicle represents a class, then the instances of the class vehicle have the same setup. That means they all share the same kind of components like a brand, model and color or the same functionality like the acceleration or braking distance. However, the values of these components are different from instance to instance. For example, one instance is a red sedan of brand A having a certain acceleration; another instance is a black SUV of brand B and so on. You can create an object (or instance respectively) that stands for an actual vehicle with which you can work with. You might create any number of objects that are based on such a class - if instantiation is allowed.
  • contain components:
    • Attributes of the objects (the data object declarations)
    • Methods that determine the behavior of an object
    • Events to trigger the processing of ABAP code

⬆️ back to top

Creating Classes

You can either create local or global classes:

Local classes
  • can be defined within an ABAP program
  • can only be used in the ABAP program in which the class is defined
Global classes
  • are defined as global types, i. e. they are visible as a repository object - in contrast to local classes. As a global type, they can be used - as the name implies - globally in other ABAP programs or global classes
  • are declared in class pools that can contain a CCIMP include and other include programs

💡 Note

  • If a class is only used in one ABAP program, creating a local class is enough. However, if you choose to create a global class, you must bear in mind that such a class can be used everywhere. Consider the impact on the users of the global class when you change, for example, the visibility section of a component or you delete it.
  • Apart from ADT, global classes can also be created in the ABAP Workbench (SE80) or with transaction SE24 in classic ABAP.

Basic structure of classes:

  • Declaration part that includes declarations of the class components.
  • Implementation part that includes method implementations.
  • Both are introduced by CLASS and ended by ENDCLASS.

Creating a Local Class

"Declaration part
CLASS local_class DEFINITION.

    ... "Here go the declarations for all components and visibility sections.
        "You should place the declarations at the beginning of the program.

ENDCLASS.

"Implementation part
CLASS local_class IMPLEMENTATION.

    ...  "Here go the method implementations.
         "Only required if you declare methods in the declaration part.

ENDCLASS.

Creating a Global Class

The code snippet shows a basic skeleton of a global class. There are further additions possible for the declaration part.

"Declaration part
CLASS global_class DEFINITION
  PUBLIC                       "Makes the class a global class in the class library.
  FINAL                        "Means that no subclasses can be derived from this class.
  CREATE PUBLIC.               "This class can be instantiated anywhere it is visible.

    ... "Here go the declarations for all components and visibility sections.

ENDCLASS.

"Implementation part
CLASS global_class IMPLEMENTATION.

    ... "Here go the method implementations.
        "Only required if you declare methods in the DEFINITION part.

ENDCLASS.

💡 Note

  • Addition ... CREATE PROTECTED.: The class can only be instantiated in methods of its subclasses, of the class itself, and of its friends.
  • Addition ... CREATE PRIVATE: The class can only be instantiated in methods of the class itself or of its friends. Hence, it cannot be instantiated as an inherited component of subclasses.

⬆️ back to top

Excursion: Class Pool and Include Programs

  • A class pool is an ABAP program containing the definition of one global class (Global Class tab in ADT)
  • Global classes are marked as such with the PUBLIC addition to the CLASS statement: CLASS zcl_demo_test DEFINITION PUBLIC ...
  • Additionally, a class pool can also contain local classes that are defined in dedicated include programs (CCDEF and the other include names are internal names the include programs end with):
    • CCDEF include (Class-relevant Local Types tab in ADT): Is included in front of the declaration part of the global class
    • CCIMP include (Local Types tab in ADT): Is included behind the declaration part and in front of the implementation part of the global class
    • CCAU include (Test classes tab in ADT): Test include; contains ABAP Unit test classes

The following simplified example demonstrates include programs.

Expand to view the details

Global class:

  • Create a new global class (the example uses the name zcl_demo_test) and copy and paste the following code in the Global Class tab in ADT.
  • The method in the private section makes use of local types that are defined in the CCDEF include. In the example the method is deliberately included in the private visibility section. Having it like this in the public section is not possible due to the use of local types.
  • The if_oo_adt_classrun~main implementation contains method calls to this method. It also contains method calls to a method implemented in a local class in the CCIMP include.
  • As a prerequisite to activate the class, you also need to copy and paste the code snippets further down for the CCDEF and CCIMP include. Once you have pasted the code, the syntax errors in the global class will disappear.
  • You can run the class using F9. Some output is displayed.
  • When you have copied and pasted the CCAU code snippet for the simple ABAP Unit test, and activated the class, you can choose CTRL+Shift+F10 in ADT to run the ABAP Unit test. Alternatively, you can make a right click in the class code, choose Run As and 4 ABAP Unit Test.
CLASS zcl_demo_test DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.

  PROTECTED SECTION.
  PRIVATE SECTION.
    "This methods uses types (data type c1 and the local exception class)
    "defined in the CCDEF include
    METHODS calculate IMPORTING num1          TYPE i
                                operator      TYPE c1
                                num2          TYPE i
                      RETURNING VALUE(result) TYPE i
                      RAISING   lcx_wrong_operator.
ENDCLASS.

CLASS zcl_demo_test IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.

    "---- The method called uses type declarations in the CCDEF include ----
    TRY.
        DATA(result1) = calculate( num1 = 10 operator = '+' num2 = 4 ).
        out->write( data = result1 name = `result1` ).
      CATCH lcx_wrong_operator.
        out->write( `Operator not allowed` ).
    ENDTRY.

    TRY.
        DATA(result2) = calculate( num1 = 10 operator = '-' num2 = 4 ).
        out->write( data = result2 name = `result2` ).
      CATCH lcx_wrong_operator.
        out->write( `Operator not allowed` ).
    ENDTRY.

    TRY.
        DATA(result3) = calculate( num1 = 10 operator = '*' num2 = 4 ).
        out->write( data = result3 name = `result3` ).
      CATCH lcx_wrong_operator.
        out->write( `Operator not allowed` ).
    ENDTRY.

    "---- Using local class implemented in the CCIMP include ----
    DATA(hi1) = lcl_demo=>say_hello( ).
    out->write( data = hi1 name = `hi1` ).
    DATA(hi2) = lcl_demo=>say_hello( xco_cp=>sy->user( )->name ).
    out->write( data = hi2 name = `hi2` ).

    "--------------- Test include (CCAU) ---------------
    "For running the ABAP Unit test, choose CTRL+Shift+F10 in ADT.
    "Or you can make a right click in the class code, choose
    "'Run As' and '4 ABAP Unit Test'.
  ENDMETHOD.

  METHOD calculate.
    result = SWITCH #( operator
                       WHEN '+' THEN num1 + num2
                       WHEN '-' THEN num1 - num2
                       ELSE THROW lcx_wrong_operator( ) ).
  ENDMETHOD.

ENDCLASS.

Code snippet for the CCDEF include:

CLASS lcx_wrong_operator DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.

TYPES c1 type c length 1.

Code snippet for the CCIMP include:

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: say_hello IMPORTING name TYPE string optional
                             RETURNING VALUE(hi) type string.

ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.

  METHOD say_hello.
    hi = |Hallo{ COND #( when name is supplied then ` ` && name ) }!|.
  ENDMETHOD.

ENDCLASS.

Code snippet for the test include (CCAU):

CLASS ltc_test DEFINITION DEFERRED.
CLASS zcl_demo_test DEFINITION LOCAL FRIENDS ltc_test.

CLASS ltc_test DEFINITION FOR TESTING
RISK LEVEL HARMLESS
DURATION SHORT.

  PRIVATE SECTION.
    METHODS test_calculate FOR TESTING.

ENDCLASS.

CLASS ltc_test IMPLEMENTATION.

  METHOD test_calculate.
    "Creating an object of class under test
    DATA(ref_cut) = NEW zcl_demo_test( ).

    "Calling method that is to be tested
    TRY.
        DATA(result1) = ref_cut->calculate( num1 = 10 operator = '+' num2 = 4 ).
      CATCH lcx_wrong_operator.
    ENDTRY.

    TRY.
        DATA(result2) = ref_cut->calculate( num1 = 10 operator = '-' num2 = 4 ).
      CATCH lcx_wrong_operator.
    ENDTRY.

    "Assertions
    cl_abap_unit_assert=>assert_equals(
          act = result1
          exp = 14
          quit = if_abap_unit_constant=>quit-no ).

    cl_abap_unit_assert=>assert_equals(
          act = result2
          exp = 6
          quit = if_abap_unit_constant=>quit-no ).
  ENDMETHOD.

ENDCLASS.

⬆️ back to top

Visibility of Components

In the class declaration part, you must specify (at least one of the) three visibility sections and include class components to define their visibility. These visibility sections serve the purpose of encapsulation in ABAP Objects. For example, you do not want to make certain components publicly available for all users. The visibility sections are as follows:

PUBLIC SECTION.
Components declared in this section can be accessed from within the class and from all users of the class.
PROTECTED SECTION.
Components declared in this section can be accessed from within the class and subclasses as well as friends - concepts related to inheritance.
PRIVATE SECTION.
Components declared in this section can only be accessed from within the class in which they are declared and its friends.

Summary:

Visible for PUBLIC SECTION PROTECTED SECTION PRIVATE SECTION
Same class and its friends X X X
Any subclasses X X -
Any repository objects X - -

Creating the Visibility Sections

At least one section must be specified.

CLASS local_class DEFINITION.
    PUBLIC SECTION.
      "Here go the components.
    PROTECTED SECTION.
      "Here go the components.
    PRIVATE SECTION.
      "Here go the components.
ENDCLASS.

⬆️ back to top

Defining Components

All components, i. e.

  • attributes (using TYPES, DATA, CLASS-DATA, and CONSTANTS for data types and data objects),
  • methods (using METHODS and CLASS-METHODS),
  • events (using EVENTS and CLASS-EVENTS) as well as
  • interfaces,

are declared in the declaration part of the class. There, they must be assigned to a visibility section.

Two kinds of components are to be distinguished when, for example, looking at declarations using DATA and CLASS-DATA having a preceding CLASS-:

  • Instance components: Components that exist separately for each instance and can only be accessed in instances of a class.
  • Static components (the declarations with CLASS-): Components that exist only once per class. They do no not exclusively exist for specific instances. They can be addressed using the name of the class.

Attributes

  • The attributes of a class (or interface) mean the data objects declared within a class (or interface).
  • Instance attributes (DATA): Determine the state of a objects of a class. The data is only valid in the context of an instance. As shown further down, instance attributes can only be accessed via an object reference variable.
  • Static attributes (CLASS-DATA): Their content is independent of instances of a class and, thus, valid for all instances. That means that if you change such a static attribute, the change is visible in all instances. As shown further down, static attributes can be accessed by using the class name without a prior creation of an instance.

💡 Note

  • You can declare constant data objects that should not be changed using CONSTANTS statements. You specify the values for the constants (which are also static attributes) when you declare them in the declaration part of a class.
  • The addition READ-ONLY can be used in the public visibility section. Effect:
    • Can be read from outside of the class
    • Cannot be changed from outside
    • Can only be changed using methods of the class or its subclasses

Declaring attributes in visibility sections. In the code snippet below, all attributes are declared in the public section of a local class.

CLASS local_class DEFINITION.

    PUBLIC SECTION.
      TYPES some_type TYPE c LENGTH 3.              "Type declaration

      DATA: inst_number TYPE i,                      "Instance attributes
            inst_string TYPE string,
            dobj_r_only TYPE c LENGTH 5 READ-ONLY.   "Read-only attribute

      CLASS-DATA: stat_number TYPE i,                "Static attributes
                  stat_char   TYPE c LENGTH 3.

      CONSTANTS const_num TYPE i VALUE 123.         "Non-changeable constant

    PROTECTED SECTION.
      "Here go more attributes if needed.

    PRIVATE SECTION.
      "Here go more attributes if needed.

ENDCLASS.

CLASS local_class IMPLEMENTATION.

      ... "Here go all method implementations.

ENDCLASS.

⬆️ back to top

Methods

  • Are internal procedures determining the behavior of the class.
  • Can access all of the attributes of a class and, if not defined otherwise, change their content.
  • Have a parameter interface (also known as signature) with which methods can get values to work with when being called and pass values back to the caller (see the notes on formal and actual parameters below).
  • Static methods can only access static attributes of a class and trigger static events. You declare them using CLASS-METHODS statements in a visibility section.
  • Instance methods can access all of the attributes of a class and trigger all events. You declare them using METHODS statements in a visibility section. Note that you must create an instance of a class first before using instance methods.

Parameter Interface

In the simplest form, methods can have no parameter at all. Apart from that, methods can be defined with the following parameters:

Addition Details
IMPORTING Defines one or more input parameters to be imported by the method.
EXPORTING Defines one or more output parameters to be exported by the method.
CHANGING Defines one or more input or output parameters, i. e. that can be both imported and exported.
RETURNING For functional methods, i. e. such methods have only one RETURNING parameter that can be defined. As an output parameter like the EXPORTING parameter, RETURNING parameters pass back values (note that the formal parameters of returning parameters must be passed by value as covered below). In contrast to EXPORTING for which multiple parameters can be specified, only one RETURNING parameter can be specified in a method. If you only need one output parameter, you can benefit from using a RETURNING parameter by shortening the method call and enabling method chaining. Another big plus is that such functional methods can, for example, be used in expressions. In case of standalone method calls, the returned value can be accessed using the addition RECEIVING.
RAISING Used to declare the class-based exceptions that can be propagated from the method to the caller.

💡 Note

  • You may find the addition EXCEPTIONS especially in definitions of older classes. They are for non-class-based exceptions. This addition should not be used in ABAP for Cloud Development.
  • Notes on formal parameter versus actual parameters:
    • You define method parameters by specifying a name with a type which can be a generic or complete type. Examples:
      • fp is the formal parameter that has a complete type: ... meth IMPORTING fp TYPE string ...
      • gen is the formal parameter that has a generic type: ... meth IMPORTING gen TYPE any ...
      • Find more information about generic types also in the Data Types and Data Objects cheat sheet.
    • This formal parameter includes the specification of how the value passing should happen. Parameters can be passed by reference (... REFERENCE(param) ...; note that just specifying the parameter name ... param ... - as a shorter syntax - means passing by reference by default) or by value (... VALUE(param) ...).
    • The actual parameter represents the data object whose content is passed to or copied from a formal parameter as an argument when a procedure is called. If passing by reference is used, a local data object is not created for the actual parameter. Instead, the procedure is given a reference to the actual parameter during the call and works with the actual parameter itself. Note that parameters that are input and passed by reference cannot be modified in the procedure. However, the use of a reference is beneficial regarding the performance compared to creating a local data object.
  • Parameters can be defined as optional using the OPTIONAL addition. In doing so, it is not mandatory to pass an actual parameter. The DEFAULT addition also makes the passing of an actual parameter optional. However, when using this addition, as the name implies, a default value is set.

⬆️ back to top

Constructors

  • Constructors are special methods that are usually used for setting a defined initial value for attributes of the class or its objects.
  • A class has exactly one instance constructor and one static constructor.
  • The declaration and use of constructors is optional.
  • Static constructor:
    • Declared using the predefined name class_constructor as part of a CLASS-METHODS statement in the public visibility section.
    • Has no parameters.
    • Automatically and immediately called once for each class when calling a class for the first time in an internal session, i. e. when, for example, an instance of a class is created or a component is used. Note: If it is not explicitly declared and implemented, it is merely an empty method.
  • Instance constructor:
    • Declared using the predefined name constructor as part of a METHODS statement. In case of global classes, it can only be declared in the public visibility section.
    • Automatically called when a class is instantiated and an instance is created.
    • Can have IMPORTING parameters and raise exceptions.

Example for method definitions: The following snippet shows multiple method definitions in the public section of a local class. Most of the formal parameters of the demo methods below are defined by just using the parameter name. This means passing by reference (returning parameters require to be passed by value).

CLASS local_class DEFINITION.
    PUBLIC.
      METHODS: inst_meth1,                                      "instance methods

               inst_meth2 IMPORTING a TYPE string,

               inst_meth3 IMPORTING b TYPE i
                          EXPORTING c TYPE i,

               inst_meth4 IMPORTING d TYPE string
                          RETURNING VALUE(e) TYPE string,

               inst_meth5 IMPORTING f TYPE i
                          EXPORTING g TYPE i
                          CHANGING  h TYPE string
                          RETURNING VALUE(i) TYPE i
                          RAISING   cx_sy_zerodivide,

              constructor IMPORTING j TYPE i.                   "instance constructor with importing parameter

      CLASS-METHODS: stat_meth1,                                "static methods  

                     stat_meth2 IMPORTING k TYPE i              
                                EXPORTING l TYPE i,

                     class_constructor,                         "static constructor

                     "Options of formal parameter definitions
                     stat_meth3 IMPORTING VALUE(m) TYPE i,       "pass by value
                     stat_meth4 IMPORTING REFERENCE(n) TYPE i,   "pass by reference
                     stat_meth5 IMPORTING o TYPE i,              "same as n; the specification of REFERENCE(...) is optional
                     stat_meth6 RETURNING VALUE(p) TYPE i,       "pass by value once more (note: it's the only option for returning parameters)

                     "OPTIONAL/DEFAULT additions
                     stat_meth7 IMPORTING q TYPE i DEFAULT 123
                                          r TYPE i OPTIONAL,

                     "The examples above use a complete type for 
                     "the parameter specification. Generic types
                     "are possible.
                     stat_meth8 IMPORTING s TYPE any           "Any data type
                                          t TYPE any table     "Any internal table type                 
                                          u TYPE clike.        "Character-like types (c, n, string, d, t and character-like flat structures)

ENDCLASS.

CLASS local_class IMPLEMENTATION.
   METHOD inst_meth1.
      ...
   ENDMETHOD.

  ... "Further method implementations. Note that all declared methods must go here.
ENDCLASS.

⬆️ back to top

Working with Objects and Components

Declaring Reference Variables

  • To create an object, a reference variable must be declared.
  • Such an object reference variable is also necessary for accessing objects and their components. That means objects are not directly accessed but only via references that point to those objects. This object reference variable contains the reference to the object - after assigning the reference to the object (see further down).
"Declaring object reference variables
DATA: ref1 TYPE REF TO local_class,
      ref2 TYPE REF TO global_class,
      ref3 LIKE ref1.

⬆️ back to top

Creating Objects

  • Using the instance operator NEW, you can create objects of a class (and anonymous data objects, too, that are not dealt with here). As a result, you get a reference variable that points to the created object.
  • Regarding the type specifications before and parameters within the parentheses:
    • Right before the first parenthesis after NEW, the type, i. e. the class, must be specified. The # character - instead of the class name - means that the type (TYPE REF TO ...) can be derived from the context (in this case from the type of the reference variable). You can also omit the explicit declaration of a reference variable by declaring a new reference variable inline, for example, using DATA. In this case, the name of the class must be placed after NEW and before the first parenthesis.
    • No parameter specified within the parentheses: No values are passed to the instance constructor of an object. However, non-optional input parameters of the instance constructor of the instantiated class must be filled. No parameters are passed for a class without an explicitly declared instance constructor. See more information: here.
  • The operator basically replaces the syntax CREATE OBJECT you might stumble on.
"Declaring object reference variable
DATA: ref1 TYPE REF TO some_class.

"Creating objects
ref1 = NEW #( ).                     "Type derived from already declared ref1

DATA(ref2) = NEW some_class( ).      "Reference variable declared inline, explicit type
                                     "(class) specification

"Old syntax. Do not use.
"CREATE OBJECT ref3.                  "Type derived from already declared ref3
"CREATE OBJECT ref4 TYPE some_class.  "Corresponds to the result of the expression above

⬆️ back to top

Assigning Reference Variables

To assign or copy reference variables, use the assignment operator =. In the example below, both object reference variables have the same type.

DATA: ref1 TYPE REF TO some_class,
      ref2 TYPE REF TO some_class.

ref1 = NEW #( ).

"Assigning existing reference
ref2 = ref1.

More examples for dealing with object reference variables:

Overwriting reference variables: An object reference is overwritten when a new object is created with a reference variable already pointing to an instance.

ref1 = NEW #( ).

"Existing reference is overwritten
ref1 = NEW #( ).

Retaining object references:

  • If your use case is to retain the object references, for example, if you create multiple objects using the same object reference variable, you can put the reference variables in internal tables that are declared using ... TYPE TABLE OF REF TO ....
  • The following code snippet just visualizes that the object references are not overwritten. Three objects are created with the same reference variable. The internal table includes all object references and, thus, their values are retained.
DATA: ref TYPE REF TO some_class,
      itab TYPE TABLE OF REF TO some_class.

DO 3 TIMES.
  ref = NEW #( ).
  itab = VALUE #( BASE itab ( ref ) ).   "Adding the reference to itab
ENDDO.

Clearing object references: You can use CLEAR statements to explicitly clear a reference variable.

CLEAR ref.

💡 Note
Objects use up space in the memory and should therefore be cleared if they are no longer needed. However, the garbage collector is called periodically and automatically by the ABAP runtime framework and clears all objects without any reference.

⬆️ back to top

Accessing Attributes

  • Instance attributes: Accessed using the object component selector -> via a reference variable.
  • Static attributes: Accessed (if the attributes are visible) using the class component selector => via the class name. You can also declare data objects and types by referring to static attributes.
"Accessing instance attribute via an object reference variable

... ref->some_attribute ...

"Accessing static attributes via the class name

... some_class=>static_attribute ...

"Without the class name only within the class itself
... static_attribute ...

"Type and data object declarations

TYPES some_type LIKE some_class=>some_static_attribute.
DATA dobj1      TYPE some_class=>some_type.
DATA dobj2      LIKE some_class=>some_static_attribute.

⬆️ back to top

Calling Methods

  • Similar to accessing attributes, instance methods are called using -> via a reference variable.
  • Static methods are called using => via the class name. When used within the class in which it is declared, the static method can also be called without class_name=>....
  • When methods are called, the (non-optional) parameters must be specified within parentheses.
  • You might also stumble on method calls with CALL METHOD statements. These statements should no longer be used. Note that CALL METHOD statements are the only option in the context of dynamic programming. Therefore, CALL METHOD statements should be reserved for dynamic method calls.

Examples for instance method calls and static method calls:

"Calling instance methods via reference variable;
"within the parentheses, the parameters must be specified and assigned - if required

ref->inst_meth( ... ).

"Calling static methods via/without the class name

class_name=>stat_meth( ... ).

"Only within the program in which it is declared.
stat_meth( ... ).

"Calling (static) method having no parameter

class_name=>stat_meth( ).

"Calling (static) methods having a single importing parameter:

"Note that in the method call, the caller exports values to the
"method having importing parameters defined; hence, the addition
"EXPORTING is relevant for the caller. The following three method calls are the same

"Explicit use of EXPORTING.
class_name=>meth( EXPORTING a = b ).

"Only importing parameters in the method signature: explicit EXPORTING not needed

class_name=>meth( a = b ).

"If only a single value must be passed:
"the formal parameter name (a) and EXPORTING not needed

stat_meth( b ).

"Calling (static) methods having importing/exporting parameters
"Parameters must be specified if they are not marked as optional

class_name=>meth( EXPORTING a = b c = d     "a/c: importing parameters in the method signature
                  IMPORTING e = f ).        "e: exporting parameter in the method signature

"To store the value of the parameter, you may also declare it inline.

class_name=>meth( EXPORTING a = b c = d
                  IMPORTING e = DATA(z) ).  

"Calling (static) methods having a changing parameter;
"should be reserved for changing an existing local variable and value

DATA h TYPE i VALUE 123.
class_name=>meth( CHANGING g = h ).

"Calling (static) methods having a returning parameter.
"Basically, they do the same as methods with exporting parameters
"but they are way more versatile, and you can save lines of code.

"They do not need temporary variables.
"In the example, the return value is stored in a variable declared inline.

"i and k are importing parameters
DATA(result) = class_name=>meth( i = j k = l ).

"They can be used with other statements, e. g. logical expressions.
"In the example below, the assumption is that the returning parameter is of type i.
IF class_name=>meth( i = j k = l ) > 100.
  ...
ENDIF.

"They enable method chaining.
"The example shows a method to create random integer values.
"The methods have a returning parameter.
DATA(random_no) = cl_abap_random_int=>create( )->get_next( ).

"RECEIVING parameter: Available in methods defined with a returning parameter;
"used in standalone method calls only.
"In the snippet, m is the returning parameter; n stores the result.
class_name=>meth( EXPORTING i = j k = l RECEIVING m = DATA(n) ).

⬆️ back to top

Method Chaining

As shown in the previous example, method chaining is possible for functional method calls (i.e. methods that have exactly one return value declared with RETURNING) at appropriate read positions. In this case, the method's return value is used as an ABAP operand. A chained method call can consist of multiple functional methods that are linked using component selectors ->. The return values of each method are references to the next method.

"The following example demonstrates method chaining
"The following class creates random integers. Find more information in the
"class documentation.
"Both methods have returning parameters specified.
DATA(some_int1) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                                              min  = 1
                                              max  = 10 )->get_next( ).

"Getting to the result as above - not using method chaining and inline declarations.
DATA some_int2 TYPE i.
DATA dref TYPE REF TO cl_abap_random_int.

dref = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                                   min  = 1
                                   max  = 10 ).

some_int2 = dref->get_next( ).

"Using the RECEIVING parameter in a standalone method call
DATA some_int3 TYPE i.
dref->get_next( RECEIVING value = some_int3 ).

"IF statement that uses the return value in a read position
IF cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                               min  = 1
                               max  = 10 )->get_next( ) < 5.
  ... "The random number is lower than 5.
ELSE.
  ... "The random number is greater than 5.
ENDIF.

"Examples using classes of the XCO library (see more information in the
"ABAP for Cloud Development and Misc ABAP Classes cheat sheets), in which
"multiple chained method calls can be specified. Each of the methods
"has a returning parameter specified.

"In the following example, 1 hour is added to the current time.
DATA(add1hour) = xco_cp=>sy->time( xco_cp_time=>time_zone->user )->add( iv_hour = 1 )->as( xco_cp_time=>format->iso_8601_extended )->value.

"In the following example, a string is converted to xstring using a codepage
DATA(xstr) = xco_cp=>string( `Some string` )->as_xstring( xco_cp_character=>code_page->utf_8 )->value.

"In the following example, JSON data is created. First, a JSON data builder
"is created. Then, using different methods, JSON data is added. Finally,
"the JSON data is turned to a string.
DATA(json) = xco_cp_json=>data->builder( )->begin_object(
  )->add_member( 'CarrierId' )->add_string( 'DL'
  )->add_member( 'ConnectionId' )->add_string( '1984'
  )->add_member( 'CityFrom' )->add_string( 'San Francisco'
  )->add_member( 'CityTo' )->add_string( 'New York'
  )->end_object( )->get_data( )->to_string( ).

⬆️ back to top

Self-Reference me

When implementing instance methods, you can optionally make use of the implicitly available object reference variable me which is always available at runtime and points to the respective object itself. You can use it to refer to components of the instance of a particular class:

... some_method( ... ) ...

... me->some_method( ... ) ...

The following code snippet shows a method implementation. In this case, a local data object from within the method and an instance attribute that is declared in the declaration part of the class in which this method is implemented have identical names. me is used to access the non-local data object.

METHOD me_ref.

  DATA str TYPE string VALUE `Local string`.

  DATA(local_string) = str.

  "Assuming there is a variable str declared in the class declaration part.
  DATA(other_string) = me->str.

ENDMETHOD.

⬆️ back to top

Notes on Inheritance

  • Concept: Deriving a new class (i. e. subclass) from an existing one (superclass).
  • In doing so, you create a hierarchical relationship between superclasses and subclasses (an inheritance hierarchy) to form an inheritance tree. This is relevant for a class that can have multiple subclasses and one direct superclass.
  • Subclasses ...
    • inherit and thus adopt all components from superclasses.
    • can be made more specific by declaring new components and redefining instance methods (i. e. you can alter the implementation of inherited methods). In case a subclass has no further components, it contains exactly the components of the superclass - but the ones of the private visibility section are not visible there.
    • can redefine the public and protected instance methods of all preceding superclasses. Note: Regarding the static components of superclasses, accessing them is possible but not redefining them.
    • can themselves have multiple direct subclasses but only one direct superclass.
  • Components that are changed or added to subclasses are not visible to superclasses, hence, these changes are only relevant for the class itself and its subclasses.
  • Classes can rule out derivation: classes cannot inherit from classes that are specified with the addition FINAL (e. g. CLASS global_class DEFINITION PUBLIC FINAL CREATE PUBLIC. ...).

⬆️ back to top

Excursion: Additions ABSTRACT and FINAL

  • Both classes and methods can be defined with the additions ABSTRACT and FINAL.
  • FINAL with ...:
    • Classes: These classes cannot be inherited. All methods are automatically and implicitly FINAL. In this case, the addition FINAL cannot be used for methods.
    • Methods: These methods cannot be redefined in subclasses.
  • ABSTRACT with ...:
    • Classes: Defines abstract classes. You cannot create an instance of an abstract class. To use instance components of an abstract class, you must create an instance of a subclass of such classes.
    • Methods: Defines abstract methods. The addition is only allowed in abstract classes (and not for private methods). These methods cannot be implemented in the implementation part of the class where they are declared. They must be redefined in subclasses. Note that you can also have non-abstract methods in abstract classes.
  • See here more information on class options.
"Declaration of an abstract method of an abstract superclass
"and its implementation in a concrete subclass.
CLASS cls1 DEFINITION ABSTRACT.
  PROTECTED SECTION.
    METHODS meth ABSTRACT.
ENDCLASS.

CLASS cls2 DEFINITION INHERITING FROM cls1.
  PROTECTED SECTION.
    METHODS meth REDEFINITION.
ENDCLASS.

CLASS cls2 IMPLEMENTATION.
  METHOD meth.
    ...
  ENDMETHOD.
ENDCLASS.

⬆️ back to top

Redefining Methods

  • Redefining methods is possible for the public and protected instance (not the static) methods of all preceding superclasses in a subclass (but only if the methods are not specified with FINAL).
  • In the declaration part of the subclass, you must specify the method as follows (and using the same method name): METHODS meth REDEFINITION.
  • This must be done in the same visibility section of the subclass as in the superclass.
  • You cannot change the parameters of the method.
  • Redefined methods work with private attributes of the subclass and cannot access private attributes of the superclass with the same name.
  • If you want to access the identically named method implementation in a superclass from within the method implementation of the subclass, use the pseudo reference super->meth.

💡 Note
Inheritance and constructors:

  • Constructors cannot be redefined.
  • If the instance constructor is implemented in a subclass, the instance constructor of the superclass must be called explicitly using super->constructor, even if the latter is not explicitly declared. An exception to this: Direct subclasses of the root node OBJECT.
  • Regarding the static constructor: When calling a subclass for the first time, the preceding static constructors of all of the entire inheritance tree must have been called first.
  • More information here.

⬆️ back to top

Notes on Polymorphism and Casting

The object orientation concept polymorphism means you can address differently implemented methods belonging to different objects of different classes using one and the same reference variable, for example, object reference variables pointing to a superclass can point to objects of a subclass.

Note the concept of static and dynamic type in this context:

  • Object reference variables (and also interface reference variables) have both a static and a dynamic type.
  • When declaring an object reference variable, e. g. DATA oref TYPE REF TO cl, you determine the static type, i. e. cl - a class - is used to declare the reference variable that is statically defined in the code. This is the class of an object to which the reference variable points to.
  • Similarly, the dynamic type also defines the class of an object which the reference variable points to. However, the dynamic type is determined at runtime, i. e. the class of an object which the reference variable points to can change.
  • Relevant for? This differentiation enters the picture in polymorphism when a reference variable typed with reference to a subclass can always be assigned to reference variables typed with reference to one of its superclasses or their interfaces. That's what is called upcast (or widening cast). Or the assignment is done the other way round. That's what is called downcast (or narrowing cast).

✔️ Hints

  • The following basic rule applies: The static type is always more general than or the same as the dynamic type. The other way round: The dynamic type is always more special than or equal to the static type.
  • That means:
  • If the static type is a class, the dynamic type must be the same class or one of its subclasses.
  • If the static type is an interface, the dynamic type must implement the interface.
  • Regarding assignments: If it can be statically checked that an assignment is possible although the types are different, the assignment is done using the assignment operator = that triggers an upcast automatically.
  • Otherwise, it is a downcast. Here, the assignability is not checked until runtime. The downcast - in contrast to upcasts - must be triggered explicitly using the casting operator CAST. You might see code using the older operator ?=.
  • See more information in the topic Assignment Rules for Reference Variables.

As an example, assume there is an inheritance tree with lcl_super as the superclass and lcl_sub as a direct subclass. lcl_sub2 is a direct subclass of lcl_sub.

In the following code snippet, the rule is met since the superclass is either the same as or more generic than the subclass (the subclass has, for example, redefined methods and is, thus, more specific). Hence, the assignment of an object reference variable pointing to the subclass to a variable pointing to a superclass works. An upcast is triggered. After this casting, the type of oref_super has changed and the methods of lcl_sub can be accessed via oref_super.

"Creating object references
DATA(oref_super) = NEW lcl_super( ).

DATA(oref_sub) = NEW lcl_sub( ).

"Upcast
oref_super = oref_sub.

"The casting might be done when creating the object.
DATA super_ref TYPE REF TO lcl_super.

super_ref = NEW lcl_sub( ).
  • As mentioned above, a downcast must be triggered manually. Just an assignment like oref_sub = oref_super. does not work. A syntax error occurs saying the right-hand variable's type cannot be converted to the left-hand variable's type.
  • If you indeed want to carry out this casting, you must use CAST (or you might see code using the older operator ?=) to overcome this syntax error (but just the syntax error!). Note: You might also use these casting operators for the upcasts. That means oref_super = oref_sub. has the same effect as oref_super = CAST #( oref_sub ).. Using the casting operator for upcasts is usually not necessary.
  • At runtime, the assignment is checked and if the conversion does not work, you face a (catchable) exception. Even more so, the assignment oref_sub = CAST #( oref_super ). does not throw a syntax error but it does not work in this example either because it violates the rule mentioned above (oref_sub is more specific than oref_super).
  • To check whether such an assignment is possible on specific classes, you can use the predicate expression IS INSTANCE OF or the case distinction CASE TYPE OF. Carrying out an upcast before the downcast ensures that the left-hand variable's type is compatible to the right-hand variable's type.
DATA(oref_super) = NEW lcl_super( ).
DATA(oref_sub) = NEW lcl_sub( ).
DATA(oref_sub2) = NEW lcl_sub2( ).

"Downcast impossible (oref_sub is more specific than oref_super);
"the exception is caught here

TRY.
  oref_sub = CAST #( oref_super ).
  CATCH CX_SY_MOVE_CAST_ERROR INTO DATA(e).
    ...
ENDTRY.

"Working downcast with a prior upcast

oref_super = oref_sub2.

"Due to the prior upcast, the following check is actually not necessary.

IF oref_super IS INSTANCE OF lcl_sub.
  oref_sub = CAST #( oref_super ).
  ...
ENDIF.

"Excursion RTTI: Downcasts, CAST and method chaining
"Downcasts particularly play, for example, a role in the context of
"retrieving type information using RTTI. Method chaining is handy
"because it reduces the lines of code in this case.
"The example below shows the retrieval of type information
"regarding the components of a structure. 
"Due to the method chaining in the second example, the three
"statements in the first example are reduced to one statement.

DATA struct4cast TYPE zdemo_abap_carr.

DATA(rtti_a) = cl_abap_typedescr=>describe_by_data( struct4cast ).
DATA(rtti_b) = CAST cl_abap_structdescr( rtti_a ).
DATA(rtti_c) = rtti_b->components.

DATA(rtti_d) = CAST cl_abap_structdescr(
  cl_abap_typedescr=>describe_by_data( struct4cast )
      )->components.

⬆️ back to top

Notes on Interfaces

Interfaces ...

  • represent a template for the components in the public visibility section of classes.
  • enhance classes by adding interface components.
  • are possible as both local and global interfaces.
  • support polymorphism in classes. Each class that implements an interface can implement its methods differently. Interface reference variables can point to objects of all classes that implement the associated interface.
  • can be implemented by classes of an inheritance tree. It can be any number of interfaces. However, each interface can be implemented only once in an inheritance tree.
  • are different from classes in the following ways:
    • They only consist of a part declaring the components without an implementation part. The implementation is done in classes that use the interface.
    • There are no visibility sections. All components of an interface are visible.
    • No instances can be created from interfaces.
    • Declarations as mentioned for classes, e. g. DATA, CLASS-DATA, METHODS, CLASS-METHODS, are possible. Constructors are not possible.

⬆️ back to top

Defining interfaces:

  • Can be done either globally in the repository or locally in an ABAP program.
INTERFACE intf.
"The addition PUBLIC is for global interfaces:
"INTERFACE intf_g PUBLIC.

    DATA ...
    CLASS-DATA ...
    METHODS ...
    CLASS-METHODS ...

ENDINTERFACE.

Implementing interfaces:

  • A class can implement multiple interfaces.
  • Interfaces must be specified in the declaration part of a class using the statement INTERFACES.
  • Since all interface components are public, you must include this statement and the interfaces in the public visibility section of a class. When an interface is implemented in a class, all interface components are added to the other components of the class in the public visibility section.
  • Interface components can be addressed using the interface component selector: ... intf~comp ....
  • You can specify alias names for the interface components using the statement ALIASES ... FOR .... The components can then be addressed using the alias name.
  • The class must implement the methods of all implemented interfaces in it unless the methods are flagged as abstract or final. You can adapt some interface components to requirements of your class.
    • You can specify the additions ABSTRACT METHODS followed by method names or ALL METHODS ABSTRACT for the INTERFACES statement in the declaration part of classes. In this case, the class(es) need not implement the methods of the interface. The implementation is then relevant for a subclass inheriting from a superclass that includes such an interface declaration. Note that the whole class must be abstract.
    • The additions FINAL METHODS followed by method names or ALL METHODS FINAL for the INTERFACES statement in the declaration part of classes flag the method(s) as final.
  • In the interface, methods can mark their implementation as optional using the additions DEFAULT IGNORE or DEFAULT FAIL.

Syntax for using interfaces in classes:

CLASS class DEFINITION.
  PUBLIC SECTION.
    "Multiple interface implementations possible
    INTERFACES intf.
    ALIASES meth_alias FOR intf~some_method.
ENDCLASS.

CLASS class IMPLEMENTATION.
  METHOD intf~some_meth.           "Method implementation using the original name
   ...
  ENDMETHOD.

  "Just for demo purposes: Method implementation using the alias name
  "METHOD meth_alias.
  " ...
  "ENDMETHOD.

  ...
ENDCLASS.

"Abstract class
CLASS cl_super DEFINITION ABSTRACT.
  PUBLIC SECTION.
    INTERFACES intf ALL METHODS ABSTRACT.
    ALIASES:
      meth1 FOR intf~meth1,
      meth2 FOR intf~meth2.
ENDCLASS.

"Subclass inheriting from abstract class and implementing interface methods
CLASS cl_sub DEFINITION INHERITING FROM cl_super.
  PUBLIC SECTION.
    METHODS:
      meth1 REDEFINITION,
      meth2 REDEFINITION.
ENDCLASS.

CLASS cl_sub IMPLEMENTATION.
  METHOD meth1.
    ...
  ENDMETHOD.
  METHOD meth2.
    ...
  ENDMETHOD.
ENDCLASS.

Interface reference variables and accessing objects:

  • As mentioned above, addressing an object happens via an object reference variable with reference to a class.
  • An interface variable can contain references to objects of classes that implement the corresponding interface.
  • You create an interface reference variable like this: DATA i_ref TYPE REF TO intf.

Addressing interface components:

  • Addressing instance components using interface reference variable
    • attribute: i_ref->attr
    • instance method: i_ref->meth( )
  • Addressing instance components using an object reference variable (Note: The type is a class that implements the interface) is also possible but it's not the recommended way:
    • attribute: cl_ref->intf~attr
    • instance method: cl_ref->intf~meth
  • Addressing static components:
    • static attribute: class=>intf~attr,
    • static method: class=>intf~meth( )
    • constant: intf=>const
"Addressing instance interface components using interface reference variable
DATA i_ref TYPE REF TO intf.

DATA cl_ref TYPE REF TO class.

"Creating an instance of a class that implements the interface intf
cl_ref = NEW #( ).

"If the class class implements an interface intf,
"the class reference variable cl_ref can be assigned
"to the interface reference variable i_ref.
"The reference in i_ref then points to the same object
"as the reference in cl_ref.
i_ref = cl_ref.

"Can also be done directly, i. e. directly creating an object to which the interface reference variable points
i_ref = NEW class( ).

"Instance interface method via interface reference variable
... i_ref->inst_method( ... ) ...

"Instance interface attribute via interface reference variable
... i_ref->inst_attr ...

"Addressing instance components using the class reference variable
"is also possible but it's not the recommended way.
... cl_ref->intf~inst_method( ... ) ...
... cl_ref->intf~inst_attr ...

"Addressing static interface components
"class=> can be dropped if the method is called in the same class that implements the interface
... class=>intf~stat_method( ... ) ...
... class=>intf~stat_attr ...

"Just for the record: Static interface components can be called via reference variables, too.
... i_ref->stat_method( ... ) ...
... i_ref->stat_attr ...
... cl_ref->intf~stat_method( ... ) ...

"Constants
"A constant can be addressed using the options mentioned above.
"Plus, it can be addressed using the following pattern
... intf=>const ...

⬆️ back to top

Excursions

Friendship

  • The concept of friendship enters the picture if your use case for your classes is to work together very closely. This is true, for example, for unit tests if you want to test private methods.
  • Classes can grant access to invisible components for their friends.
  • The friends can be other classes and interfaces. In case of interfaces, friendship is granted to all classes that implement the interface.
  • Impact of friendship:
    • Access is granted to all components, regardless of the visibility section or the addition READ-ONLY.
    • Friends of a class can create instances of the class without restrictions.
    • Friendship is a one-way street, i. e. a class granting friendship to another class is not granded friendship the other way round. If class a grants friendship to class b, class b must also explicitly grant friendship to class a so that a can access the invisible components of class b.
    • Friendship and inheritance: Heirs of friends and interfaces that contain a friend as a component interface also become friends. However, granting friendship is not inherited, i. e. a friend of a superclass is not automatically a friend of its subclasses.

You specify the befriended class in the definition part using a FRIENDS addition:

"For local classes. Friendship can be granted to all classes/interfaces
"of the same program and the class library.
"Multiple classes can be specified as friends.
CLASS lo_class DEFINITION FRIENDS other_class ... .
...

CLASS lo_class DEFINITION CREATE PRIVATE FRIENDS other_class ... .

"Addition GLOBAL only allowed for global classes, i. e. if the addition PUBLIC is also used
"Other global classes and interfaces from the class library can be specified after GLOBAL FRIENDS.
CLASS global_class DEFINITION CREATE PUBLIC FRIENDS other_global_class ... .

⬆️ back to top

Events

  • Events can trigger the processing of processing blocks.
  • Declaring events: Can be declared in a visibility section of the declaration part of a class or in an interface, e. g. as
    • instance event using an EVENTS statement. Note that they can only be raised in instance methods of the same class.
    • static event using CLASS-EVENTS. They can be raised in all methods of the same class or of a class that implements the interface. Static event handlers can be called by the event independently of an instance of the class.
"Declaration part of a class/interface
"Instance events
EVENTS: i_evt1,

"Events can only have output parameters that are passed by value
        i_evt2 EXPORTING VALUE(num) TYPE i ...
...
"Static events
CLASS-EVENTS: st_evt1,
              st_evt2 EXPORTING VALUE(num) TYPE i ...
  • Event handlers:
    • An event is raised by a RAISE EVENT statement in another method or in the same method.
    • Raising an event means that event handlers are called.
    • This event handler must be declared with the following syntax (see more information and more additions here):
"Event handlers for instance events
METHODS: handler_meth1 FOR EVENT i_evt1 OF some_class,

         "Parameter names must be the same as declared;
         "no further additions possible for the parameter (e.g. TYPE);
         "the predefined, implicit parameter sender as another formal parameter is possible with instance events,
         "it is typed as a reference variable, which itself has the class/interface as a static type,
         "If the event handler is called by an instance event, it is passed a reference to the raising object in sender.
         handler_meth2 FOR EVENT i_evt2 OF some_class IMPORTING num sender,
...
  • To make sure that an event handler handles a raised event, it must be registered with the statement SET HANDLER. See more information here (for example, events can also be deregistered).
"Registering event for a specific instance
SET HANDLER handler1 FOR ref.

"Registering event for all instances
SET HANDLER handler2 FOR ALL INSTANCES.

"Registering static event for the whole class/interface
SET HANDLER handler3.
"Note that multiple handler methods can be specified.

⬆️ back to top

Factory Methods and Singletons as Design Patterns

In object-oriented programming, there a plenty of design patterns. Covering these ones here to get a rough idea: factory methods and singletons. Both are relevant if you want to restrict or control the instantiation of a class by external users of this class.

A singleton is a design pattern in which it is only up to the class to create objects. In doing so, the class ensures that only one object exists for every internal session that is made available to consumers.

The following code snippet shows an implementation of the singleton design pattern. The get_instance method is used to return the object reference to the object created. Only one instance can be created.

"Using the addition CREATE PRIVATE, objects can only be created by the class itself.
CLASS singleton_class DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS get_instance RETURNING VALUE(ret) TYPE REF TO singleton_class.

  PRIVATE SECTION.
    CLASS-DATA inst TYPE REF TO singleton_class.
ENDCLASS.

CLASS singleton_class IMPLEMENTATION.
  METHOD get_instance.
    IF inst IS NOT BOUND.
      inst = NEW #( ).
    ENDIF.
    ret = inst.
  ENDMETHOD.
ENDCLASS.

Controlling the creation of objects - the instantiation of a class - can be realized using a factory method. For example, certain checks might be required before a class can be instantiated. If a check is not successful, the instantiation is denied. You might create a (static) factory method as follows:

  • A check is carried out in the factory method, for example, by evaluating importing parameters.
  • If the check is successful, an object of the class is created.
  • The method signature includes an output parameter that returns an object reference to the caller.

This is rudimentarily demonstrated in the following snippet:

CLASS class DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
  CLASS-METHODS
    factory_method IMPORTING par ...,
                   RETURNING VALUE(obj) TYPE REF TO class.
  ...
ENDCLASS.

CLASS class IMPLEMENTATION.
  METHOD factory_method.
    IF par = ...
       obj = NEW class( ).
      ELSE.
      ...
    ENDIF.

  ENDMETHOD.
  ...
ENDCLASS.
...

"Calling a factory method.
DATA obj_factory TYPE REF TO class.

obj_factory = class=>factory_method( par = ... ).

⬆️ back to top

More Information

You can check the subtopics of

in the ABAP Keyword Documentation.

Executable Example

zcl_demo_abap_objects

💡 Note

  • The executable example covers the following topics, among others:
    • Working with objects and components
    • Redefining methods, inheritance
    • Working with interfaces
    • Upcast and downcast
    • Concepts such as factory methods, singleton and abstract classes
    • Events
  • The steps to import and run the code are outlined here.
  • Disclaimer