ABAP Objects Workshop
Vinitha Rajan Intelligroup Asia Ltd. Intelligroup Asia Ltd., Technology Team
SAP AG
Topics Covered
Object orientation Objects & Object References ABAP Objects
Intelligroup Asia Ltd., Technology Team
Object Orientation
Data
Data
Data
Attributes Data
Data
Data
Abstraction Function
Methods Function Function
Function
Method Method Method
Function Function Function Function
Functions and data Data model as an abstraction of the real world
Intelligroup Asia Ltd., Technology Team
Software objects Object model as an abstraction of the real world
Real-world objects
Objects & Object References
Interface Private components
Airline Flight number
Flight
Private access
Customer
Address
Passengerlist
Intelligroup Asia Ltd., Technology Team
Public methods BOOK
FLIGHT
Public attributes
Public events
Public access
ABAP Objects
ABAP Objects is an upwards-compatible extension of the existing ABAP language You can use existing ABAP statements within ABAP Objects You can use ABAP Objects within existing programs ABAP Objects is fully integrated in the ABAP Debugger
Intelligroup Asia Ltd., Technology Team
From Function Groups to Classes
Instances of function groups as objects Example: Function group as counter
Intelligroup Asia Ltd., Technology Team
Instances of Function Groups as Objects
Function group 1
Function module …
Function group 2
Data
Function module
Data
…
ABAP program with data CALL FUNCTION ... Internal session of an ABAP program External session
Intelligroup Asia Ltd., Technology Team
Function Group as Counter - Definition
FUNCTION-POOL COUNTER. DATA COUNT TYPE I. FUNCTION SET_COUNTER. * Local Interface IMPORTING VALUE(SET_VALUE) COUNT = SET_VALUE. ENDFUNCTION. FUNCTION INCREMENT_COUNTER. COUNT = COUNT + 1. ENDFUNCTION. FUNCTION GET_COUNTER. * Local Interface: EXPORTING VALUE(GET_VALUE) GET_VALUE = COUNT. ENDFUNCTION.
Intelligroup Asia Ltd., Technology Team
Function Group as Counter - Call
DATA NUMBER TYPE I VALUE 5. CALL FUNCTION 'SET_COUNTER' EXPORTING SET_VALUE = NUMBER. DO 3 TIMES. CALL FUNCTION 'INCREMENT_COUNTER'. ENDDO. CALL FUNCTION 'GET_COUNTER' IMPORTING GET_VALUE = NUMBER.
NUMBER has value 8
Intelligroup Asia Ltd., Technology Team
Classes Generalize Function Groups
nth instance, class m 1st instance, class m
nth instance, class 1 1st instance, class 1
Daten Interface Data Interface Data
Daten Interface Data Interface Data
...
...
...
...
…
…
…
…
ABAP program with data Internal session of an ABAP program External session
Intelligroup Asia Ltd., Technology Team
Classes, References, and Objects
Example: Class as counter Reference variables Creating objects Calling methods Working with references
Intelligroup Asia Ltd., Technology Team
Example: Class as Counter
CLASS counter DEFINITION. PUBLIC SECTION. METHODS: set IMPORTING VALUE(set_value) TYPE i, increment, get EXPORTING VALUE(get_value) TYPE i. PRIVATE SECTION. DATA count TYPE i. ENDCLASS. CLASS counter IMPLEMENTATION. METHOD set. count = set_value. ENDMETHOD. METHOD increment. count = count + 1. ENDMETHOD. METHOD get. get_value = count. ENDMETHOD. ENDCLASS. Intelligroup Asia Ltd., Technology Team
Reference Variables
DATA: cnt_1 TYPE REF TO counter.
CNT_1 Intelligroup Asia Ltd., Technology Team
Creating an Object
DATA: cnt_1 TYPE REF TO counter.
CREATE OBJECT cnt_1 TYPE counter.
1
CNT_1 Intelligroup Asia Ltd., Technology Team
Calling Methods
DATA: cnt_1 TYPE REF TO counter. DATA number TYPE I VALUE 5. CREATE OBJECT cnt_1 TYPE counter. CALL METHOD cnt_1->set EXPORTING set_value = number. DO 3 TIMES. CALL METHOD cnt_1->increment. ENDDO. 1
CALL METHOD cnt_1->get IMPORTING get_value = number.
NUMBER has the value 8 Intelligroup Asia Ltd., Technology Team
CNT_1
Several Reference Variables
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter.
CNT_3 CNT_2 CNT_1 Intelligroup Asia Ltd., Technology Team
Several Objects
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1, cnt_2. 2
1
CNT_3 CNT_2 CNT_1 Intelligroup Asia Ltd., Technology Team
Assigning Reference Variables
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1, cnt_2.
2
MOVE cnt_2 TO cnt_3.
1
CNT_3 CNT_2 CNT_1 Intelligroup Asia Ltd., Technology Team
Deleting Reference Variables
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1, cnt_2. MOVE cnt_2 TO cnt_3. CLEAR cnt_2.
2 1 CNT_3 CNT_2 CNT_1
Intelligroup Asia Ltd., Technology Team
Garbage Collection
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1, cnt_2.
2
MOVE cnt_2 TO cnt_3. CLEAR cnt_2. cnt_3 = cnt_1.
1
CNT_3 CNT_2 CNT_1 Intelligroup Asia Ltd., Technology Team
Garbage Collection
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1, cnt_2. MOVE cnt_2 TO cnt_3.
2
CLEAR cnt_2. cnt_3 = cnt_1.
1
CLEAR cnt_3. CNT_3 CNT_2 CNT_1 Intelligroup Asia Ltd., Technology Team
New Objects
DATA: cnt_1 TYPE REF TO counter,
3
cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1,
2
cnt_2. MOVE cnt_2 TO cnt_3. CLEAR cnt_2.
1
cnt_3 = cnt_1. CLEAR cnt_3. CREATE OBJECT: cnt_2, cnt_3.
CNT_3 CNT_2 CNT_1
Intelligroup Asia Ltd., Technology Team
Example: Class Identity
CLASS counter DEFINITION. PUBLIC SECTION. METHODS: set IMPORTING VALUE(set_value) TYPE i, increment, get EXPORTING VALUE(get_value) TYPE i. PUBLIC SECTION. DATA count TYPE i. ENDCLASS. CLASS counter IMPLEMENTATION. METHOD set. count = set_value. ENDMETHOD. METHOD increment. count = count + 1. ENDMETHOD. METHOD get. get_value = count. ENDMETHOD. ENDCLASS. Intelligroup Asia Ltd., Technology Team
Identity
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter, cnt_3 TYPE REF TO counter. DATA number TYPE I VALUE 5. CREATE OBJECT: cnt_1, 2
cnt_2. CALL METHOD cnt_1->set EXPORTING set_value = number. CALL METHOD cnt_2->set EXPORTING set_value = number. * Reference Semantics If cnt_1 = cnt_2. * This will not be executed endif Intelligroup Asia Ltd., Technology Team
1
CNT_3 CNT_2 CNT_1
Identity (Contd.)
* Attribute values are the same!!! If cnt_1->count = cnt_2->count. * This will be executed
2
Endif cnt_3 = cnt_1 If cnt_3 = cnt_1.
1
* This will be executed Endif.
CNT_3 CNT_2 CNT_1
Intelligroup Asia Ltd., Technology Team
Methods of Several Objects
DATA: cnt_1 TYPE REF TO counter, cnt_2 TYPE REF TO counter,
3
cnt_3 TYPE REF TO counter. CREATE OBJECT: cnt_1, cnt_2,
2
cnt_3. CALL METHOD cnt_1->set EXPORTING set_value = 1. CALL METHOD cnt_2->set EXPORTING set_value = 10. CALL METHOD cnt_3->set EXPORTING set_value = 100.
1
CNT_3 CNT_2
The value of COUNT is different in each object Intelligroup Asia Ltd., Technology Team
CNT_1
Objects: Summary
Declaring reference variables DATA: ref1 TYPE REF TO class, ref2 TYPE REF TO class.
Creating objects CREATE OBJECT: ref1, ref2.
Accessing attributes and methods x = ref1->attr + ref2->attr. CALL METHOD ref1->method EXPORTING ...
Object References and identities
Intelligroup Asia Ltd., Technology Team