🗓️ Day 16 – Classes & Methods in ABAP Objects (OOPs)

🗓️ Day 16 – Classes & Methods in ABAP Objects (OOPs)

A Deep-Dive into the Core Building Blocks of ABAP OOP

After entering the world of ABAP Objects, Day 16 focuses on the heart of OOP — Classes and Methods.

These are the structures that define what an object is and what an object can do. Understanding them deeply is the foundation for writing scalable, reusable, and maintainable ABAP code.


🔵 1. What is a Class?

A class is a blueprint or template that defines:

  • Attributes (data/variables)
  • Methods (behavior/functions)
  • Events
  • Visibility rules
  • Types, constants

A class does NOT occupy memory until an object (instance) is created.

Example:

CLASS lcl_employee DEFINITION.
  PUBLIC SECTION.
    DATA: name TYPE string,
          age  TYPE i.

    METHODS: display_details.
ENDCLASS.
        

🔵 2. What is an Object?

Objects are instances of classes — meaning a class becomes “alive” only after object creation.

DATA(lo_emp) = NEW lcl_employee( ).
lo_emp->name = 'Rushikesh'.
lo_emp->age  = 23.
lo_emp->display_details( ).
        

Each object gets its own copy of attributes but shares the method definitions.


🔵 3. What are Methods?

Methods define behavior — what the class can do.

Types of methods:

a) Instance Methods

Work on object-specific data.

b) Static Methods

Belong to the class, not to any particular object. No need to create objects.

CLASS-METHODS show_count.
        

c) Constructors

Special methods that run automatically when the object is created.

METHOD constructor.
  "Initialization logic
ENDMETHOD.
        

🔵 4. Method Definitions & Implementations

Definition (What to do)

CLASS lcl_student DEFINITION.
  PUBLIC SECTION.
    METHODS: set_data
      IMPORTING i_name TYPE string
                i_marks TYPE i.

    METHODS: display.
ENDCLASS.
        

Implementation (How to do)

CLASS lcl_student IMPLEMENTATION.
  METHOD set_data.
    name  = i_name.
    marks = i_marks.
  ENDMETHOD.

  METHOD display.
    WRITE: / 'Name:', name,
           / 'Marks:', marks.
  ENDMETHOD.
ENDCLASS.
        

🔵 5. Importing, Exporting & Returning Parameters

Methods can pass data in multiple ways:

IMPORTING

Input values → Method receives

EXPORTING

Output values → Method sends back

CHANGING

Input + modified values

RETURNING

Preferred for returning a single value

Example:

METHOD get_total RETURNING VALUE(rv_total) TYPE i.
  rv_total = marks1 + marks2.
ENDMETHOD.
        

🔵 6. Visibility: PUBLIC, PRIVATE, PROTECTED

PUBLIC SECTION Accessible from anywhere (recommended only when needed)

PRIVATE SECTION Accessible only inside the class (Used for data safety & encapsulation)

PROTECTED SECTION Accessible only inside the class and its subclasses

Best practice:

  • Keep attributes PRIVATE
  • Expose behaviors (methods) through PUBLIC section


🔵 7. Static Attributes & Static Methods

Static = shared across all objects of a class.

CLASS-DATA total_students TYPE i.
CLASS-METHODS increment_count.
        

Call without object:

lcl_student=>increment_count( ).
        

🔵 8. Why Classes & Methods Matter

They bring:

✔️ Reuse → Write once, use many times ✔️ Structure → Clean and modular code ✔️ Security → Encapsulation protects data ✔️ Flexibility → Easy to extend using inheritance ✔️ Readability → Logic separated into meaningful units

SAP modern frameworks like RAP, OData, CDS-based classes, BOPF, and ALV OO depend heavily on Classes & Methods.

To view or add a comment, sign in

More articles by Rushikesh Narawade

Others also viewed

Explore content categories