Skip to content

Lesson 4 — Classes, Objects & Encapsulation ​

Structures hold data. Objects combine state and behavior behind a public API.

text
Product object
  private state
    code
    name
    stock
  public behavior
    add_stock
    remove_stock
    get_stock

This lesson starts with private state rather than teaching public mutable attributes as the default design.

Learning objectives ​

You should be able to:

  • distinguish a class from an object;
  • create a global class in ADT;
  • use PUBLIC SECTION and PRIVATE SECTION;
  • declare instance attributes;
  • implement an instance constructor;
  • instantiate objects with NEW;
  • call instance methods with ->;
  • implement command and query methods;
  • create several independent objects from one class;
  • explain encapsulation and object identity.

1. Create the Product class ​

Create:

text
ZCL_PRODUCT_###

Do not implement IF_OO_ADT_CLASSRUN in the Product class.

It represents a Product, not the console entry point.

2. Define a public API and private state ​

abap
CLASS zcl_product_### DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.

    METHODS constructor
      IMPORTING
        code          TYPE c LENGTH 6
        name          TYPE string
        initial_stock TYPE i.

    METHODS add_stock
      IMPORTING quantity TYPE i.

    METHODS remove_stock
      IMPORTING quantity TYPE i
      RETURNING VALUE(removed) TYPE abap_bool.

    METHODS get_code
      RETURNING VALUE(code) TYPE c LENGTH 6.

    METHODS get_name
      RETURNING VALUE(name) TYPE string.

    METHODS get_stock
      RETURNING VALUE(stock) TYPE i.

  PRIVATE SECTION.

    DATA code  TYPE c LENGTH 6.
    DATA name  TYPE string.
    DATA stock TYPE i.

ENDCLASS.

3. Why private attributes? ​

If stock were publicly writable, any caller could do:

text
stock = -500

without using Product behavior.

Private state means callers interact through methods:

text
add_stock
remove_stock
get_stock

The class controls its own rules.

4. Constructor ​

abap
METHOD constructor.
  me->code = code.
  me->name = name.

  IF initial_stock >= 0.
    stock = initial_stock.
  ELSE.
    stock = 0.
  ENDIF.
ENDMETHOD.

me-> explicitly refers to the current object when a parameter has the same name as an attribute.

5. Add stock ​

abap
METHOD add_stock.
  IF quantity > 0.
    stock += quantity.
  ENDIF.
ENDMETHOD.

6. Remove stock ​

abap
METHOD remove_stock.
  removed = abap_false.

  IF quantity > 0 AND quantity <= stock.
    stock -= quantity.
    removed = abap_true.
  ENDIF.
ENDMETHOD.

The method returns whether the operation succeeded.

7. Getters ​

abap
METHOD get_code.
  code = me->code.
ENDMETHOD.

METHOD get_name.
  name = me->name.
ENDMETHOD.

METHOD get_stock.
  stock = me->stock.
ENDMETHOD.

A getter exposes information without allowing direct mutation of the attribute.

8. Create the Runner class ​

Create:

text
ZCL_ABAP_L04_###

Implement IF_OO_ADT_CLASSRUN.

9. Instantiate with NEW ​

abap
DATA(product) = NEW zcl_product_###(
  code          = 'P00001'
  name          = 'Keyboard'
  initial_stock = 10
).

NEW creates an object and returns an object reference.

The variable product is a reference to that object.

10. Call methods with -> ​

abap
product->add_stock( quantity = 5 ).

DATA(stock) = product->get_stock( ).

out->write( |Stock: { stock }| ).

Instance component selector:

text
->

11. Test removal ​

abap
IF product->remove_stock( quantity = 8 ) = abap_true.
  out->write( 'Stock removed' ).
ELSE.
  out->write( 'Could not remove stock' ).
ENDIF.

12. Create several products ​

abap
DATA(keyboard) = NEW zcl_product_###(
  code = 'P00001' name = 'Keyboard' initial_stock = 10 ).

DATA(mouse) = NEW zcl_product_###(
  code = 'P00002' name = 'Mouse' initial_stock = 40 ).

Each object has its own state.

text
keyboard object stock = 10
mouse object stock    = 40

Changing one does not modify the other.

13. Class vs object ​

text
Class
  definition / blueprint / type

Object
  runtime instance with identity and state

A class can have many objects.

14. Command vs query methods ​

Useful design vocabulary:

text
Command
  changes object state
  add_stock
  remove_stock

Query
  returns information
  get_stock
  get_name

Keeping methods focused makes object behavior easier to reason about.

15. Why no set_stock? ​

A generic setter:

text
set_stock( -500 )

would bypass the domain operations.

Prefer meaningful behavior:

text
add_stock
remove_stock

This is an important encapsulation principle: expose operations that reflect the problem domain, not automatic setters for every field.

16. Summary ​

You now understand:

text
class
object
reference
NEW
private state
constructor
public methods
->
encapsulation
object identity

After this lesson's practice and companion, complete the Local Classes & Local Types mini lesson. Lesson 5 then strengthens object APIs with validation and collaboration between objects.