SAP ABAP Objects — Object-Oriented Programming Guide
In this tutorial, you'll learn about SAP ABAP Objects. We cover key concepts, practical examples, and best practices.
ABAP Objects is the object-oriented extension of the ABAP language, enabling classes, inheritance, polymorphism, and encapsulation for building modular, reusable, and maintainable SAP applications.
What You'll Learn
You will learn how to define classes, create objects, implement inheritance, use interfaces for polymorphism, raise and handle events, write clean exception handling, and apply design patterns in ABAP.
Why It Matters
Procedural ABAP worked for simple reports, but modern SAP applications demand structured code. Object-oriented ABAP reduces code duplication, enables unit testing with ABAP Unit, simplifies enhancement through inheritance, and makes your code understandable to other developers.
Real-World Use
An SAP shipping system calculates logistics costs differently for each transport mode (air, sea, ground). In procedural ABAP, this requires CASE statements sprawled across multiple programs. With ABAP Objects, a zcl_cost_calculator interface with separate implementations for air, sea, and ground carriers allows adding a new transport mode without touching existing code.
Learning Path
flowchart LR A["SAP ABAP"] --> B["ABAP Objects
You are here"] B --> C["SAP ALV Reports"] C --> D["SAP Workflow"] D --> E["SAP BTP"] style B fill:#f90,color:#fff
Classes and Objects
A class is a blueprint. An object is an instance of that blueprint.
Class Definition
CLASS zcl_employee DEFINITION PUBLIC.
PUBLIC SECTION.
METHODS: constructor
IMPORTING iv_name TYPE string,
get_salary
RETURNING VALUE(rv_salary) TYPE p DECIMALS 2.
PRIVATE SECTION.
DATA: mv_name TYPE string,
mv_base_salary TYPE p DECIMALS 2 VALUE 50000.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD constructor.
mv_name = iv_name.
ENDMETHOD.
METHOD get_salary.
rv_salary = mv_base_salary.
ENDMETHOD.
ENDCLASS.
Using the Class
DATA: lo_employee TYPE REF TO zcl_employee.
CREATE OBJECT lo_employee
EXPORTING
iv_name = 'Alice'.
DATA(lv_salary) = lo_employee->get_salary( ).
WRITE: / 'Employee:', lo_employee->mv_name, " Error — private
/ 'Salary:', lv_salary.
Inheritance
Inheritance creates a parent-child relationship where the child class extends the parent.
CLASS zcl_manager DEFINITION INHERITING FROM zcl_employee.
PUBLIC SECTION.
METHODS: get_salary REDEFINITION,
set_bonus IMPORTING iv_bonus TYPE p DECIMALS 2.
PRIVATE SECTION.
DATA: mv_bonus TYPE p DECIMALS 2.
ENDCLASS.
CLASS zcl_manager IMPLEMENTATION.
METHOD get_salary.
rv_salary = super->get_salary( ) + mv_bonus.
ENDMETHOD.
METHOD set_bonus.
mv_bonus = iv_bonus.
ENDMETHOD.
ENDCLASS.
Usage
DATA: lo_mgr TYPE REF TO zcl_manager.
CREATE OBJECT lo_mgr
EXPORTING
iv_name = 'Bob'.
lo_mgr->set_bonus( 10000 ).
WRITE: / 'Manager salary:', lo_mgr->get_salary( ).
" Output: 60000 (base 50000 + bonus 10000)
Interfaces
Interfaces define a contract that implementing classes must fulfill.
INTERFACE zif_calculator.
METHODS: calculate
IMPORTING iv_amount TYPE p DECIMALS 2
RETURNING VALUE(rv_result) TYPE p DECIMALS 2.
ENDINTERFACE.
CLASS zcl_ground_calc DEFINITION.
PUBLIC SECTION.
INTERFACES zif_calculator.
ENDCLASS.
CLASS zcl_ground_calc IMPLEMENTATION.
METHOD zif_calculator~calculate.
rv_result = iv_amount * 1.10. " Ground rate
ENDMETHOD.
ENDCLASS.
CLASS zcl_air_calc DEFINITION.
PUBLIC SECTION.
INTERFACES zif_calculator.
ENDCLASS.
CLASS zcl_air_calc IMPLEMENTATION.
METHOD zif_calculator~calculate.
rv_result = iv_amount * 3.50. " Air rate
ENDMETHOD.
ENDCLASS.
Polymorphism in Action
DATA: lo_calc TYPE REF TO zif_calculator.
IF iv_transport_mode = 'AIR'.
CREATE OBJECT lo_calc TYPE zcl_air_calc.
ELSE.
CREATE OBJECT lo_calc TYPE zcl_ground_calc.
ENDIF.
DATA(lv_cost) = lo_calc->calculate( 1000 ).
Event Handling
Events allow one object to notify others without knowing about them:
CLASS zcl_inventory DEFINITION.
PUBLIC SECTION.
EVENTS: stock_low
EXPORTING value(ev_material) TYPE string.
METHODS: reduce_stock IMPORTING iv_material TYPE string.
ENDCLASS.
CLASS zcl_inventory IMPLEMENTATION.
METHOD reduce_stock.
IF iv_material = 'BATTERY'.
RAISE EVENT stock_low
EXPORTING
ev_material = iv_material.
ENDIF.
ENDMETHOD.
ENDCLASS.
CLASS zcl_buyer DEFINITION.
PUBLIC SECTION.
METHODS: on_stock_low FOR EVENT stock_low OF zcl_inventory
IMPORTING ev_material.
ENDCLASS.
CLASS zcl_buyer IMPLEMENTATION.
METHOD on_stock_low.
WRITE: / 'Reordering material:', ev_material.
ENDMETHOD.
ENDCLASS.
Exception Handling
CLASS zcl_division DEFINITION.
PUBLIC SECTION.
METHODS: divide
IMPORTING iv_a TYPE i iv_b TYPE i
RETURNING VALUE(rv_result) TYPE p DECIMALS 2
RAISING cx_sy_zerodivide.
ENDCLASS.
CLASS zcl_division IMPLEMENTATION.
METHOD divide.
IF iv_b = 0.
RAISE EXCEPTION TYPE cx_sy_zerodivide.
ENDIF.
rv_result = iv_a / iv_b.
ENDMETHOD.
ENDCLASS.
DATA: lo_div TYPE REF TO zcl_division.
CREATE OBJECT lo_div.
TRY.
DATA(lv_result) = lo_div->divide( iv_a = 10 iv_b = 0 ).
CATCH cx_sy_zerodivide.
WRITE: / 'Cannot divide by zero'.
ENDTRY.
Real-World Scenario: Payroll Processing
zcl_employeebase class withget_salarymethodzcl_managerandzcl_contractorsubclasses override the methodzcl_payroll_processoriterates over employee list polymorphicallyzcl_invoice_senderregisters for thepayment_processedevent- Each employee type is paid correctly without CASE statements
Common Errors
1. Forgetting CREATE OBJECT
Declaring a reference variable does not create the object. Always call CREATE OBJECT before using the object.
2. Calling Private Methods from Outside
Private methods are only accessible within the class. Use protected for subclasses and public for external access.
3. Not Using super-> in Redefined Methods
When overriding a method, call super->method( ) first to execute parent logic before adding child logic.
4. Circular Interface References
Two interfaces referencing each other cause a compilation error. Use only one-directional dependencies.
5. Missing EXPORTING in Event Raises
If the event is defined with EXPORTING, the RAISE EVENT must include the parameter. Otherwise the event handler receives nothing.
6. Not Catching Exceptions
Any exception raised in a method must either be caught or declared in the method signature with RAISING.
Practice Questions
What is the difference between a class and an object? A class is a template/blueprint. An object is a concrete instance of that class in memory.
What does the REDEFINITION keyword do? Allows a subclass to override (redefine) an inherited method with its own implementation.
What is an interface in ABAP Objects? A contract that defines methods an implementing class must provide — enables polymorphism without inheritance.
How do you raise an event in ABAP Objects? Use RAISE EVENT event_name EXPORTING ... within the class method.
What keyword catches an exception? CATCH exception_class within a TRY...ENDTRY block.
Challenge: Build a class hierarchy for a document approval system. Define a base document class, derived classes for invoice, purchase order, and contract. Each has a different approval workflow. Use an interface for the approval step and events when approval is complete.
FAQ
What's Next
| Tutorial | What You'll Learn |
|---|---|
| SAP ABAP Programming | Foundational procedural ABAP before objects |
| SAP ALV Reports | Building interactive reports using ABAP Objects |
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Updated 2026-06-24.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro