OOP ABAP
ABAP DevelopmentBeginner

OOP ABAP Basics: Class, Object, Method and Attribute

Understand the basic building blocks of OOP ABAP with a simple runnable local class example.

Explanation

In OOP ABAP, a class is a blueprint and an object is a runtime instance of that class. Attributes store data, and methods perform actions. For example, a class can represent a customer validator. The class contains a method to check whether a customer exists. Instead of writing validation logic directly inside a report, the report creates an object and calls the method. This makes the code easier to read, reuse and test. Beginners should first understand that a class does not do anything until an object is created or a static method is called. In real projects, classes are used to organize validation logic, data fetching, mapping, pricing rules, output preparation and API behavior.

Code example

ABAP Code
REPORT zoop_customer_demo. CLASS lcl_customer_validator DEFINITION.  PUBLIC SECTION.    METHODS is_customer_valid      IMPORTING iv_kunnr TYPE kunnr      RETURNING VALUE(rv_valid) TYPE abap_bool.ENDCLASS. CLASS lcl_customer_validator IMPLEMENTATION.  METHOD is_customer_valid.    SELECT SINGLE kunnr      FROM kna1      WHERE kunnr = @iv_kunnr      INTO @DATA(lv_kunnr).     rv_valid = xsdbool( sy-subrc = 0 ).  ENDMETHOD.ENDCLASS. START-OF-SELECTION.  DATA(lo_validator) = NEW lcl_customer_validator( ).   IF lo_validator->is_customer_valid( '0000100000' ) = abap_true.    WRITE: / 'Customer is valid'.  ELSE.    WRITE: / 'Customer not found'.  ENDIF.

Real project scenario

A sales order upload report validates customer numbers before calling a BAPI. Instead of writing customer validation directly inside the report, create a validator class and call its method from the report.

Common mistakes

- Thinking class and object are the same. - Writing all logic in the report even after creating a class. - Creating a class but keeping all data global. - Using OOP syntax without separating responsibilities.

Best practices

- Create classes for reusable business logic. - Keep method names business-readable. - Avoid unnecessary global variables. - Start with simple local classes before moving to global reusable classes.

Interview angle

For beginners, explain class as blueprint and object as runtime instance. For experienced candidates, explain why validation logic should be moved into reusable classes.