ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentbeginner 9 min read

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

Free sample lesson · open to everyone

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.

In this lessonShow sections

Visual overview

OOP ABAP Part 1: Classes, Objects, Attributes and Methods — learning sheetFlow: Create report ZOOP_BASICS with the code example → Run it: two objects keep separate seat counts → Comment out CREATE OBJECT for the second object and run again — see the dump for an initial reference; then add an IS BOUND check. Key ideas: WHY OOP IN ABAP; DEFINITION AND IMPLEMENTATION; ATTRIBUTES; METHODS. Watch out: A reference variable is initial until CREATE OBJECT — calling a method on it dumps (CX_SY_REF_IS_INITIAL).ERPClimb.com / ABAP / LESSON 1/10 / LEARNING SHEETOOP ABAP Part 1: Classes, Objects, Attributes and Methods1 · HOW IT FLOWS1Create report ZOOP_BASICSwith the code example2Run ittwo objects keep separate seatcounts3Comment out CREATE OBJECTfor the second object andrun again — see the dump…2 · KEY IDEAS EXPLAINEDWhy OOP In ABAPClassic ABAP used FORM routines and functiongroups with global data. Classes bundle data andthe code that works on it, control what outsiders…Definition And ImplementationCLASS lcl_flight DEFINITION — declares attributesand method signatures in sections (PUBLIC,PROTECTED, PRIVATE).CLASS lcl_flight IMPLEMENTATION — contains themethod code.AttributesInstance attributes (DATA) — each object has itsown values (e.g. carrier, connection, seats).Static attributes (CLASS-DATA) — shared by allobjects of the class.MethodsDeclared with METHODS name IMPORTING … EXPORTING …RETURNING VALUE(…); implemented between METHOD andENDMETHOD. Inside, me-> refers to the current…3 · REMEMBER · WATCH OUTRememberA class is a blueprint (definition + implementation); an object is aninstance created at runtime.Objects are accessed through reference variables: DATA lo_flight TYPEREF TO lcl_flight, then CREATE OBJECT lo_flight.Attributes hold the object's data; methods contain its behaviour.!Watch outA reference variable is initialuntil CREATE OBJECT — calling amethod on it dumps(CX_SY_REF_IS_INITIAL).4 · IN A REAL PROJECTA 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'sglobals existed only once per session. With a class, each order became its own object with its own attributes…Original ERPClimb learning sheet • Verify against your SAP release and edition

Expert visual walkthrough

ABAP DEVELOPMENT ACADEMY · Architecture hierarchyERPClimb.com

Classes, Objects, Attributes and Methods

OOP ABAP · Chapter-specific technical teaching map

Architecture hierarchy

1Top level

Class definition

2Related level

Attributes

3Related level

Methods

4Related level

Instances

Tutor's chapter 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.

In a real project

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…

Watch out for

• 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…

Conceptual study map, not an SAP configuration screenshot or a guaranteed runtime sequence. Consult the lesson for implementation prerequisites and exceptions.

The situation: a 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.

Explanation

A class is a blueprint, made up of a definition and an implementation; an object is an instance of that blueprint created at runtime. Objects are accessed through reference variables: you declare DATA lo_flight TYPE REF TO lcl_flight, and then CREATE OBJECT lo_flight to actually bring an instance into existence. Attributes hold an object's data, while methods hold its behavior — but a reference variable stays initial until CREATE OBJECT has been called on it, and calling a method on an initial reference causes a dump (CX_SY_REF_IS_INITIAL).

This lesson opens a ten-part series: Part 1 (this lesson) covers classes and objects; Part 2 covers local versus global classes; Part 3 covers inheritance, interfaces, and polymorphism; Part 4 covers class-based exceptions; Part 5 covers instance and static methods and constructors; Part 6 covers visibility and encapsulation; Part 7 covers composition versus inheritance; Part 8 covers interfaces and factory methods; Part 9 covers dependency injection and ABAP Unit; and Part 10 covers OOP in S/4HANA, RAP, and clean core.

Why bother with OOP in ABAP at all? Classic ABAP relied on FORM routines and function groups built around global data. Classes instead bundle data together with the code that works on it, control what outside code is allowed to access, allow several independent instances to exist at once, and support inheritance, interfaces, and unit testing. Every modern SAP framework — BAdIs, ALV built on CL_SALV_TABLE, RAP, ABAP Unit — is class-based.

A class has two parts: CLASS lcl_flight DEFINITION declares its attributes and method signatures, organized into PUBLIC, PROTECTED, and PRIVATE sections, while CLASS lcl_flight IMPLEMENTATION contains the actual method code.

Attributes come in three forms: instance attributes, declared with DATA, where each object has its own independent values (for example, carrier, connection, seats); static attributes, declared with CLASS-DATA, which are shared by every object of the class; and constants, declared with CONSTANTS.

Methods are declared with METHODS name IMPORTING … EXPORTING … RETURNING VALUE(…), and implemented between METHOD and ENDMETHOD; inside a method, me-> refers to the current object.

To create and use an object: DATA lo_flight TYPE REF TO lcl_flight. declares the reference, CREATE OBJECT lo_flight EXPORTING iv_carrid = 'LH' iv_connid = '0400'. creates the instance, and lo_flight->book_seats( 2 ). calls a method on it. The -> operator accesses instance components, while => accesses static ones. Two separate CREATE OBJECT calls produce two fully independent objects, and once no reference points to an object any longer, the garbage collector removes it.

On syntax: the classic form is CREATE OBJECT lo_flight EXPORTING … followed by CALL METHOD lo_flight->book_seats EXPORTING iv_seats = 2.; the functional call style, available from release 7.02 onward, lets you write lo_flight->book_seats( 2 ). directly; and modern syntax from release 7.40 onward lets you combine declaration and creation in one line: DATA(lo_flight) = NEW lcl_flight( iv_carrid = 'LH' iv_connid = '0400' ).

To practice, create a report named ZOOP_BASICS using the code example, run it and confirm that two objects keep separate seat counts, then comment out the CREATE OBJECT call for the second object and run it again to see the dump caused by an initial reference, before adding an IS BOUND check to guard against it. By the end, you should be able to define a local class, create objects from it, 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.

Watch out

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.

Do this

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).

In the interview

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

0 of 3 questions attempted

Question 1 of 3

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

Practise this in an interview

Answer in your own words and get instant feedback, the way an interviewer would judge it.

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

2Method call raises CX_SY_REF_IS_INITIAL. What happened?

Try a related Code Lab

This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.

Open: God class doing everything →

See it in code

Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.

Was this lesson helpful?

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.