Classes and interfaces

ABAP Objects has been fully object-oriented for years: classes, interfaces, inheritance, polymorphism. An interface defines a contract that several classes can implement independently.

ABAPzif_contract_validator.intf.abap
INTERFACE zif_contract_validator.
  METHODS:
    is_valid
      IMPORTING is_contract     TYPE zcontract
      RETURNING VALUE(rv_valid) TYPE abap_bool.
ENDINTERFACE.
ABAPzcl_contract_validator.clas.abap
CLASS zcl_contract_validator DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES zif_contract_validator.
ENDCLASS.

CLASS zcl_contract_validator IMPLEMENTATION.
  METHOD zif_contract_validator~is_valid.
    rv_valid = xsdbool( is_contract-manager_1 IS NOT INITIAL ).
  ENDMETHOD.
ENDCLASS.

Modern syntax: inline expressions

Since ABAP 7.40, much of the code can be written in a more functional style, avoiding separate helper variable declarations and reducing the number of lines.

ABAPmodern-syntax.abap
" Inline declaration + VALUE constructor
DATA(lt_contracts) = VALUE ty_contracts(
  ( contract_id = '4500001234' manager_1 = 'DOLL' )
  ( contract_id = '4500001235' manager_1 = 'JMART' )
).

" REDUCE: ABAP's equivalent of a functional "reduce"
DATA(lv_count) = REDUCE i(
  INIT n = 0
  FOR ls_contract IN lt_contracts
  NEXT n = n + 1
).

" COND: inline conditional expression
DATA(lv_label) = COND string(
  WHEN lv_count = 0 THEN 'No contracts'
  WHEN lv_count = 1 THEN '1 contract'
  ELSE |{ lv_count } contracts|
).

Class-based exceptions

Modern error handling uses exception classes (inheriting from CX_STATIC_CHECK or similar) instead of classic return codes, making the error flow explicit and typed.

ABAPexception-handling.abap
TRY.
    DATA(lv_result) = zcl_contract_service=>get_contract(
      iv_contract_id = '4500001234'
    ).
  CATCH zcx_contract_not_found INTO DATA(lx_error).
    DATA(lv_msg) = lx_error->get_text( ).
    MESSAGE lv_msg TYPE 'E'.
ENDTRY.
Connection to RAP: the Behavior Implementation classes covered on the RAP page are, underneath, ordinary ABAP classes implementing methods generated from the Behavior Definition — everything above applies equally inside them.

Eclipse ADT vs. SAP GUI

Modern ABAP development (RAP included) is done in Eclipse with ADT (ABAP Development Tools), not in SAP GUI/SE80. ADT provides real autocompletion, quick fixes, and is mandatory for working with CDS views and Behavior Definitions.

CDS Views Previous