Section 8 of 11

ABAP Object-Oriented Programming

Defining classes, instance versus static members, inheritance and interfaces, and exception classes.

Open contents

ABAP Objects arrived in 1999 and is now the standard way to write ABAP.

Class structure

Definition and implementation
CLASS lcl_order DEFINITION.
  PUBLIC SECTION.
    METHODS constructor IMPORTING iv_order_id TYPE vbeln.
    METHODS get_total RETURNING VALUE(rv_total) TYPE p LENGTH 8 DECIMALS 2.
    CLASS-METHODS create_from_db
      IMPORTING iv_order_id     TYPE vbeln
      RETURNING VALUE(ro_order) TYPE REF TO lcl_order.

  PRIVATE SECTION.
    DATA mv_order_id TYPE vbeln.
    DATA mt_items    TYPE ty_items.
ENDCLASS.

CLASS lcl_order IMPLEMENTATION.
  METHOD constructor.
    mv_order_id = iv_order_id.
  ENDMETHOD.
ENDCLASS.
Visibility
SectionAccessible from
PUBLICAnywhere
PROTECTEDThe class and its subclasses
PRIVATEThe class only

Instance and static

KindKeywordAccess
Instance attributeDATAobj->attr
Static attributeCLASS-DATAclass=>attr
Instance methodMETHODSobj->method( )
Static methodCLASS-METHODSclass=>method( )
Creating objects
DATA(lo_order) = NEW lcl_order( iv_order_id = '0000012345' ).
DATA(lv_total) = lo_order->get_total( ).
DATA(lo_new)   = lcl_order=>create_from_db( '0000012345' ).

Inheritance

Redefining a method
CLASS lcl_rush_order DEFINITION INHERITING FROM lcl_order.
  PUBLIC SECTION.
    METHODS get_total REDEFINITION.
ENDCLASS.

CLASS lcl_rush_order IMPLEMENTATION.
  METHOD get_total.
    rv_total = super->get_total( ) * '1.2'.
  ENDMETHOD.
ENDCLASS.

ABAP supports single inheritance only. To combine behaviours, use interfaces.

Interfaces

Defining and implementing
INTERFACE lif_printable.
  METHODS print.
ENDINTERFACE.

CLASS lcl_invoice DEFINITION.
  PUBLIC SECTION.
    INTERFACES lif_printable.
ENDCLASS.

CLASS lcl_invoice IMPLEMENTATION.
  METHOD lif_printable~print.
    WRITE: / 'Printing the invoice'.
  ENDMETHOD.
ENDCLASS.

DATA lo_printable TYPE REF TO lif_printable.
lo_printable = NEW lcl_invoice( ).
lo_printable->print( ).

Interfaces let unrelated classes be treated uniformly โ€” invoices and delivery notes both handled as printable things. They also make substituting a mock object for testing straightforward.

Exception classes

Raising and catching
IF sy-subrc <> 0.
  RAISE EXCEPTION TYPE zcx_customer_not_found
    EXPORTING customer_id = iv_kunnr.
ENDIF.

TRY.
    DATA(ls_customer) = lo_service->get_customer( '0000001000' ).

  CATCH zcx_customer_not_found INTO DATA(lx_notfound).
    MESSAGE lx_notfound->get_text( ) TYPE 'E'.

  CATCH cx_root INTO DATA(lx_root).
    MESSAGE lx_root->get_text( ) TYPE 'E'.
ENDTRY.
Exception base classes
ClassBehaviour
CX_STATIC_CHECKMust be declared and handled
CX_DYNAMIC_CHECKChecked at runtime; no declaration needed
CX_NO_CHECKNeither declared nor caught; for fatal errors

๐Ÿ“– Unfamiliar term? Look it up in the SAP glossary.

Check your understanding

Test what you just read.

Quiz 1

Which visibility section in an ABAP class allows direct external access?

Quiz 2

ABAP supports class inheritance (INHERITING FROM).

Quiz 3

Which type of method can be called without creating an instance?

Quiz 4

ABAP classes support multiple inheritance (inheriting from multiple parent classes).

Quiz 5

Which statements are used to create an object (instance) in ABAP?

Quiz 6

Which statement correctly describes ABAP interfaces?