Timo Denk's Blog

ABAP Objects Quick Reference

· Timo Denk

This is the second (and final) part of a quick reference for the programming language ABAP. Extending the first part, this one is primarily focusing on ABAP’s object oriented programming features, like classes, OOP-exceptions, and inheritance.

The first part can be found here: ABAP Quick Reference

Encapsulation using function groups

FUNCTION-POOL s_person.

* age is a global variable in the function-pool
DATA: age TYPE i.

FUNCTION inc_age.
  ADD 1 to age.
ENDFUNCTION.

Local class definition

REPORT ... .
DATA: ...
...
CLASS lcl_classname DEFINITION.
  " class definition
  ...
ENDCLASS.

CLASS lcl_classname IMPLEMENTATION.
  " class implementation
  ...
ENDCLASS.

* report content

CREATE OBJECT ...

Public and private attributes

ABAP has the visibilities publicprivate, and protected.

CLASS lcl_classname DEFINITION.

  PUBLIC SECTION.
    DATA: mv_publicvar TYPE i.

  PRIVATE SECTION.
    DATA: mv_privatevar1 TYPE i READ-ONLY, " const
          mv_privatevar2 LIKE mv_privatevar1.
    
    " static variable
    CLASS-DATA: gv_n_o_staticvar TYPE i.

ENDCLASS.

Methods

Normal methods are declared using the METHOD keyword. CLASS-METHODS are ABAP’s equivalent to static methods, known from other programming languages. The actual implementation uses the same keyword, for both types of methods: METHOD

CLASS lcl_classname.

  " attributes
  ...

  PUBLIC SECTION.

    METHODS method_name
      IMPORTING        iv_par TYPE ANY
      EXPORTING        ev_par TYPE i
      CHANGING         cv_par TYPE type_name
      RETURNING value(rv_par) TYPE i
      EXCEPTIONS exception " sy-subrc id: 1
      RAISING exception_class.

  PROTECTED SECTION.

    " protected static method
    CLASS-METHODS static_method
      EXPORTING ev_result TYPE i.


ENCLASS.

...

CLASS lcl_classname.
  METHOD method_name.
    " method body
    ...
  ENDMETHOD.

  METHOD static_method.
    ...
  ENDMETHOD.
ENDCLASS.

Create an object

CLASS lcl_classname.
...
ENDCLASS.

...

START-OF-SELECTION.

* create reference
DATA: 
  go_objectname TYPE REF TO lcl_classname,
  gt_objecttablename TYPE TABLE OF REF TO lcl_classname.

* create instance
CREATE OBJECT go_objectname.

* append object to table
APPEND go_objectname TO gt_objecttablename.

Call methods and access attributes

DATA: ref TYPE REF TO lcl_classname.
...

* call object's method
CALL METHOD ref->method_name
  EXPORTING iv_par = val_ex
  ... .

ref->method_name( EXPORTING iv_par = val_ex ... ).


* call static method
CALL METHOD class_name=>method_name ... .
class_name=>method_name( ... ).


* function with return value
return_value = ref->method_name( ... ).

Constructor

The this keyword from other programming languages is called me in ABAP Objects.

CLASS classname DEFINITION.

  METHODS constructor ... .

  ...
ENDCLASS.

CLASS classname IMPLEMENTATION.
  
  METHODS constructor ... .

ENDCLASS.

...

CREATE OBJECT ref
  EXPORTING iv_par = val_ex
  ... .
CLASS classname DEFINITION.
  PUBLIC-SECTION.
    " class constructor
    CLASS-METHODS class_constructor.
ENDCLASS.

CLASS classname IMPLEMENTATION.
  METHOD class_constructor.
    ...
  ENDMETHOD.
ENDCLASS.

Inheritance and overwriting methods

CLASS parent_class DEFINITION.
  PUBLIC SECTION.
    METHODS method_name.
ENDCLASS.

...

* class inherits from parent class
CLASS inheriting_class DEFINITION INHERITING FROM parent_class.
  PUBLIC SECTION.
    METHODS method_name REDEFINITION. " overwrite method
ENDCLASS.

CLASS inheriting_class IMPLEMENTATION.
  METHOD method_name.
    super->parent_method( ).
  ENDMETHOD.
ENDCLASS.

Up-casts and down-casts

When down-casting to a wrong object reference the cx_sy_move_cast_error exception will be thrown (and can be caught using TRY. CATCH … . ENDTRY.).

See section Run time type services for a clean way to realize down-casts without the TRY … CATCH construct.

DATA: go_fruit TYPE REF TO lcl_fruit,
      go_orange TYPE REF TO lcl_orange, " lcl_orange is child class of lcl_fruit
      go_orange2 TYPE REF TO lcl_orange. " second orange

CREATE OBJECT go_orange.

* up-cast (not cricital)
go_fruit = go_orange.

* down-cast (narrowing-cast; critical)
go_orange2 ?= go_fruit.

 

Interfaces

While multiple inheritance is not available in ABAP, a class can implement multiple interfaces.

* define an interface (public components only)
INTERFACE lif_interface.
  METHODS interface_method.
ENDINTERFACE.

* implement interface in a class
CLASS lcl_class DEFINITION.
  PUBLIC SECTION.
    INTERFACES lif_interface. " multiple interfaces possible
ENDCLASS.

* implement interface method
CLASS lcl_class IMPLEMENTATION.
  METHOD lif_interface~interface_method.
    ...
  ENDMETHOD.
ENDCLASS.

* call interface method
CREATE OBJECT ...
go_object->lif_interface~interface_method( ).

Events

Four steps are necessary to work with events:

  1. Event definition in method signature (EVENTS …)
  2. Event raising in method implementation (RAISE EVENT …)
  3. Event handling method (METHOD … FOR EVENT)
  4. Event set handler  (SET HANDLER …)

Raise events

* event definition
CLASS raising_class DEFINITION.
  EVENTS eventname [ EXPORTING value(ev_par) TYPE typename ].
  ...
ENDCLASS.


* event implementation
CLASS raising_class IMPLEMENTATION.
  METHOD methodname.
    RAISE EVENT eventname [ EXPORTING ev_par = lv_par ].
  ENDMETHOD.
ENDCLASS.

Handle events

* handling class definition
CLASS handling_class DEFINITION.
  METHODS on_eventname 
    FOR EVENT eventname 
    OF raising_class " or interface
    [ IMPORTING ev_par1 ev_par2 ... [ sender ] ].
ENDCLASS.


* register for handling (e.g. in class constructor)
SET HANDLER ref_handler->on_eventname " ref_handler e.g. "me"
  [ FOR ref_sender | FOR ALL REFERENCES ]
  [ ACTIVATION flag ].

sender will be a reference to the object raising the event.

OOP design patterns

Abstract classes

* abstract class
CLASS lcl_classname DEFINITION ABSTRACT.
  * abstract method
  METHODS ... ABSTRACT ... .
ENDCLASS.

Final classes

* final class
CLASS lcl_classname DEFINITION FINAL.
  * final method
  METHODS ... FINAL ... .
ENDCLASS.

Constructor visibility

CLASS lcl_classname DEFINITION CREATE < PUBLIC | PROTECTED | PRIVATE >.
  ...
ENDCLASS.

Friend classes

Classes can allow other classes to access private and protected methods (including the constructor method; see Constructor visibility) and attributes by declaring them as friends.

CLASS lcl_2 DEFINITION.
  ...
ENDCLASS.

CLASS lcl_1 DEFINITION CREATE PRIVATE FRIENDS lcl_2.
  PRIVATE SECTION.
    DATA attribute1.
ENDCLASS.

...

CLASS lcl_2 IMPLEMENTATION.
  METHOD method.
    CREATE OBJECT lcl_1_instance.
    lcl_1_instance->attribute1 = ... .
  ENDMETHOD.
ENDCLASS.

 Exceptions

The traditional way of throwing exceptions in ABAP was RAISE, the new, object oriented style for throwing an exception object uses the keywords RAISE EXCEPTION TYPE

* throw exception
RAISE EXCEPTION TYPE cx_exception
  [ EXPORTING export1 = ... ].

* handle exceptions
TRY.
  ...
CATCH cx_exception INTO gx_exception_object.
  gx_exception_object->get_text( ).
  ...
ENDTRY.

Class methods that trow exceptions need that information in their definition (the non oop style RAISING became EXCEPTIONS):

METHODS:
  method_name
    ...
    EXCEPTIONS " oop style exceptions
      cx_exception_class.

ABAP also features RETRY and CATCH BEFORE UNWIND … RESUME. Whereas former restarts the entire TRY block and latter continues right after the line where the exception was raised (RAISE RESUMABLE EXCEPTION).

Run time type services

In order to avoid the TRY … CATCH construct to guarantee error free down-casting the following code snippet can be used, to identify the class of an object.

lo_descr ?= cl_abap_typedescr=>describe_by_object_ref( object_reference ).
IF lo_descr->get_relative_name( ) = 'CLASS_NAME'.
  " down-cast to CLASS_NAME possible
  ...
ENDIF.