Вы находитесь на странице: 1из 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

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 (Horst Keller) / 4

Introduction

z Object orientation z Objects z ABAP Objects

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

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 - Introduction (Horst Keller) / 6

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

Instances of Function Groups as Objects

Function group 1 Function module

Function group 2 Function module

Data
...

Data
...

ABAP program with data CALL FUNCTION ...


Internal session of an ABAP program External session

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

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.

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

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

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

Classes Generalize Function Groups

nth instance, class 1 1st instance, class 1 Daten SchnittData stelle Data Interface
... ... ...

nth instance, class m 1st instance, Daten class m Funktions- Data baustein Data Interface
... ... ...

ABAP program with data


Internal session of an ABAP program External session

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

Classes, References, 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 - Introduction (Horst Keller) / 14

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. CLASS ENDCLASS. CLASS counter counter IMPLEMENTATION. IMPLEMENTATION. METHOD set. METHOD set. count count = = set_value. set_value. ENDMETHOD. ENDMETHOD. METHOD METHOD increment. increment. count count = = count count + + 1. 1. ENDMETHOD. ENDMETHOD. METHOD METHOD get. get. get_value get_value = = count. count. ENDMETHOD. ENDMETHOD. ENDCLASS. ENDCLASS.
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 15

Reference Variables

DATA: DATA: cnt_1 cnt_1 TYPE TYPE REF REF TO TO counter. counter.

CNT_1
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 16

Creating an Object

DATA: DATA: cnt_1 cnt_1 TYPE TYPE REF REF TO TO counter. counter.

CREATE CREATE OBJECT OBJECT cnt_1 cnt_1 TYPE TYPE counter. counter.

1<COUNTER>

CNT_1
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 17

Calling Methods

DATA: DATA: cnt_1 cnt_1 TYPE TYPE REF REF TO TO counter. counter. DATA DATA number number TYPE TYPE I I VALUE VALUE 5. 5. CREATE CREATE OBJECT OBJECT cnt_1 cnt_1 TYPE TYPE counter. counter. CALL CALL METHOD METHOD cnt_1->set cnt_1->set EXPORTING EXPORTING set_value set_value = = number. number. DO DO 3 3 TIMES. TIMES. CALL CALL METHOD METHOD cnt_1->increment. cnt_1->increment. ENDDO. ENDDO. 1<COUNTER> CALL CALL METHOD METHOD cnt_1->get cnt_1->get IMPORTING IMPORTING get_value get_value = = number. number.

NUMBER has the value 8


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

CNT_1

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, counter, counter, counter. counter.

CNT_3 CNT_2 CNT_1


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

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, counter, counter. counter.

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2. cnt_2. 2<COUNTER>

1<COUNTER>

CNT_3 CNT_2 CNT_1


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

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, counter, counter, counter, counter. counter.

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2. cnt_2. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. cnt_3. 2<COUNTER>

1<COUNTER>

CNT_3 CNT_2 CNT_1


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

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, counter, counter, counter. counter.

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2. cnt_2. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. cnt_3. CLEAR CLEAR cnt_2. cnt_2. 2<COUNTER>

1<COUNTER>

CNT_3 CNT_2 CNT_1


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

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, counter, counter, counter, counter. counter.

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2. cnt_2. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. cnt_3. CLEAR CLEAR cnt_2. cnt_2. cnt_3 cnt_3 = = cnt_1. cnt_1. 1<COUNTER> 2<COUNTER>

CNT_3 CNT_2 CNT_1


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

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, counter, counter, counter, counter. counter.

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2. cnt_2. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. cnt_3. CLEAR CLEAR cnt_2. cnt_2. cnt_3 cnt_3 = = cnt_1. cnt_1. 1<COUNTER> CLEAR CLEAR cnt_3. cnt_3. CNT_3 CNT_2 CNT_1
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 24

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, counter, counter, counter, counter. counter. 3<COUNTER>

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2. cnt_2. MOVE MOVE cnt_2 cnt_2 TO TO cnt_3. cnt_3. CLEAR CLEAR cnt_2. cnt_2. cnt_3 cnt_3 = = cnt_1. cnt_1. 1<COUNTER> CLEAR CLEAR cnt_3. cnt_3. CREATE CREATE OBJECT: OBJECT: cnt_2, cnt_2, cnt_3. cnt_3. CNT_3 CNT_2 CNT_1
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 25

2<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, counter, counter, counter, counter. counter. 3<COUNTER>

CREATE CREATE OBJECT: OBJECT: cnt_1, cnt_1, cnt_2, cnt_2, cnt_3. cnt_3. CALL CALL METHOD METHOD cnt_1->set cnt_1->set EXPORTING EXPORTING set_value set_value = = 1. 1. CALL CALL METHOD METHOD cnt_2->set cnt_2->set EXPORTING EXPORTING set_value set_value = = 10. 10.

2<COUNTER>

1<COUNTER> CALL CALL METHOD METHOD cnt_3->set cnt_3->set EXPORTING EXPORTING set_value set_value = = 100. 100. CNT_3 CNT_2

The value of COUNT is different in each object


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

CNT_1

Objects: Summary

z Declaring reference variables


DATA: DATA: ref1 ref1 TYPE TYPE REF REF TO TO class, class, ref2 ref2 TYPE TYPE REF REF TO TO class. class.

z Creating objects
CREATE CREATE OBJECT: OBJECT: ref1, ref1, ref2. ref2.

z Accessing attributes and methods

x x = = ref1->attr ref1->attr + + ref2->attr. ref2->attr. CALL CALL METHOD METHOD ref1->method ref1->method EXPORTING EXPORTING ... ...

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

Classes in Detail

z Structure of classes z Components of classes z Accessing the components

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

Structure of Classes - Visibility Sections

CLASS c1 DEFINITION. PUBLIC SECTION. DATA: a1 METHODS: m1 EVENTS: e1 PROTECTED SECTION. DATA: a2 METHODS: m2 EVENTS: e2 PRIVATE SECTION. DATA: a3 METHODS: m3 EVENTS: e3 ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD m1. ENDMETHOD. METHOD m2. ENDMETHOD. METHOD m3. ENDMETHOD. ENDCLASS.

Class c1 Public components


a1, m1, e1

Private components a3,


m3, e3 Method implementations

Protected components
a2, m2, e2,

Subclasses of c1 All users

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

Components of Classes: Attributes

CLASS CLASS ... ... DEFINITION. DEFINITION. ... ... ... ... SECTION. SECTION. DATA ... DATA ... TYPE TYPE CLASS-DATA CLASS-DATA ... ... TYPE TYPE CONSTANTS CONSTANTS ... ... TYPE TYPE ... ... ENDCLASS. ENDCLASS.

... ... ... ... ... ...

[READ-ONLY] [READ-ONLY] [READ-ONLY] [READ-ONLY] VALUE VALUE ... ...

... ... ... ...

z DATA:

Instance attributes

z CLASS-DATA: Static attributes z CONSTANTS: Constants

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

Static Attributes and Instance Attributes

CLASS CLASS c c DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. ... ... CLASS-DATA CLASS-DATA a1(10) a1(10) TYPE TYPE C C VALUE VALUE 'Static'. 'Static'. DATA a2(10) DATA a2(10) TYPE TYPE C C VALUE VALUE 'Instance'. 'Instance'. ... ... ENDCLASS. ENDCLASS. DATA: DATA: cref cref TYPE TYPE REF REF TO TO c. c.

CLASS a1

...

1<CLASS> a2

...
WRITE WRITE c=>a1. c=>a1. CREATE CREATE OBJECT OBJECT cref cref TYPE TYPE c. c. WRITE WRITE cref->a2. cref->a2. CREF

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

Components of Classes: Methods


CLASS CLASS ... ... DEFINITION. DEFINITION. ... ... ... ... SECTION. SECTION. METHODS METHODS ... ... IMPORTING IMPORTING EXPORTING EXPORTING CHANGING CHANGING RETURNING RETURNING EXCEPTIONS EXCEPTIONS CLASS-METHODS CLASS-METHODS ... ... ... ... ENDCLASS. ENDCLASS. CLASS CLASS ... ... IMPLEMENTATION. IMPLEMENTATION. METHOD METHOD ... ... ... ... ENDMETHOD. ENDMETHOD. ENDCLASS. ENDCLASS.

[VALUE] [VALUE] ... ... [VALUE] [VALUE] ... ... [VALUE] [VALUE] ... ... VALUE(...) VALUE(...) ... ...

TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE

... ... [OPTIONAL] [OPTIONAL] ... ... ... ... [OPTIONAL] [OPTIONAL] ... ...

z METHODS:

Instance methods

z CLASS-METHODS: Static methods


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

Constructors
CLASS CLASS c c DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. METHODS METHODS CONSTRUCTOR CONSTRUCTOR [IMPORTING [IMPORTING arg1 arg1 TYPE TYPE type type ... ... ]. ]. CLASS-METHODS CLASS-METHODS CLASS_CONSTRUCTOR. CLASS_CONSTRUCTOR. ENDCLASS. ENDCLASS. CLASS CLASS c c IMPLEMENTATION. IMPLEMENTATION. METHOD METHOD CONSTRUCTOR. CONSTRUCTOR. ... ... ENDMETHOD. ENDMETHOD. METHOD METHOD CLASS_CONSTRUCTOR. CLASS_CONSTRUCTOR. ... ... ENDMETHOD. ENDMETHOD. ENDCLASS. ENDCLASS. PROGRAM PROGRAM . . DATA DATA o1 o1 TYPE TYPE REF REF CREATE CREATE OBJECT OBJECT o1 o1 TO TO c. c. EXPORTING EXPORTING arg1 arg1 = = v1 v1 ... ...

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

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 ...

ME

SAP AG 1999 ABAP Objects - 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

Inheritance and Constructors

CLASS CLASS subclass subclass DEFINITION DEFINITION INHERITING INHERITING FROM FROM superclass. superclass. PUBLIC PUBLIC SECTION. SECTION. METHODS METHODS CONSTRUCTOR CONSTRUCTOR IMPORTING IMPORTING ... ... ... ... ENDCLASS. ENDCLASS. CLASS CLASS subclass subclass IMPLEMENTATION. IMPLEMENTATION. METHOD METHOD CONSTRUCTOR. CONSTRUCTOR. Access to static ... ... attributes only CALL CALL METHOD METHOD SUPER->CONSTRUCTOR SUPER->CONSTRUCTOR EXPORTING EXPORTING ... ... ... ... ENDMETHOD. ENDMETHOD. Accress to instance ... ... attributes also ENDCLASS. ENDCLASS.

PROGRAM PROGRAM ... ... DATA DATA o1 o1 TYPE TYPE REF REF CREATE CREATE OBJECT OBJECT o1 o1 TO TO subclass. subclass. TYPE TYPE subclass subclass EXPORTING EXPORTING ... ...

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

Subclass for Superclass Counter*


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

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

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 - Introduction (Horst Keller) / 43

Interfaces - Overview

INTERFACE i1. DATA: a1 ... METHODS: m1 ... EVENTS: e1 ... ENDINTERFACE.

class c1 Public components


a1,... i1~a1, i1~m1, ...

Private components
a2, m2, e2 Method implementations

CLASS c1 DEFINITION. PUBLIC SECTION. DATA a1 ... INTERFACES i1 ... PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD i1~m1. ENDMETHOD. ENDCLASS.
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 44

Protected components a3, m3, e3, Subclasses of c1 All users

Interfaces - Definition

INTERFACE INTERFACE ... ... ... ... DATA: DATA: CLASS-DATA: CLASS-DATA: CONSTANTS: CONSTANTS:

. . ... ... ... ... ... ... TYPE TYPE TYPE TYPE TYPE TYPE ... ... ... ... ... ... [READ-ONLY] [READ-ONLY] ... ... [READ-ONLY] [READ-ONLY] ... ... [VALUE [VALUE ...] ...] TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE ... ... [OPTIONAL] [OPTIONAL] ... ... ... ... [OPTIONAL] [OPTIONAL] ... ...

METHODS: METHODS: ... ... IMPORTING IMPORTING EXPORTING EXPORTING CHANGING CHANGING RETURNING RETURNING EXCEPTIONS EXCEPTIONS CLASS-METHODS: CLASS-METHODS: ... ...

[VALUE] [VALUE] ... ... [VALUE] [VALUE] ... ... [VALUE] [VALUE] ... ... VALUE(...) VALUE(...) ... ...

EVENTS: EVENTS: ... ... [EXPORTING [EXPORTING VALUE(...) VALUE(...) TYPE TYPE ... ... [OPTIONAL]]. [OPTIONAL]]. CLASS-EVENTS:... CLASS-EVENTS:... INTERFACES: INTERFACES: ... ... ENDINTERFACE. ENDINTERFACE.

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

Interfaces - Implementation

CLASS CLASS ... ... DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. INTERFACES: INTERFACES: ... ... ... ... ... ... ENDCLASS. ENDCLASS.

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

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

Interface References
INTERFACE INTERFACE i1. i1. ... ... ENDINTERFACE. ENDINTERFACE. CLASS CLASS c1 c1 DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. DATA DATA a1. a1. INTERFACES INTERFACES i1. i1. ENDCLASS. ENDCLASS. CLASS CLASS c2 c2 DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. INTERFACES INTERFACES i1. i1. ENDCLASS. ENDCLASS. 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. c1. REF REF TO TO i1, i1, cnt_1. cnt_1. 1<C2>

1<C1>

CNT_C

CREATE CREATE OBJECT: OBJECT: cnt_c cnt_c TYPE TYPE c1, c1, cnt_1 cnt_1 TYPE TYPE c2. c2. CNT_2 MOVE MOVE cnt_c cnt_c to to cnt_2. cnt_2.
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 47

Interface for Object State - Implementation


INTERFACE INTERFACE status. status. METHODS write. METHODS write. ENDINTERFACE. ENDINTERFACE. CLASS CLASS counter counter DEFINITION. DEFINITION. PUBLIC SECTION. PUBLIC SECTION. INTERFACES INTERFACES status. status. METHODS increment. METHODS increment. PRIVATE PRIVATE SECTION. SECTION. DATA DATA count count TYPE TYPE i. i. ENDCLASS. ENDCLASS. CLASS CLASS counter counter IMPLEMENTATION. IMPLEMENTATION. METHOD status~write. METHOD status~write. WRITE: WRITE: 'Count 'Count in in counter counter is', is', count. count. ENDMETHOD. ENDMETHOD. METHOD METHOD increment. increment. count count = = count count + + 1. 1. ENDMETHOD. ENDMETHOD. ENDCLASS. ENDCLASS. CLASS CLASS bicycle bicycle IMPLEMENTATION. IMPLEMENTATION. METHOD METHOD status~write. status~write. WRITE: 'Speed WRITE: 'Speed of of bicycle bicycle is', is', speed. speed. ENDMETHOD. ENDMETHOD. METHOD METHOD drive. drive. speed speed = = speed speed + + 10. 10. ENDMETHOD. ENDMETHOD. ENDCLASS. ENDCLASS.

CLASS CLASS bicycle bicycle DEFINITION. DEFINITION. PUBLIC SECTION. PUBLIC SECTION. INTERFACES INTERFACES status. status. METHODS drive. METHODS drive. PRIVATE PRIVATE SECTION. SECTION. DATA DATA speed speed TYPE TYPE i. i. ENDCLASS. ENDCLASS.

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

Interface for Object State - Use

DATA: DATA: count count TYPE TYPE REF REF TO TO counter, counter, bike bike TYPE TYPE REF REF TO TO bicycle, bicycle, status status TYPE TYPE REF REF TO TO status. status. CREATE CREATE OBJECT: OBJECT: count, count, bike. bike. DO DO 5 5 TIMES. TIMES. CALL CALL METHOD: METHOD: count->increment, count->increment, bike->drive. bike->drive. ENDDO. ENDDO. status status = = count. count. CALL METHOD CALL METHOD status->write. status->write. status status = = bike. bike. CALL METHOD CALL METHOD status->write. status->write.

Counter status

Bike speed
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 49

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) / 50

Events - 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 - Introduction (Horst Keller) / 51

Events - Overview

CLASS c1 DEFINITION. PUBLIC SECTION. EVENTS e1 EXPORTING VALUE(p1) TYPE i. METHODS m1. PRIVATE SECTION. DATA a1 TYPE i. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD m1. a1 = ... RAISE EVENT e1 EXPORTING p1 = a1. ENDMETHOD. ENDCLASS.

CLASS c2 DEFINITION. PUBLIC SECTION. METHODS m2 FOR EVENT e1 OF c1 IMPORTING p1.

PRIVATE SECTION. DATA a2 TYPE i. ENDCLASS.

CLASS C2 IMPLEMENTATION. METHOD m2. a2 = p1. ... ENDMETHOD. ENDCLASS.

Event trigger
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 52

Event handler

Declaring and Triggering Events


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

Event Handler Methods

CLASS CLASS ... ... DEFINITION. DEFINITION. ... ... SECTION. SECTION. METHODS METHODS ... ... FOR FOR EVENT EVENT ... ... OF OF ... ... [IMPORTING [IMPORTING ... ... SENDER SENDER ... ... ]. ].

ENDCLASS. ENDCLASS.

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

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

Event Handling - Registration

PROGRAM PROGRAM ... ... 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, trigger, handler, handler, handler. handler.

CREATE CREATE OBJECT: OBJECT: trigger, trigger, handler_1, handler_1, handler_2. handler_2. SET SET HANDLER HANDLER handler_1->handle_event handler_1->handle_event handler_2->handle_event handler_2->handle_event FOR FOR trigger. trigger. CALL CALL METHOD METHOD trigger->raise_event. trigger->raise_event.

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

Handling Events - References

Ereignis Methode Methode

2<HANDLER>

1<HANDLER>

.
1<TRIGGER>

HANDLER_1 HANDLER_2 TRIGGER

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

Threshold in Counter - Trigger


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

Threshold in Counter - Handling


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

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

Class Pools
CLASS-POOL ... TYPES TYPES CLASS CLASS ... ... ENDCLASS. ENDCLASS. INTERFACE INTERFACE ... ... ENDINTERFACE. ENDINTERFACE.

Visibility

CLASS CLASS DEFINITION DEFINITION PUBLIC. PUBLIC. ... ... ENDCLASS. ENDCLASS. CLASS CLASS IMPLEMENTATION. IMPLEMENTATION. ... ... ENDCLASS. ENDCLASS.
SAP AG 1999 ABAP Objects - Introduction (Horst Keller) / 60

Class Library and Class Browser

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

Class Builder

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

Using a Global Class


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

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

After Mouse Click

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

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

Further Information

Keyword Documentation

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

Exercises

1. Defining classes 2. Creating objects 3. Deriving classes using inheritance 4. Using interfaces 5. Triggering and handling events

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

Exercises - 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, a name, and an event

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

Exercise 1 - Classes

z Copy the template ABAP_OBJECTS_ENJOY_0 into an own program. z Define a local class VEHICLE in the designated area in front of the

predefined class MAIN.


z Class VEHICLE should have the protected instance attributes SPEED and

MAX_SPEED for its speed and maximum speed, and the public methods SPEED_UP, STOP, and WRITE.
z SPEED_UP should have an IMPORTING parameter STEP. The method

should increase the speed by STEP, but not allow it to exceed the maximum speed.
z STOP should reset the speed to zero.

z WRITE should display a list line containing the speed and the maximum speed.

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

Exercise 2 - Objects
z Continuing the program from exercise 1, create objects from the class

VEHICLE.
z Program the respective coding in the predefined method START of class

MAIN.
z Define a reference variable VEHICLE with type VEHICLE, and an internal

table VEHICLE_TAB, whose line type is also a reference variable to this class.
z In a DO loop, create a number of instances of the class VEHICLE and

insert them into the internal table.


z In a LOOP construction, call the methods SPEED_UP and WRITE once for

each entry in the internal table. When you call SPEED_UP, pass the value SY-TABIX * 10 to the parameter STEP.

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

Exercise 3 - Inheritance
z Change your program from exercise 2 to define classes TRUCK and SHIP

as direct subclasses of VEHICLE.


z The class TRUCK should have an instance constructor and redefine the

method WRITE.
z The class SHIP should have an IMPORTING parameter NAME, a new

public attribute NAME, and should also redefine the method WRITE.
z The instance constructor of each class should change the maximum

speed. The instance constructor of SHIP should set the attribute NAME to the actual parameter that you imported.
z The WRITE method should show the class from which the display comes.

For SHIP, use the NAME attribute.


z Declare extra reference variables TRUCK and SHIP for the new classes. z You can delete the code that creates objects for VEHICLE. Instead, create

one instance of each of your new classes and place the corresponding reference into VEHICLE_TAB.
z Call the method SPEED_UP for both classes using the correct subclass

reference, and WRITE using a superclass reference.

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

Exercise 4 - Interfaces
z Change your program from exercise 3 so that the method WRITE is

contained in an interface STATUS.


z The class VEHICLE should implement the interface instead of its own

WRITE method. The name must be changed accordingly in each implementation (including the subclasses), but the function remains the same.
z Replace the reference variables VEHICLE and the table VEHICLE_TAB

with the interface reference STATUS and internal table STATUS_TAB.


z Use these references to display the status of the objects. z Create a new class HELICOPTER that also implements STATUS. Declare

an alias WRITE in that class for the interface method.


z The interface method in HELICOPTER should display a different line from

that displayed in VEHICLE.


z Declare a reference variable HELI for the class HELICOPTER, create a

corresponding object, and insert the reference variable into the table STATUS_TAB.

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

Exercise 5 - Events
z Change your program from exercise 4 to include an event DAMAGED for

the class SHIP.


z Redefine the method SPEED_UP in the class SHIP. z When the maximum speed is exceeded, SPEED_UP should set the

maximum speed to zero and trigger the event DAMAGED.


z Add a new method RECEIVE to the class HELICOPTER to allow it to

handle the event DAMAGED. Import the implicit parameter SENDER.


z RECEIVE should use the SENDER parameter to display the name of the

ship that triggered the event.


z Register the handler method of the object to which HELI is pointing as a

handler for objects from the class SHIP.


z Increase the speed of the object from the class SHIP until it exceeds the

maximum speed and triggers the event.

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