Let the object guard its own rules

Suppose a parcel can move from Packed to Shipped, but an external caller should not set its status to arbitrary text. If every caller can change the field directly, every caller must remember the rule. Encapsulation moves that responsibility to the object.

Visibility controls the public surface

ABAP classes organize components into PUBLIC SECTION, PROTECTED SECTION, and PRIVATE SECTION. Keep implementation details private; expose the smallest method interface callers need.

CLASS lcl_parcel DEFINITION.
  PUBLIC SECTION.
    METHODS set_status IMPORTING iv_status TYPE string.
    METHODS get_status RETURNING VALUE(result) TYPE string.
  PRIVATE SECTION.
    DATA status TYPE string.
ENDCLASS.
METHOD set_status.
  IF iv_status = 'Packed' OR iv_status = 'Shipped'.
    status = iv_status.
  ENDIF.
ENDMETHOD.

The example shows the boundary, but silently ignoring bad input is not always good design. A production method could return a result or raise a specific exception so the caller knows a requested change was rejected.

Invariants are rules the object keeps true

An invariant is a condition that should remain true for the object’s lifetime—for example, a shipped parcel must have a tracking code. Validate changes at the boundary, and avoid exposing writable attributes that let callers bypass the rule.

Practice

Make status private; provide a setter that allows only the two stated values; provide a getter and assign its return to result. Add the selected output statement. Structural checking is not execution.

Version note: Visibility concepts are fundamental, while syntax and allowed APIs are still subject to the target release/language version.

Sources

Developer practice · ABAP

Practice: keep invalid status changes out

Make the status private, provide a public method to set it, and only accept Packed or Shipped. Set result to the status.

Choose your ABAP environment

This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.

Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.

0 required structures detected · 0 checks