All skills
secondsky avatar

/sap-abap

@620a19a
by Eddiesecondsky/sap-skills456 stars
120

Comprehensive ABAP development skill for SAP systems. Use when writing ABAP code, working with internal tables, structures, ABAP SQL, object-oriented programming, RAP (RESTful Application Programming Model), CDS views, EML statements, ABAP Cloud development, string processing, dynamic programming, RTTI/RTTC, field symbols, data references, exception handling, or ABAP unit testing. Covers both classic ABAP and modern ABAP for Cloud Development patterns.

Use this Skill: https://skilld.dev/gh/secondsky/sap-skills/sap-abap

This session only. Nothing lands on disk.

referencesobject-orientation.md

≈2.6k tokens on demand. Your agent reads this file only when SKILL.md points to it.

ABAP Object Orientation - Complete Reference

Source: https://github.com/SAP-samples/abap-cheat-sheets/blob/main/04_ABAP_Object_Orientation.md

Version Note: IS INSTANCE OF and CASE TYPE OF require 7.50+. On 7.40, use CAST with cx_sy_move_cast_error handling or RTTI checks instead. All other OO features (classes, interfaces, inheritance, events, friendship) are available from 7.40.


Class Definition

Basic Structure

CLASS zcl_my_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    " Visible to all
    TYPES: ty_data TYPE string.
    CONSTANTS: c_max TYPE i VALUE 100.
    DATA: mv_public TYPE string.

    METHODS:
      constructor IMPORTING iv_name TYPE string,
      get_name RETURNING VALUE(rv_name) TYPE string,
      process IMPORTING iv_input TYPE string
              EXPORTING ev_output TYPE string
              CHANGING cv_data TYPE string
              RAISING zcx_my_exception.

    CLASS-METHODS:
      factory RETURNING VALUE(ro_instance) TYPE REF TO zcl_my_class.

  PROTECTED SECTION.
    " Visible to class and subclasses
    DATA: mv_protected TYPE string.
    METHODS: internal_process.

  PRIVATE SECTION.
    " Visible only within class
    DATA: mv_private TYPE string.
    METHODS: helper_method.

ENDCLASS.

Class Implementation

CLASS zcl_my_class IMPLEMENTATION.

  METHOD constructor.
    mv_private = iv_name.
  ENDMETHOD.

  METHOD get_name.
    rv_name = mv_private.
  ENDMETHOD.

  METHOD process.
    " Implementation
  ENDMETHOD.

  METHOD factory.
    ro_instance = NEW #( 'Default' ).
  ENDMETHOD.

  METHOD internal_process.
    " Protected method
  ENDMETHOD.

  METHOD helper_method.
    " Private method
  ENDMETHOD.

ENDCLASS.

Visibility Modifiers

Modifier Class Subclass External
PUBLIC Yes Yes Yes
PROTECTED Yes Yes No
PRIVATE Yes No No

CREATE Additions

CREATE PUBLIC     " Can be instantiated anywhere
CREATE PROTECTED  " Only within class and subclasses
CREATE PRIVATE    " Only within the class itself

Class Types

" Final class - cannot be inherited
CLASS zcl_final DEFINITION FINAL.

" Abstract class - cannot be instantiated
CLASS zcl_abstract DEFINITION ABSTRACT.

" Abstract method
CLASS zcl_abstract DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS process ABSTRACT.
ENDCLASS.

Object Instantiation

" CREATE OBJECT
DATA oref TYPE REF TO zcl_my_class.
CREATE OBJECT oref EXPORTING iv_name = 'Test'.

" NEW operator (preferred)
DATA(oref) = NEW zcl_my_class( iv_name = 'Test' ).

" Inline with method call
DATA(result) = NEW zcl_processor( )->process( input ).

" Factory method
DATA(instance) = zcl_my_class=>factory( ).

Method Signatures

Parameter Types

METHODS process
  IMPORTING iv_input TYPE string        " Input (read-only)
  EXPORTING ev_output TYPE string       " Output (cleared first)
  CHANGING cv_data TYPE string          " Input/Output
  RETURNING VALUE(rv_result) TYPE string " Functional return
  RAISING zcx_my_exception.             " Exceptions

Optional Parameters

METHODS process
  IMPORTING
    iv_required TYPE string
    iv_optional TYPE string OPTIONAL    " Can be omitted
    iv_default TYPE i DEFAULT 10.       " Has default value

PREFERRED PARAMETER

METHODS process
  IMPORTING
    iv_first TYPE string
    iv_second TYPE string
  PREFERRED PARAMETER iv_first.

" Call without parameter name
oref->process( 'value' ).  " Goes to iv_first

Method Calls

" Full syntax
oref->method(
  EXPORTING iv_input = value
  IMPORTING ev_output = result
  CHANGING cv_data = data ).

" Functional style (for RETURNING methods)
DATA(result) = oref->get_value( ).

" Chaining
DATA(text) = oref->get_processor( )->process( input )->get_result( ).

" Static method
DATA(instance) = zcl_factory=>create( ).
zcl_utility=>helper_method( ).

Inheritance

Subclass Definition

CLASS zcl_child DEFINITION
  INHERITING FROM zcl_parent
  PUBLIC FINAL CREATE PUBLIC.

  PUBLIC SECTION.
    METHODS constructor
      IMPORTING iv_name TYPE string.

    " Override parent method
    METHODS process REDEFINITION.

    " New method in child
    METHODS additional_method.

ENDCLASS.

CLASS zcl_child IMPLEMENTATION.

  METHOD constructor.
    super->constructor( iv_name ).
    " Additional initialization
  ENDMETHOD.

  METHOD process.
    " Call parent implementation if needed
    super->process( ).
    " Child-specific logic
  ENDMETHOD.

ENDCLASS.

Abstract Classes

CLASS zcl_abstract_base DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS:
      concrete_method,
      abstract_method ABSTRACT.
ENDCLASS.

CLASS zcl_concrete DEFINITION
  INHERITING FROM zcl_abstract_base
  FINAL.
  PUBLIC SECTION.
    METHODS abstract_method REDEFINITION.
ENDCLASS.

Interfaces

Interface Definition

INTERFACE zif_my_interface PUBLIC.
  TYPES: ty_data TYPE string.
  CONSTANTS: c_version TYPE i VALUE 1.
  DATA: mv_value TYPE string.

  METHODS:
    process IMPORTING iv_input TYPE string
            RETURNING VALUE(rv_result) TYPE string,
    get_status RETURNING VALUE(rv_status) TYPE string.

  CLASS-METHODS:
    factory RETURNING VALUE(ro_instance) TYPE REF TO zif_my_interface.

ENDINTERFACE.

Interface Implementation

CLASS zcl_impl DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES zif_my_interface.

    " Alias for convenience
    ALIASES process FOR zif_my_interface~process.
    ALIASES get_status FOR zif_my_interface~get_status.

ENDCLASS.

CLASS zcl_impl IMPLEMENTATION.

  METHOD zif_my_interface~process.
    rv_result = |Processed: { iv_input }|.
  ENDMETHOD.

  METHOD zif_my_interface~get_status.
    rv_status = 'Active'.
  ENDMETHOD.

ENDCLASS.

Interface Reference

" Reference via interface
DATA intf TYPE REF TO zif_my_interface.
intf = NEW zcl_impl( ).
DATA(result) = intf->process( 'input' ).

" Check interface implementation                  " [7.50+] IS INSTANCE OF
IF oref IS INSTANCE OF zif_my_interface.
  DATA(intf) = CAST zif_my_interface( oref ).
ENDIF.

" 7.40 alternative using RTTI:
" DATA(type) = cl_abap_typedescr=>describe_by_object_ref( oref ).
" IF type->is_instance_of( 'ZIF_MY_INTERFACE' ) = abap_true.

Multiple Interfaces

CLASS zcl_multi DEFINITION.
  PUBLIC SECTION.
    INTERFACES: zif_interface_a,
                zif_interface_b.
ENDCLASS.

Interface Inheritance

INTERFACE zif_extended PUBLIC.
  INTERFACES zif_base.
  " Additional methods
  METHODS extended_method.
ENDINTERFACE.

Polymorphism

" Parent type reference
DATA parent TYPE REF TO zcl_parent.

" Can hold child instances
parent = NEW zcl_child_a( ).
parent->process( ).  " Calls zcl_child_a~process

parent = NEW zcl_child_b( ).
parent->process( ).  " Calls zcl_child_b~process

" Interface polymorphism
DATA processor TYPE REF TO zif_processor.
processor = NEW zcl_processor_a( ).
processor->process( ).

processor = NEW zcl_processor_b( ).
processor->process( ).

Type Checking and Casting

" Check instance type                            " [7.50+] IS INSTANCE OF
IF oref IS INSTANCE OF zcl_specific.
  " Is of type or subtype
ENDIF.

" 7.40 alternative: use CAST with error handling
TRY.
    DATA(specific_old) = CAST zcl_specific( general_ref ).
    " Cast succeeded - is of type
  CATCH cx_sy_move_cast_error.
    " Not of type
ENDTRY.

" Safe casting with CAST
TRY.
    DATA(specific) = CAST zcl_specific( general_ref ).
  CATCH cx_sy_move_cast_error.
    " Cast failed
ENDTRY.

" Casting to interface
DATA(intf) = CAST zif_my_interface( oref ).

Events

Event Definition

CLASS zcl_publisher DEFINITION.
  PUBLIC SECTION.
    EVENTS: data_changed
              EXPORTING VALUE(ev_old_value) TYPE string
                        VALUE(ev_new_value) TYPE string.

    METHODS set_data IMPORTING iv_data TYPE string.
ENDCLASS.

CLASS zcl_publisher IMPLEMENTATION.
  METHOD set_data.
    DATA(old) = mv_data.
    mv_data = iv_data.
    RAISE EVENT data_changed
      EXPORTING ev_old_value = old
                ev_new_value = iv_data.
  ENDMETHOD.
ENDCLASS.

Event Handler

CLASS zcl_subscriber DEFINITION.
  PUBLIC SECTION.
    METHODS:
      register_for IMPORTING io_publisher TYPE REF TO zcl_publisher,
      on_data_changed FOR EVENT data_changed OF zcl_publisher
        IMPORTING ev_old_value ev_new_value sender.
ENDCLASS.

CLASS zcl_subscriber IMPLEMENTATION.
  METHOD register_for.
    SET HANDLER on_data_changed FOR io_publisher.
  ENDMETHOD.

  METHOD on_data_changed.
    " Handle event
  ENDMETHOD.
ENDCLASS.

Event Registration

DATA(publisher) = NEW zcl_publisher( ).
DATA(subscriber) = NEW zcl_subscriber( ).

" Register handler
subscriber->register_for( publisher ).

" Trigger event
publisher->set_data( 'new value' ).

Friendship

CLASS zcl_friend DEFINITION DEFERRED.

CLASS zcl_private_class DEFINITION
  FRIENDS zcl_friend.
  PRIVATE SECTION.
    DATA mv_secret TYPE string.
ENDCLASS.

CLASS zcl_friend DEFINITION.
  PUBLIC SECTION.
    METHODS access_secret
      IMPORTING io_private TYPE REF TO zcl_private_class
      RETURNING VALUE(rv_secret) TYPE string.
ENDCLASS.

CLASS zcl_friend IMPLEMENTATION.
  METHOD access_secret.
    " Can access private members
    rv_secret = io_private->mv_secret.
  ENDMETHOD.
ENDCLASS.

Class-Based Exception Handling

CLASS zcx_my_exception DEFINITION
  INHERITING FROM cx_static_check
  PUBLIC FINAL CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_t100_message.

    METHODS constructor
      IMPORTING
        textid   LIKE if_t100_message=>t100key OPTIONAL
        previous LIKE previous OPTIONAL.

ENDCLASS.

Best Practices

  1. Prefer composition over inheritance
  2. Program to interfaces, not implementations
  3. Keep classes focused (Single Responsibility)
  4. Use factory methods for complex instantiation
  5. Prefer FINAL unless inheritance is needed
  6. Use PROTECTED sparingly
  7. Document public interfaces
  8. Use meaningful names for classes and methods

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    A comprehensive reference for SAP ABAP development, providing documentation and code examples for both classic ABAP and modern ABAP Cloud patterns. No security issues were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    3/32 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 620a19a. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 2 months ago
Other metadata
metadata
{
  "maintainer": "Eduard Jiglau",
  "maintainer_email": "hello@sap-ai-skills.com",
  "website": "https://sap-ai-skills.com",
  "version": "2.4.1",
  "last_verified": "2026-04-02",
  "abap_release": "7.40 SP08+ / 7.50+ / ABAP Cloud",
  "sources": [
    "https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm",
    "https://github.com/SAP-samples/abap-cheat-sheets"
  ]
}

README badge

README badge for secondsky/sap-skills/sap-abap