You are on page 1of 76

ABAP Objects

- Introduction ABAP Objects A Workshop A Workshop from the
ABAP Language Group
Horst Keller
SAP AG
© SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 1 © SAP AG

Workshop Goals

z Position of ABAP Objects within the R/3 System z Overview of the syntax of ABAP Objects z Working with existing classes and interfaces z Defining classes and interfaces z Creating objects z Reacting to events z Understanding the polymorphism provided by

interfaces and inheritance

© SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 2

Workshop Goals

This is not a comprehensive course in objectoriented programming
z There is more to object-oriented development than just object-oriented programming. It has logical advantages that are independent of the concrete implementation.
z The most important (and most time-consuming) part of

a real object-oriented application is the object-oriented modeling.

The OO Rollout Group provides another ABAP Objects course

© SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 3

Introduction (Horst Keller) / 4 .Contents z Introduction z From Function Groups to Classes z Classes z Objects z Events z Interfaces z Inheritance z Using Global Classes z Exercises © SAP AG 1999 ABAP Objects .

Introduction z Object orientation z Objects z ABAP Objects © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 5 .

Introduction (Horst Keller) / 6 .Object Orientiation Data Data Data Data Data Attributes Data Abstraction Function Methods Function Function Function Data Method Method Method Function Function Function Function Functions and data Data model as an abstraction of the real world Software objects Object model as an abstraction of the real world Real-world objects © SAP AG 1999 ABAP Objects .

Objects Interface Private components Flight Public attributes Airline Flight number Private access Customer Address Passengerlist Public methods BOOK Public access FLIGHT Public events © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 7 .

ABAP Objects z z ABAP ABAPObjects Objectsis isan anupwards-compatible upwards-compatibleextension extensionof ofthe the existing existingABAP ABAPlanguage language z z You Youcan canuse useexisting existingABAP ABAPstatements statementswithin withinABAP ABAPObjects Objects z z You Youcan canuse useABAP ABAPObjects Objectswithin withinexisting existingprograms programs z z ABAP ABAPObjects Objectsis isfully fullyintegrated integratedin inthe theABAP ABAPDebugger Debugger © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 8 .

From Function Groups to Classes z Instances of function groups as objects z Example: Function group as counter © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 9 .

..... Internal session of an ABAP program External session © SAP AG 1999 ABAP Objects .. Data .Introduction (Horst Keller) / 10 .Instances of Function Groups as Objects Function group 1 Function module … Function group 2 Function module … Data . ABAP program with data CALL FUNCTION .

* Local Interface IMPORTING VALUE(SET_VALUE) COUNT = SET_VALUE. ENDFUNCTION. FUNCTION SET_COUNTER.Function Group as Counter . DATA COUNT TYPE I. ENDFUNCTION. FUNCTION GET_COUNTER. * Local Interface: EXPORTING VALUE(GET_VALUE) GET_VALUE = COUNT. © SAP AG 1999 ABAP Objects . COUNT = COUNT + 1. FUNCTION INCREMENT_COUNTER.Definition FUNCTION-POOL COUNTER. ENDFUNCTION.Introduction (Horst Keller) / 11 .

CALL FUNCTION 'SET_COUNTER' EXPORTING SET_VALUE = NUMBER. CALL FUNCTION 'INCREMENT_COUNTER'. NUMBER has value 8 © SAP AG 1999 ABAP Objects . CALL FUNCTION 'GET_COUNTER' IMPORTING GET_VALUE = NUMBER.Call DATA NUMBER TYPE I VALUE 5. ENDDO. DO 3 TIMES.Function Group as Counter .Introduction (Horst Keller) / 12 .

Data baustein Data Interface . … .. class m 1st instance...Introduction (Horst Keller) / 13 ..... . … .. class 1 Daten SchnittData stelle Data Interface .. … … nth instance. Daten class m Funktions..Classes Generalize Function Groups … nth instance. class 1 1st instance. … ABAP program with data Internal session of an ABAP program External session © SAP AG 1999 ABAP Objects ... .

Introduction (Horst Keller) / 14 .Classes. and Objects z Example: Class as counter z Reference variables z Creating objects z Calling methods z Working with references © SAP AG 1999 ABAP Objects . References.

CLASS counter counter IMPLEMENTATION. count. increment. ENDCLASS.Example: Class as Counter CLASS counter DEFINITION. PRIVATE SECTION. ENDMETHOD. ENDMETHOD. METHODS: set IMPORTING VALUE(set_value) TYPE i. DATA count TYPE i. count count = = count count + + 1. ENDMETHOD. get_value get_value = = count. METHOD set. ENDMETHOD. METHOD METHOD get. IMPLEMENTATION. get. 1. increment. set_value. get EXPORTING VALUE(get_value) TYPE i. METHOD METHOD increment. ENDCLASS. count count = = set_value. PUBLIC SECTION. ENDMETHOD. CLASS ENDCLASS.Introduction (Horst Keller) / 15 . METHOD set. © SAP AG 1999 ABAP Objects . ENDMETHOD.

Introduction (Horst Keller) / 16 . CNT_1 © SAP AG 1999 ABAP Objects .Reference Variables DATA: DATA: cnt_1 cnt_1 TYPE TYPE REF REF TO TO counter. counter.

Introduction (Horst Keller) / 17 . 1<COUNTER> CNT_1 © SAP AG 1999 ABAP Objects . counter. CREATE CREATE OBJECT OBJECT cnt_1 cnt_1 TYPE TYPE counter.Creating an Object DATA: DATA: cnt_1 cnt_1 TYPE TYPE REF REF TO TO counter. counter.

counter. DO DO 3 3 TIMES. CALL CALL METHOD METHOD cnt_1->set cnt_1->set EXPORTING EXPORTING set_value set_value = = number. NUMBER has the value 8 © SAP AG 1999 ABAP Objects . 5. ENDDO. TIMES. CALL CALL METHOD METHOD cnt_1->increment. counter. ENDDO. number. 1<COUNTER> CALL CALL METHOD METHOD cnt_1->get cnt_1->get IMPORTING IMPORTING get_value get_value = = number. number.Calling Methods DATA: DATA: cnt_1 cnt_1 TYPE TYPE REF REF TO TO counter. cnt_1->increment. DATA DATA number number TYPE TYPE I I VALUE VALUE 5.Introduction (Horst Keller) / 18 CNT_1 . CREATE CREATE OBJECT OBJECT cnt_1 cnt_1 TYPE TYPE counter.

counter. counter.Introduction (Horst Keller) / 19 . counter. counter.Several Reference Variables DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. counter. CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects .

cnt_2. 2<COUNTER> 1<COUNTER> CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects . cnt_1. counter. counter.Several Objects DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. counter. counter. cnt_2. counter. CREATE CREATE OBJECT: OBJECT: cnt_1.Introduction (Horst Keller) / 20 .

Introduction (Horst Keller) / 21 . counter. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3.Assigning Reference Variables DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. cnt_1. counter. cnt_3. CREATE CREATE OBJECT: OBJECT: cnt_1. cnt_2. counter. cnt_2. counter. 2<COUNTER> 1<COUNTER> CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects . counter.

counter. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. counter. CLEAR CLEAR cnt_2.Introduction (Horst Keller) / 22 . cnt_2. 2<COUNTER> 1<COUNTER> CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects . cnt_1. cnt_3. counter. cnt_2. CREATE CREATE OBJECT: OBJECT: cnt_1.Deleting Reference Variables DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. counter. cnt_2. counter.

cnt_2. CREATE CREATE OBJECT: OBJECT: cnt_1. cnt_1. cnt_1. cnt_3. counter. cnt_3 cnt_3 = = cnt_1. counter. cnt_2. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. 1<COUNTER> 2<COUNTER> CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects . counter. CLEAR CLEAR cnt_2. counter.Introduction (Horst Keller) / 23 . cnt_2. counter.Garbage Collection DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter.

cnt_2. 1<COUNTER> CLEAR CLEAR cnt_3. counter. counter.Garbage Collection DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. CLEAR CLEAR cnt_2. cnt_2. CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects . cnt_3 cnt_3 = = cnt_1. cnt_2. cnt_1. counter. counter. CREATE CREATE OBJECT: OBJECT: cnt_1. cnt_3. cnt_3. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. counter. cnt_1.Introduction (Horst Keller) / 24 .

cnt_2. counter. 3<COUNTER> CREATE CREATE OBJECT: OBJECT: cnt_1. cnt_2. cnt_2. cnt_2. counter. cnt_1. counter. CNT_3 CNT_2 CNT_1 © SAP AG 1999 ABAP Objects .New Objects DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. 1<COUNTER> CLEAR CLEAR cnt_3. cnt_3. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. cnt_1. cnt_3. CREATE CREATE OBJECT: OBJECT: cnt_2. cnt_3. cnt_3. cnt_3 cnt_3 = = cnt_1. counter. counter. CLEAR CLEAR cnt_2.Introduction (Horst Keller) / 25 2<COUNTER> .

cnt_3. counter. counter. cnt_2. 10. CALL CALL METHOD METHOD cnt_2->set cnt_2->set EXPORTING EXPORTING set_value set_value = = 10.Introduction (Horst Keller) / 26 CNT_1 . 1. 3<COUNTER> CREATE CREATE OBJECT: OBJECT: cnt_1. 2<COUNTER> 1<COUNTER> CALL CALL METHOD METHOD cnt_3->set cnt_3->set EXPORTING EXPORTING set_value set_value = = 100. counter.Methods of Several Objects DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 TYPE TYPE cnt_3 cnt_3 TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO counter. cnt_2. cnt_3. counter. cnt_1. CALL CALL METHOD METHOD cnt_1->set cnt_1->set EXPORTING EXPORTING set_value set_value = = 1. counter. 100. CNT_3 CNT_2 The value of COUNT is different in each object © SAP AG 1999 ABAP Objects .

class.. ref2 ref2 TYPE TYPE REF REF TO TO class.. © SAP AG 1999 ABAP Objects .. ref2->attr.. ref1. CALL CALL METHOD METHOD ref1->method ref1->method EXPORTING EXPORTING . ref2.Introduction (Horst Keller) / 27 . z Creating objects CREATE CREATE OBJECT: OBJECT: ref1.Objects: Summary z Declaring reference variables DATA: DATA: ref1 ref1 TYPE TYPE REF REF TO TO class. z Accessing attributes and methods x x = = ref1->attr ref1->attr + + ref2->attr. . ref2. class.

Classes in Detail z Structure of classes z Components of classes z Accessing the components © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 28 .

Visibility Sections CLASS c1 DEFINITION. METHOD m2. e2. PUBLIC SECTION. ENDCLASS. m1. CLASS c1 IMPLEMENTATION. … Subclasses of c1 All users © SAP AG 1999 ABAP Objects .Structure of Classes . DATA: a1 … METHODS: m1 … EVENTS: e1 … PROTECTED SECTION. DATA: a2 … METHODS: m2 … EVENTS: e2 … PRIVATE SECTION. … ENDMETHOD. METHOD m3. … ENDMETHOD. m2. e3 Method implementations … Protected components a2. DATA: a3 … METHODS: m3 … EVENTS: e3 … ENDCLASS. … ENDMETHOD. m3. METHOD m1. Class c1 Public components a1. e1 Private components a3.Introduction (Horst Keller) / 29 .

TYPE TYPE CONSTANTS CONSTANTS ......... .... TYPE TYPE . ... ... DEFINITION.... .. SECTION..... DEFINITION.Introduction (Horst Keller) / 30 .. .... . DATA . .. ... . ... . .... .. ENDCLASS.. . . SECTION. z DATA: Instance attributes z CLASS-DATA: Static attributes z CONSTANTS: Constants © SAP AG 1999 ABAP Objects .. TYPE TYPE CLASS-DATA CLASS-DATA ..Components of Classes: Attributes CLASS CLASS . . [READ-ONLY] [READ-ONLY] [READ-ONLY] [READ-ONLY] VALUE VALUE . ...... ... ..... ENDCLASS... DATA .

.Introduction (Horst Keller) / 31 . 'Instance'. . 'Static'. cref->a2. . CREF © SAP AG 1999 ABAP Objects . c=>a1.Static Attributes and Instance Attributes CLASS CLASS c c DEFINITION.... ... 1<CLASS> a2 . c. DATA a2(10) DATA a2(10) TYPE TYPE C C VALUE VALUE 'Instance'. SECTION. DATA: DATA: cref cref TYPE TYPE REF REF TO TO c. WRITE WRITE cref->a2. ENDCLASS. ENDCLASS. CLASS a1 . CLASS-DATA CLASS-DATA a1(10) a1(10) TYPE TYPE C C VALUE VALUE 'Static'.. . DEFINITION. PUBLIC PUBLIC SECTION.. WRITE WRITE c=>a1.. c.... CREATE CREATE OBJECT OBJECT cref cref TYPE TYPE c.

) VALUE(. .. SECTION. . .... ENDCLASS.... ...... . .. [OPTIONAL] [OPTIONAL] ........ .Introduction (Horst Keller) / 32 . SECTION.. .... ENDCLASS. VALUE(........ CLASS CLASS . METHODS METHODS .. TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE . .. ENDCLASS... . ENDMETHOD. ENDMETHOD. IMPLEMENTATION. . [VALUE] [VALUE] .. DEFINITION..... . .. [VALUE] [VALUE] . METHOD METHOD ...... .Components of Classes: Methods CLASS CLASS ... .. IMPORTING IMPORTING EXPORTING EXPORTING CHANGING CHANGING RETURNING RETURNING EXCEPTIONS EXCEPTIONS CLASS-METHODS CLASS-METHODS . ... .. ... IMPLEMENTATION. .... z METHODS: Instance methods z CLASS-METHODS: Static methods © SAP AG 1999 ABAP Objects ..) . ... [OPTIONAL] [OPTIONAL] . ENDCLASS..... . ... DEFINITION.... [VALUE] [VALUE] ....

Introduction (Horst Keller) / 33 . ENDCLASS. CLASS CLASS c c IMPLEMENTATION... METHOD METHOD CONSTRUCTOR.Constructors CLASS CLASS c c DEFINITION. . c.. . PROGRAM PROGRAM … … .. . METHODS METHODS CONSTRUCTOR CONSTRUCTOR [IMPORTING [IMPORTING arg1 arg1 TYPE TYPE type type .. ENDCLASS.. CLASS_CONSTRUCTOR. EXPORTING EXPORTING arg1 arg1 = = v1 v1 .. ENDCLASS.... ENDMETHOD. CLASS_CONSTRUCTOR. CONSTRUCTOR. . METHOD METHOD CLASS_CONSTRUCTOR. ENDMETHOD. IMPLEMENTATION. DATA DATA o1 o1 TYPE TYPE REF REF CREATE CREATE OBJECT OBJECT o1 o1 TO TO c. © SAP AG 1999 ABAP Objects . .. SECTION.. ].. . ENDMETHOD.. CLASS-METHODS CLASS-METHODS CLASS_CONSTRUCTOR. PUBLIC PUBLIC SECTION. ]. ENDMETHOD. .. DEFINITION. ENDCLASS..

ME © SAP AG 1999 ABAP Objects .Accessing the components of classes z Instance components „ Instance attribute „ Instance method: z Static components „ Static attribute: „ Static method: ref–>comp ref->attr call method ref->meth class=>comp class=>attr call method class=>meth n<class> z Special references in methods „ Self reference: „ Pseudo reference ME->comp SUPER->comp ..Introduction (Horst Keller) / 34 ..

Inheritance z Introduction z Overview z Single inheritance z Redefining methods z Example: Subclass of superclass counter © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 35 .

Inheritance: Introduction z Definition of a class by inheriting the components from a superclass (Reuse) z Specialization by adding own components and redefining methods in subclasses z Polymorphism by accessing subclass objects n<class3> class1 CREF1 class2 CREF2 class3 CREF3 © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 36 .

Inheritance - Overview
Class OBJECT ...
CLASS c1 DEFINITION INHERITING FROM ... ... ENDCLASS. CLASS c1 IMPLEMENTATION. ... ENDCLASS.

Class c1

Class c2
CLASS c2 DEFINITION INHERITING FROM c1. ... ENDCLASS. CLASS c2 IMPLEMENTATION. ... ENDCLASS.

Class ... ...
© SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 37

Single Inheritance
OBJECT

C1

...

...
C2

...
© SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 38

Redefining Methods

CLASS CLASS ... ... DEFINITION DEFINITION INHERITING INHERITING FROM FROM ... ... ... ... SECTION. SECTION. METHODS METHODS ... ... REDEFINITON REDEFINITON ... ... ... ... ... ... ENDCLASS. ENDCLASS.

CLASS CLASS ... ... IMPLEMENTATION. IMPLEMENTATION. METHOD METHOD ... ... ... ... ENDMETHOD. ENDMETHOD. ENDCLASS. ENDCLASS.

Semantic rules
z Subclasses must behave just like their superclass for all users of

inherited components
z A redefined method must observe the original semantics z Inheritance should only be used to specialize
© SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 39

SECTION..... . . superclass.. ENDCLASS. DATA DATA o1 o1 TYPE TYPE REF REF CREATE CREATE OBJECT OBJECT o1 o1 TO TO subclass. ENDCLASS. IMPLEMENTATION... attributes only CALL CALL METHOD METHOD SUPER->CONSTRUCTOR SUPER->CONSTRUCTOR EXPORTING EXPORTING .. CONSTRUCTOR. ..... . Access to static . ENDMETHOD..... subclass. PUBLIC PUBLIC SECTION. CLASS CLASS subclass subclass IMPLEMENTATION.. TYPE TYPE subclass subclass EXPORTING EXPORTING . ENDMETHOD. . attributes also ENDCLASS. .. © SAP AG 1999 ABAP Objects .... ..Introduction (Horst Keller) / 40 . METHOD METHOD CONSTRUCTOR.... PROGRAM PROGRAM .. ENDCLASS... Accress to instance .Inheritance and Constructors CLASS CLASS subclass subclass DEFINITION DEFINITION INHERITING INHERITING FROM FROM superclass. . . METHODS METHODS CONSTRUCTOR CONSTRUCTOR IMPORTING IMPORTING .... ..

count->increment. ENDDO. ENDCLASS. super->increment. PUBLIC SECTION. DO DO 10 10 TIMES. DATA: DATA: count count TYPE TYPE REF REF TO TO counter. count_ten DATA count_ten = = count_ten count_ten + + 1. PUBLIC SECTION. DATA DATA count_ten. modulo = count *Replace PRIVATE with: modulo = count mod mod 10. CREATE CREATE OBJECT OBJECT count count TYPE TYPE counter_ten. PROTECTED IF modulo = 0. ENDCLASS. DATA count count TYPE TYPE I. counter_ten. SECTION. PROTECTED SECTION. © SAP AG 1999 ABAP Objects . ENDIF. ENDDO. ENDMETHOD. I. number. I. METHOD increment.Introduction (Horst Keller) / 41 .Subclass for Superclass Counter* CLASS CLASS counter_ten counter_ten DEFINITION DEFINITION INHERITING INHERITING FROM FROM counter. DATA DATA modulo modulo TYPE TYPE I. ENDCLASS. counter. IF modulo = 0. 1. IMPLEMENTATION. TIMES. 10. CLASS CLASS counter_ten counter_ten IMPLEMENTATION. ENDIF. CALL CALL METHOD METHOD super->increment. REDEFINITION. CALL CALL METHOD METHOD count->set count->set EXPORTING EXPORTING set_value set_value = = number. ENDMETHOD. counter. METHOD increment. count_ten. CALL CALL METHOD METHOD count->increment. METHODS METHODS increment increment REDEFINITION. ENDCLASS.

Interfaces z Introduction z Overview z Definition z Implementation z Interface references z Example: Interface for counter © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 42 .

Introduction (Horst Keller) / 43 .Interfaces: Introduction z Definition of an interface without implementation z Classes can implement several interfaces z Uniform access with interface references z Polymorphism independent from inheritance n<class3> iref_tab Interface n<class2> iref_line iref_line iref_line n<class1> © SAP AG 1999 ABAP Objects .

PROTECTED SECTION. e3.Introduction (Horst Keller) / 44 Protected components a3.. CLASS c1 IMPLEMENTATION. m2. i1~m1.. m3.. DATA: a1 .Interfaces . METHODS: m1 . … ENDMETHOD.. METHOD i1~m1.. © SAP AG 1999 ABAP Objects . ENDINTERFACE.. Private components a2. PRIVATE SECTION. PUBLIC SECTION.. e2 Method implementations CLASS c1 DEFINITION. EVENTS: e1 . ENDCLASS. DATA a1 .... . class c1 Public components a1.. … Subclasses of c1 All users .... ENDCLASS. INTERFACES i1 . i1~a1..Overview INTERFACE i1.

ENDINTERFACE.) TYPE TYPE .. IMPORTING IMPORTING EXPORTING EXPORTING CHANGING CHANGING RETURNING RETURNING EXCEPTIONS EXCEPTIONS CLASS-METHODS: CLASS-METHODS: . ...) VALUE(... . [VALUE [VALUE ... . .Definition INTERFACE INTERFACE . [OPTIONAL] [OPTIONAL] . [OPTIONAL]]. CLASS-EVENTS:.. .. METHODS: METHODS: ...... ......... . .....) ....] . [VALUE] [VALUE] .... ....... CLASS-EVENTS:.. ..... [EXPORTING [EXPORTING VALUE(...] TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE ... ..... ... DATA: DATA: CLASS-DATA: CLASS-DATA: CONSTANTS: CONSTANTS: ..Interfaces .... . [READ-ONLY] [READ-ONLY] .) VALUE(. INTERFACES: INTERFACES: . . EVENTS: EVENTS: . .. .. .... [VALUE] [VALUE] ........ [READ-ONLY] [READ-ONLY] .... ........ [OPTIONAL]]. ..Introduction (Horst Keller) / 45 .. ENDINTERFACE... [VALUE] [VALUE] .. . .. ...... ... TYPE TYPE TYPE TYPE TYPE TYPE .. .. . . .. . VALUE(.. © SAP AG 1999 ABAP Objects .. ... ...... .. [OPTIONAL] [OPTIONAL] ....

ENDMETHOD. .. ENDCLASS. © SAP AG 1999 ABAP Objects .. ENDCLASS.. ENDCLASS. IMPLEMENTATION... .. .... ENDMETHOD.~. ....~.. DEFINITION. .... METHOD METHOD . .. CLASS CLASS ... . INTERFACES: INTERFACES: .. ENDCLASS.. .... IMPLEMENTATION.. DEFINITION.Interfaces ... ...Introduction (Horst Keller) / 46 .. .Implementation CLASS CLASS .. PUBLIC PUBLIC SECTION.. SECTION.

. i1. . INTERFACES INTERFACES i1. ENDCLASS. SECTION. © SAP AG 1999 ABAP Objects .. ENDINTERFACE. i1.. DEFINITION. 1<C2> 1<C1> CNT_C CREATE CREATE OBJECT: OBJECT: cnt_c cnt_c TYPE TYPE c1. a1. CNT_2 MOVE MOVE cnt_c cnt_c to to cnt_2. PUBLIC PUBLIC SECTION. ENDCLASS.. ENDCLASS. cnt_1 cnt_1 TYPE TYPE c2. ENDINTERFACE. DATA DATA a1. CLASS CLASS c1 c1 DEFINITION. i1.Interface References INTERFACE INTERFACE i1. cnt_1. PUBLIC PUBLIC SECTION.Introduction (Horst Keller) / 47 . DEFINITION.. SECTION. REF REF TO TO i1. i1. CNT_1 DATA DATA cnt_c cnt_c TYPE TYPE DATA: DATA: cnt_1 cnt_1 TYPE TYPE cnt_2 cnt_2 LIKE LIKE REF REF TO TO c1. CLASS CLASS c2 c2 DEFINITION. c1. c1. INTERFACES INTERFACES i1. cnt_1. c2. ENDCLASS. cnt_2.

increment. i. METHODS drive. ENDMETHOD. SECTION. DATA DATA speed speed TYPE TYPE i. ENDMETHOD. is'. INTERFACES INTERFACES status. PRIVATE PRIVATE SECTION. METHODS increment. ENDMETHOD. status~write. count. CLASS CLASS bicycle bicycle IMPLEMENTATION. ENDCLASS. CLASS CLASS bicycle bicycle DEFINITION. IMPLEMENTATION. speed. CLASS CLASS counter counter IMPLEMENTATION. METHOD status~write. CLASS CLASS counter counter DEFINITION. ENDMETHOD. METHOD status~write. PUBLIC SECTION. METHODS increment. DATA DATA count count TYPE TYPE i. © SAP AG 1999 ABAP Objects . status. is'. PUBLIC SECTION. DEFINITION.Implementation INTERFACE INTERFACE status. ENDCLASS. count. ENDINTERFACE. status. drive. PUBLIC SECTION. WRITE: 'Speed WRITE: 'Speed of of bicycle bicycle is'. IMPLEMENTATION. ENDCLASS. ENDCLASS. ENDCLASS. ENDMETHOD. ENDCLASS. METHODS write. METHODS write. ENDMETHOD. ENDMETHOD. METHOD METHOD increment. ENDCLASS. count count = = count count + + 1.Introduction (Horst Keller) / 48 . INTERFACES INTERFACES status. 10. status. speed speed = = speed speed + + 10. ENDMETHOD. speed. ENDCLASS.Interface for Object State . METHODS drive. i. DEFINITION. METHOD METHOD status~write. PUBLIC SECTION. SECTION. METHOD METHOD drive. 1. WRITE: WRITE: 'Count 'Count in in counter counter is'. ENDINTERFACE. PRIVATE PRIVATE SECTION.

count. bike. CALL METHOD CALL METHOD status->write. counter. status. bike bike TYPE TYPE REF REF TO TO bicycle. Counter status Bike speed © SAP AG 1999 ABAP Objects . bike->drive. status status = = bike. CREATE CREATE OBJECT: OBJECT: count. DO DO 5 5 TIMES. ENDDO. bicycle.Use DATA: DATA: count count TYPE TYPE REF REF TO TO counter. bike->drive.Interface for Object State .Introduction (Horst Keller) / 49 . count. CALL METHOD CALL METHOD status->write. status status = = count. status->write. status status TYPE TYPE REF REF TO TO status. bike. ENDDO. count->increment. TIMES. status->write. bike. CALL CALL METHOD: METHOD: count->increment.

Introduction (Horst Keller) / 50 .Events z Introduction z Overview z Declaring and triggering events z Event handler methods z Handling events z Example: Overflow in counter © SAP AG 1999 ABAP Objects .

Introduction (Horst Keller) / 51 .Introduction z Events are components of classes z Methods can raise the events of their class z Handler methods can be triggered by events 2<HANDLER> 1<TRIGGER> 1<HANDLER> © SAP AG 1999 ABAP Objects .Events .

ENDMETHOD. RAISE EVENT e1 EXPORTING p1 = a1. METHOD m1.Introduction (Horst Keller) / 52 Event handler . METHOD m2. ENDMETHOD. CLASS c2 DEFINITION. a1 = . CLASS c1 IMPLEMENTATION. EVENTS e1 EXPORTING VALUE(p1) TYPE i. DATA a2 TYPE i. PUBLIC SECTION.. CLASS C2 IMPLEMENTATION. METHODS m2 FOR EVENT e1 OF c1 IMPORTING p1. ENDCLASS.Events . ENDCLASS. PUBLIC SECTION. ENDCLASS..Overview CLASS c1 DEFINITION. METHODS m1. . PRIVATE SECTION. ENDCLASS.. a2 = p1. DATA a1 TYPE i. Event trigger © SAP AG 1999 ABAP Objects .. PRIVATE SECTION.

= = .... .... ENDCLASS..) VALUE(.Declaring and Triggering Events CLASS CLASS .. ..... ... RAISE RAISE EVENT EVENT ... ... ... ENDCLASS. © SAP AG 1999 ABAP Objects .. ENDCLASS.. .. IMPLEMENTATION. EXPORTING EXPORTING . EVENTS EVENTS .... [OPTIONAL]]. . .. ENDCLASS... .) TYPE TYPE . .. ..... CLASS-EVENTS CLASS-EVENTS . METHODS METHODS . DEFINITION. SECTION..... DEFINITION. SECTION... .. . [EXPORTING [EXPORTING VALUE(. ... IMPLEMENTATION... ENDMETHOD..Introduction (Horst Keller) / 53 . ENDMETHOD. METHOD METHOD .. ..... .. CLASS CLASS . [OPTIONAL]].....

SENDER SENDER . . ENDMETHOD.Event Handler Methods CLASS CLASS . FOR FOR EVENT EVENT .. © SAP AG 1999 ABAP Objects ..... ENDCLASS.. . SECTION... . ........ . ENDCLASS. IMPLEMENTATION. ...... METHODS METHODS .. .... METHOD METHOD . . ENDCLASS. DEFINITION... CLASS CLASS . OF OF .. ]. IMPLEMENTATION.. . DEFINITION. ENDCLASS......... ].. SECTION.Introduction (Horst Keller) / 54 .. .. . . ENDMETHOD. [IMPORTING [IMPORTING ..

handler_1. CALL CALL METHOD METHOD trigger->raise_event. . trigger. CREATE CREATE OBJECT: OBJECT: trigger. DATA: DATA: trigger trigger handler_1 handler_1 handler_2 handler_2 TYPE TYPE TYPE TYPE TYPE TYPE REF REF REF REF REF REF TO TO TO TO TO TO trigger.. handler_1. trigger->raise_event. trigger. © SAP AG 1999 ABAP Objects . handler.Event Handling ..Introduction (Horst Keller) / 55 . trigger. handler. SET SET HANDLER HANDLER handler_1->handle_event handler_1->handle_event handler_2->handle_event handler_2->handle_event FOR FOR trigger. handler_2.. handler.Registration PROGRAM PROGRAM . handler.. handler_2.

Handling Events . 1<TRIGGER> HANDLER_1 HANDLER_2 TRIGGER © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 56 .References Ereignis Methode Methode 2<HANDLER> 1<HANDLER> ….

DEFINITION. threshold TYPE i threshold TYPE i VALUE VALUE 10. ENDCLASS . ENDCLASS © SAP AG 1999 ABAP Objects .Trigger CLASS CLASS counter counter DEFINITION. 1. ENDIF. METHODS METHODS increment. ENDCLASS. ENDCLASS. RAISE EVENT critical_value RAISE EVENT critical_value EXPORTING EXPORTING excess excess = = diff. i. DATA DATA diff diff TYPE TYPE i. diff = count diff = count . METHOD increment.threshold.. i.Introduction (Horst Keller) / 57 . EVENTS critical_value EVENTS critical_value EXPORTING EXPORTING value(excess) value(excess) TYPE TYPE i. count count = = count count + + 1.Threshold in Counter . i. ENDMETHOD. DATA: TYPE DATA: count count TYPE i. PUBLIC PUBLIC SECTION. METHOD increment. increment. 10. CLASS CLASS counter counter IMPLEMENTATION. IMPLEMENTATION. ENDIF. ENDMETHOD. SECTION. PRIVATE SECTION. threshold. IF IF count count > > threshold. PRIVATE SECTION. threshold. diff.

PUBLIC SECTION. handle_excess. ENDDO. counter. is'. METHOD METHOD handle_excess. TIMES. excess. ENDCLASS. ENDCLASS. cnt. DEFINITION. METHODS METHODS handle_excess handle_excess FOR FOR EVENT EVENT critical_value critical_value OF OF counter counter IMPORTING IMPORTING excess. ENDDO. react TYPE REF TO handler. DO DO 20 20 TIMES. INSTANCES. cnt->increment. CALL CALL METHOD METHOD cnt->increment.Handling CLASS CLASS handler handler DEFINITION. ENDMETHOD. CLASS CLASS handler handler IMPLEMENTATION.Threshold in Counter .Introduction (Horst Keller) / 58 . react. SET SET HANDLER HANDLER react->handle_excess react->handle_excess FOR FOR ALL ALL INSTANCES. ENDMETHOD. © SAP AG 1999 ABAP Objects . react TYPE REF TO handler. excess. ENDCLASS. PUBLIC SECTION. excess. DATA: DATA: cnt cnt TYPE TYPE REF REF TO TO counter. react. WRITE: WRITE: / / ’Excess ’Excess is'. CREATE CREATE OBJECT: OBJECT: cnt. ENDCLASS. IMPLEMENTATION.

Using Global Classes z Class pools z Class Browser and Class Builder z Class Builder z Example: Using CL_GUI_PICTURE © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 59 .

... ENDCLASS.. ENDCLASS. ENDINTERFACE.. TYPES TYPES … … CLASS CLASS … … ... ...... ENDCLASS...Class Pools CLASS-POOL . . ENDINTERFACE. CLASS CLASS … … IMPLEMENTATION. ENDCLASS.. © SAP AG 1999 ABAP Objects . ENDCLASS. INTERFACE INTERFACE … … . PUBLIC.Introduction (Horst Keller) / 60 .. ENDCLASS... . Visibility CLASS CLASS … … DEFINITION DEFINITION PUBLIC. . . IMPLEMENTATION. .

Class Library and Class Browser © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 61 .

Introduction (Horst Keller) / 62 .Class Builder © SAP AG 1999 ABAP Objects .

container2 LIKE container1.. . SET SET HANDLER HANDLER react->on_single_click react->on_single_click FOR FOR pict1. cl_gui_custom_container. ENDCLASS. DATA: DATA: container1 container1 TYPE TYPE REF REF TO TO cl_gui_custom_container..Introduction (Horst Keller) / 63 ENDCLASS. container2 EXPORTING container_name = 'PICTURE2'.. CALL CALL METHOD METHOD pict2->load_picture_from_url pict2->load_picture_from_url . reaction. PUBLIC PUBLIC SECTION. pict1 TYPE pict1 TYPE REF REF TO TO cl_gui_picture. 'PICTURE1'. pict2 LIKE pict1. container2 LIKE container1. METHOD on_single_click.. ENDCLASS.Using a Global Class CLASS CLASS reaction reaction DEFINITION. CREATE CREATE OBJECT: OBJECT: container1 container1 EXPORTING EXPORTING container_name container_name = = 'PICTURE1'.. ENDMETHOD.. © SAP AG 1999 ABAP Objects . pict1 EXPORTING pict1 EXPORTING parent parent = = container1.. pict1. ENDMETHOD.. pict2 EXPORTING parent = container2. METHOD on_single_click. react TYPE react TYPE REF REF TO TO reaction. . pict2 LIKE pict1. cl_gui_picture. pict2 EXPORTING parent = container2. METHODS METHODS on_single_click on_single_click FOR FOR EVENT EVENT picture_click picture_click OF OF cl_gui_picture. CLASS CLASS reaction reaction IMPLEMENTATION. SECTION. cl_gui_picture. react.. . react. IMPLEMENTATION... container2 EXPORTING container_name = 'PICTURE2'.... DEFINITION. ENDCLASS. CALL CALL METHOD METHOD pict1->load_picture_from_url pict1->load_picture_from_url . container1.

Example of CL_GUI_PICTURE © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 64 .

Mouse Click © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 65 .

Introduction (Horst Keller) / 66 .After Mouse Click © SAP AG 1999 ABAP Objects .

Further Information z Transaction ABAPDOCU z Keyword documentation © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 67 .

Further Information Transaction ABAPDOCU © SAP AG 1999 ABAP Objects .Introduction (Horst Keller) / 68 .

Introduction (Horst Keller) / 69 .Further Information Keyword Documentation © SAP AG 1999 ABAP Objects .

Creating objects 3. Deriving classes using inheritance 4. Triggering and handling events © SAP AG 1999 ABAP Objects .Exercises 1. Using interfaces 5. Defining classes 2.Introduction (Horst Keller) / 70 .

and an event © SAP AG 1999 ABAP Objects .Scenario Interface Status With methods for displaying attributes Class "Vehicle" with attribute for speed. and a method for changing it Class "Helicopter" Can handle events from ship Class "Truck" with its own maximum speed and attribute output Class "Ship" with its own maximum speed and attribute output.Exercises . a name.Introduction (Horst Keller) / 71 .

but not allow it to exceed the maximum speed. z Define a local class VEHICLE in the designated area in front of the predefined class MAIN. and the public methods SPEED_UP.Exercise 1 . and WRITE. z STOP should reset the speed to zero. z WRITE should display a list line containing the speed and the maximum speed. The method should increase the speed by STEP. z SPEED_UP should have an IMPORTING parameter STEP.Classes z Copy the template ABAP_OBJECTS_ENJOY_0 into an own program.Introduction (Horst Keller) / 72 . z Class VEHICLE should have the protected instance attributes SPEED and MAX_SPEED for its speed and maximum speed. © SAP AG 1999 ABAP Objects . STOP.

call the methods SPEED_UP and WRITE once for each entry in the internal table.Objects z Continuing the program from exercise 1.Introduction (Horst Keller) / 73 . pass the value SY-TABIX * 10 to the parameter STEP. © SAP AG 1999 ABAP Objects . and an internal table VEHICLE_TAB. When you call SPEED_UP. z In a DO loop.Exercise 2 . whose line type is also a reference variable to this class. create objects from the class VEHICLE. create a number of instances of the class VEHICLE and insert them into the internal table. z Define a reference variable VEHICLE with type VEHICLE. z In a LOOP construction. z Program the respective coding in the predefined method START of class MAIN.

z The class SHIP should have an IMPORTING parameter NAME. z Declare extra reference variables TRUCK and SHIP for the new classes. a new public attribute NAME. z The class TRUCK should have an instance constructor and redefine the method WRITE. z The WRITE method should show the class from which the display comes. and WRITE using a superclass reference. © SAP AG 1999 ABAP Objects .Exercise 3 .Inheritance z Change your program from exercise 2 to define classes TRUCK and SHIP as direct subclasses of VEHICLE. Instead. create one instance of each of your new classes and place the corresponding reference into VEHICLE_TAB. z You can delete the code that creates objects for VEHICLE. The instance constructor of SHIP should set the attribute NAME to the actual parameter that you imported. z The instance constructor of each class should change the maximum speed. For SHIP. z Call the method SPEED_UP for both classes using the correct subclass reference.Introduction (Horst Keller) / 74 . use the NAME attribute. and should also redefine the method WRITE.

z The class VEHICLE should implement the interface instead of its own WRITE method.Introduction (Horst Keller) / 75 . z Create a new class HELICOPTER that also implements STATUS. © SAP AG 1999 ABAP Objects . z Replace the reference variables VEHICLE and the table VEHICLE_TAB with the interface reference STATUS and internal table STATUS_TAB.Interfaces z Change your program from exercise 3 so that the method WRITE is contained in an interface STATUS. The name must be changed accordingly in each implementation (including the subclasses). Declare an alias WRITE in that class for the interface method. and insert the reference variable into the table STATUS_TAB.Exercise 4 . z The interface method in HELICOPTER should display a different line from that displayed in VEHICLE. create a corresponding object. but the function remains the same. z Declare a reference variable HELI for the class HELICOPTER. z Use these references to display the status of the objects.

Exercise 5 . z Register the handler method of the object to which HELI is pointing as a handler for objects from the class SHIP. © SAP AG 1999 ABAP Objects . z Add a new method RECEIVE to the class HELICOPTER to allow it to handle the event DAMAGED.Introduction (Horst Keller) / 76 . z When the maximum speed is exceeded. z Increase the speed of the object from the class SHIP until it exceeds the maximum speed and triggers the event. z Redefine the method SPEED_UP in the class SHIP. Import the implicit parameter SENDER. SPEED_UP should set the maximum speed to zero and trigger the event DAMAGED.Events z Change your program from exercise 4 to include an event DAMAGED for the class SHIP. z RECEIVE should use the SENDER parameter to display the name of the ship that triggered the event.