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.

Internal tables: choosing the right type

ABAP gives you three internal table types with very different performance characteristics. Picking the wrong one on a large dataset is a classic cause of slow reports.

ABAPinternal-tables.abap
TYPES: BEGIN OF ty_contract,
         contract_id TYPE zcontract_id,
         manager_1   TYPE bu_partner,
       END OF ty_contract.

" Fast key lookups on a large, static reference table
TYPES ty_contracts_hashed TYPE HASHED TABLE OF ty_contract
  WITH UNIQUE KEY contract_id.

DATA(lt_lookup) = VALUE ty_contracts_hashed( ( contract_id = '4500001234' manager_1 = 'DOLL' ) ).
READ TABLE lt_lookup WITH KEY contract_id = '4500001234' INTO DATA(ls_contract). " O(1)

Unit testing with ABAP Unit

Testable ABAP separates business logic from database access behind an interface, so tests can inject a test double instead of hitting a real table — the same dependency-injection idea used in any other language.

ABAPzcl_contract_validator.clas.testclasses.abap
CLASS ltc_contract_validator DEFINITION FOR TESTING
  DURATION SHORT RISK LEVEL HARMLESS.

  PRIVATE SECTION.
    DATA cut TYPE REF TO zif_contract_validator.

    METHODS:
      setup,
      manager_missing_is_invalid FOR TESTING.
ENDCLASS.

CLASS ltc_contract_validator IMPLEMENTATION.

  METHOD setup.
    cut = NEW zcl_contract_validator( ).
  ENDMETHOD.

  METHOD manager_missing_is_invalid.
    DATA(ls_contract) = VALUE zcontract( manager_1 = '' ).
    DATA(lv_result) = cut->is_valid( ls_contract ).
    cl_abap_unit_assert=>assert_false( lv_result ).
  ENDMETHOD.

ENDCLASS.
Why RAP makes this easier: Behavior Implementation classes are already structured around small, single-purpose methods (one validation, one determination), which naturally lends itself to focused unit tests — a big practical advantage over monolithic function modules from the ECC era.
CDS Views Previous