OOP ABAP
ABAP Developmentbeginner

OOP ABAP Part 1: Classes, Objects, Attributes and Methods

Key takeaway

Start object-oriented ABAP the classic way: define and implement a local class, create objects with CREATE OBJECT, use attributes and methods, and understand why SAP moved from FORMs and function groups to classes.

What this complete series covers

Learn OOP ABAP in a clear, practical sequence — from the basic idea to real project use, troubleshooting, and interview-ready understanding.

Who it is for
Best for SAP consultants, developers, and learners who want a practical understanding of OOP ABAP.
Prerequisites
Basic SAP navigation and ABAP syntax help. For hands-on practice, use an ABAP system with ADT or the relevant ABAP development tools.
Final outcome
By the end, you should be able to explain OOP ABAP clearly, follow the main end-to-end flow, apply it in a project scenario, and troubleshoot the most common issues.
  1. Part 1: OOP ABAP Part 1: Classes, Objects, Attributes and Methods

    Learn OOP ABAP through real responsibilities: a class defines behavior and state, an object is its runtime instance, methods expose operations and attributes hold only the state th

  2. Part 2: OOP ABAP Part 2: Local Classes vs Global Classes

    Use local classes for program-specific organization and global classes for reusable business services or public package APIs, choosing the boundary from lifecycle and ownership rather than convenience.

  3. Part 3: OOP ABAP Part 3: Inheritance, Interfaces and Polymorphism

    Design ABAP OO around cohesive classes, explicit interfaces and polymorphism so business logic is testable, replaceable and decoupled from transactions, screens and concrete implementations.

  4. Part 4: OOP ABAP Part 4: Class-Based Exceptions

    Use class-based exceptions to make failure contracts explicit, preserve root cause and separate recoverable business/application conditions from truly unexpected programming failures.

  5. Part 5: OOP ABAP Part 5: Instance Methods, Static Methods and Constructors

    Use instance methods when behavior depends on object state or injected collaborators, static methods for truly stateless class-level operations and constructors to establish valid

  6. Part 6: OOP ABAP Part 6: Encapsulation and Visibility — PUBLIC, PROTECTED, PRIVATE

    Use ABAP OO visibility to protect invariants: expose only stable public behavior, reserve protected members for intentional subclass contracts and keep implementation state private

  7. Part 7: OOP ABAP Part 7: Composition vs Inheritance

    Prefer composition when behavior varies independently of the main class, and use inheritance only for stable is-a relationships where subclasses genuinely share one abstraction and substitutability.

  8. Part 8: OOP ABAP Part 8: Interfaces and Factory Methods

    Use interfaces to define interchangeable behavior and factory methods only to centralize implementation selection, keeping business logic out of the factory and callers dependent on the interface.

  9. Part 9: OOP ABAP Part 9: Dependency Injection and ABAP Unit

    Design ABAP OO for testability by injecting dependencies through interfaces, keeping business rules deterministic and using ABAP Unit test doubles or lightweight fakes instead of p

  10. Part 10: OOP ABAP Part 10: OOP in S/4HANA, RAP and Clean Core

    Use OOP ABAP as the structural foundation for RAP, clean-core extensions and reusable S/4HANA services, keeping framework handlers thin and business logic behind interfaces and coh

System and release assumptions: Examples follow modern ABAP practices. Exact syntax, available APIs, and tools can vary by SAP release, so release-dependent points are called out where needed.

Explanation

📌 KEY POINTS
✅ A class is a blueprint (definition + implementation); an object is an instance created at runtime.

✅ Objects are accessed through reference variables: DATA lo_flight TYPE REF TO lcl_flight, then CREATE OBJECT lo_flight.

✅ Attributes hold the object's data; methods contain its behaviour.

⚠️ A reference variable is initial until CREATE OBJECT — calling a method on it dumps (CX_SY_REF_IS_INITIAL).

━━━━━━━━━━━━━━━━━━━━
▶ WHAT THIS SERIES COVERS

  • Part 1 — classes and objects (this lesson)
  • Part 2 — local vs global classes
  • Part 3 — inheritance, interfaces and polymorphism
  • Part 4 — class-based exceptions
  • Part 5 — instance/static methods and constructors
  • Part 6 — visibility and encapsulation
  • Part 7 — composition vs inheritance
  • Part 8 — interfaces and factory methods
  • Part 9 — dependency injection and ABAP Unit
  • Part 10 — OOP in S/4HANA, RAP and clean core

━━━━━━━━━━━━━━━━━━━━
▶ 1. WHY OOP IN ABAP

Classic ABAP used FORM routines and function groups with global data. Classes bundle data and the code that works on it, control what outsiders can access, allow several independent instances, and support inheritance, interfaces and unit tests. Every modern SAP framework (BAdIs, ALV with CL_SALV_TABLE, RAP, ABAP Unit) is class-based.

━━━━━━━━━━━━━━━━━━━━
▶ 2. DEFINITION AND IMPLEMENTATION

A class has two parts:

  • CLASS lcl_flight DEFINITION — declares attributes and method signatures in sections (PUBLIC, PROTECTED, PRIVATE).
  • CLASS lcl_flight IMPLEMENTATION — contains the method code.

━━━━━━━━━━━━━━━━━━━━
▶ 3. ATTRIBUTES

  • Instance attributes (DATA) — each object has its own values (e.g. carrier, connection, seats).
  • Static attributes (CLASS-DATA) — shared by all objects of the class.
  • Constants (CONSTANTS).

━━━━━━━━━━━━━━━━━━━━
▶ 4. METHODS

Declared with METHODS name IMPORTING … EXPORTING … RETURNING VALUE(…); implemented between METHOD and ENDMETHOD. Inside, me-> refers to the current object.

━━━━━━━━━━━━━━━━━━━━
▶ 5. CREATING AND USING OBJECTS

DATA lo_flight TYPE REF TO lcl_flight.

CREATE OBJECT lo_flight EXPORTING iv_carrid = 'LH' iv_connid = '0400'.

lo_flight->book_seats( 2 ).

  • -> accesses instance components; => accesses static components.
  • Two CREATE OBJECT calls give two independent objects.
  • When no reference points to an object any more, the garbage collector removes it.

━━━━━━━━━━━━━━━━━━━━
▶ 6. CLASSIC VS MODERN SYNTAX

  • Classic: CREATE OBJECT lo_flight EXPORTING …, CALL METHOD lo_flight->book_seats EXPORTING iv_seats = 2.
  • Functional call (7.02+): lo_flight->book_seats( 2 ).
  • 💡 Modern (7.40+): DATA(lo_flight) = NEW lcl_flight( iv_carrid = 'LH' iv_connid = '0400' ).

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

  1. Create report ZOOP_BASICS with the code example.
  2. Run it: two objects keep separate seat counts.
  3. Comment out CREATE OBJECT for the second object and run again — see the dump for an initial reference; then add an IS BOUND check.

✅ CHECKPOINT: you can define a local class, create objects and call their methods.

Code example

ABAP Code
REPORT zoop_basics. * ===== Step 1: class definition - WHAT the class offers =====CLASS lcl_flight DEFINITION.  PUBLIC SECTION.    METHODS: constructor IMPORTING iv_carrid TYPE s_carr_id                                   iv_connid TYPE s_conn_id                                   iv_seatsmax TYPE i,             book_seats  IMPORTING iv_seats TYPE i,             get_free_seats RETURNING VALUE(rv_free) TYPE i,             display.    CLASS-DATA gv_objects TYPE i READ-ONLY.      " static: shared by all objects  PRIVATE SECTION.    DATA: mv_carrid   TYPE s_carr_id,             " instance attributes          mv_connid   TYPE s_conn_id,          mv_seatsmax TYPE i,          mv_seatsocc TYPE i.ENDCLASS. * ===== Step 2: class implementation - HOW it works =====CLASS lcl_flight IMPLEMENTATION.  METHOD constructor.    mv_carrid   = iv_carrid.    mv_connid   = iv_connid.    mv_seatsmax = iv_seatsmax.    gv_objects  = gv_objects + 1.  ENDMETHOD.   METHOD book_seats.    IF iv_seats <= get_free_seats( ).      mv_seatsocc = mv_seatsocc + iv_seats.    ELSE.      WRITE: / 'Not enough seats on', mv_carrid, mv_connid.    ENDIF.  ENDMETHOD.   METHOD get_free_seats.    rv_free = mv_seatsmax - mv_seatsocc.  ENDMETHOD.   METHOD display.    DATA lv_free TYPE i.    lv_free = get_free_seats( ).    WRITE: / me->mv_carrid, me->mv_connid, 'occupied:', mv_seatsocc, 'free:', lv_free.  ENDMETHOD.ENDCLASS. * ===== Step 3: use the class =====DATA: lo_flight1 TYPE REF TO lcl_flight,      lo_flight2 TYPE REF TO lcl_flight. START-OF-SELECTION.  CREATE OBJECT lo_flight1 EXPORTING iv_carrid = 'LH' iv_connid = '0400' iv_seatsmax = 10.  CREATE OBJECT lo_flight2 EXPORTING iv_carrid = 'AA' iv_connid = '0017' iv_seatsmax = 5.   CALL METHOD lo_flight1->book_seats EXPORTING iv_seats = 4.   " classic call  lo_flight2->book_seats( 6 ).                                  " functional call   lo_flight1->display( ).  lo_flight2->display( ).  WRITE: / 'Objects created:', lcl_flight=>gv_objects.         " static access with => * ===== Step 4: always check an optional reference =====  DATA lo_flight3 TYPE REF TO lcl_flight.  IF lo_flight3 IS BOUND.    lo_flight3->display( ).  ELSE.    WRITE: / 'lo_flight3 was never created'.  ENDIF.

Real project scenario

A developer converted a function group that kept one "current order" in global data into a class. Previously, processing two orders in one program overwrote each other's data because the function group's globals existed only once per session.
With a class, each order became its own object with its own attributes. Parallel processing of several orders in one run worked correctly, and the class could be unit-tested — the main reason the team adopted classes for all new logic.

Common mistakes

  • Calling a method on an initial reference (CX_SY_REF_IS_INITIAL).
  • Confusing -> (instance) and => (static).
  • Making all attributes PUBLIC.
  • Putting all code into one huge method.
  • Using static attributes for data that belongs to each object.

Best practices

  • Keep attributes private and expose behaviour through methods.
  • Use a constructor to create objects in a valid state.
  • Check IS BOUND for optional references.
  • Name references lo_/mo_, attributes mv_/mt_, static gv_ or per team conventions.
  • Prefer global classes for reusable logic (Part 2).

Interview angle

Questions: "Difference between a class and an object?", "What is a reference variable?", "-> vs =>?"
Strong answer: a class is a definition (attributes, methods); objects are runtime instances created with CREATE OBJECT/NEW and accessed via reference variables; -> for instance components, => for static; an initial reference dumps when used; the garbage collector frees unreferenced objects; OOP gives encapsulation, multiple instances and testability over FORMs/function groups.

Practice this lesson

Quick check

1. What happens when a method is called on a reference variable that was declared but never used with CREATE OBJECT?

2. Which operator accesses a static attribute of class lcl_flight?

3. In a class, where is the code of a method written?

Interview questions

1. Explain object references and object lifetime in ABAP OO. When are objects automatically garbage collected, and how does this differ from function module data?

Answer it in your own words first, then compare.

2. Method call raises CX_SY_REF_IS_INITIAL. What happened?

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics