Sunteți pe pagina 1din 243

ABAP 100

Object-Oriented Programming with SAP NetWeaver

msg systems ag, 15.01.2013

ABAP Programming

Level 1

Level 1

SAP Basics Day

ABAP 010

3 Days

Basics of
SAP Web AS

ABAP Workbench
basics and
concepts

Level 3
ABAP030

2 Days

ABAP Dictionary

ABAP 100

3 Days

ABAP Objects
Object Oriented
Programming
with SAP NetWeaver

ABAP110

2 Days

Development of
user dialogs

ABAP111 1 Days
Table Controls &
ALV Grids

msg systems ag, 15.01.2013

Agenda

1. Objectives and Introduction


2. Introduction to Object-Oriented Programming
3. Analysis and Design
4. Fundamentals of Object-Oriented Programming
5. Inheritance
6. Casting
7. Interfaces
8. Events
9. Global Classes and Interfaces
10. Programming Techniques
11. Exception Handling
12. Dynamic Programming

msg systems ag, 15.01.2013

1. Objectives and Introduction

msg systems ag, 15.01.2013

1. Objectives and Introduction


ABAP Objects Level 3

ABAP Objects
Advanced Business Application Programming
for
Object-Oriented Programming

A new Concept in application programming, which started imposing itself


around the middle of 90
With the introduction of the Release 4.6 the SAP changed the name of the
language from ABAP/4 in ABAP Objects

msg systems ag, 15.01.2013

1. Objectives and Introduction


Course Objectives

This course provides knowledge to:

the concepts of object-oriented programming

the development of Business Applications using ABAP


Objects

msg systems ag, 15.01.2013

1. Objectives and Introduction


Course Learning Objectives

You will acquire knowledge to:

describe and apply the most important concepts:


classes / inheritance / interfaces

polymorphism with inheritance and interfaces


Events
Exception handling

write programs using ABAP Objects.

msg systems ag, 15.01.2013

1. Objectives and Introduction


Overview

A travel agency maintains business connections to business partners


Airlines
Rental companies
Hotels

????

msg systems ag, 15.01.2013

2. Introduction to Object-Oriented Programming

Procedural Programming
Object-Oriented Programming
Goals of the Programming Language ABAP Objects

msg systems ag, 15.01.2013

2. Introduction to Object-Oriented Programming


History of Selected Programming Languages
Machine language

1950
Assembler
1952
1954
Fortran
1956
Cobol
1958
LISP
Algol 60
1960
APL
Basic
1962
PL1
1964
1966 Prolog
Simula 67 Algol 68
Logo
1968
BCPL
1970
Smalltalk
Pascal
1972
1974
C
1976
APL2
1978 FP1
Modula-2
1980
Smalltalk-80
Ada
1982
1984
ABAP
Object Pascal
1986 Miranda
C++
1988
Modula-3
Eiffel
Oberon
1990
1992
Eiffel-3
Oberon-2
1994
Java
Ada-95
1995
1996
1997
ABAP Objects
1998

10

msg systems ag, 15.01.2013

2. Introduction to Object-Oriented Programming

11

Procedural Programming
Object-Oriented Programming
Goals of the Programming Language ABAP Objects

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Characteristics of Procedural Programming

Daten

Daten

Separation of data and functions

Direct access to data is possible

Daten

Daten

Daten

Funktion

Funktion

Funktion

12

Funktion

Funktion

Funktion

Funktion

Funktion

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Typical Procedural ABAP Program
REPORT zabap_demo
* -----------------

Data declarations

Main program

TYPES: ...
DATA: ...
...

Subroutine calls

PERFORM f1 ...

Function module calls

CALL FUNCTION ...


...

Definition of subroutines

* ----------------FORM f1 ...
...
ENDFORM.

13

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Encapsulating Data Using Function Groups

Main program
Global data objects

Function groups
Global data

Function group 1

Function module 1
Function module 2
Function module ...

Global data

Function group 2

Subroutines
Interface

Interface

14

Function module 1

Function module 2

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Example: Function Group 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.
ADD 1 TO count.
ENDFUNCTION.
FUNCTION GET_COUNTER.
* Local interface EXPORTING VALUE(get_value)
get_value = count.
ENDFUNCTION.

15

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Example: Function Group Counter Usage
DATA: number TYPE I VALUE 3.

CALL FUNCTION 'SET_COUNTER'


EXPORTING set_value = number.
DO 4 TIMES.
CALL FUNCTION 'INCREMENT_COUNTER'.
ENDDO.

CALL FUNCTION 'GET_COUNTER'


IMPORTING get_value = number.
WRITE: ..., number, ...

16

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Several Counters

1 counter

any number of counters

Function group COUNTER


SET_COUNTER
INCREMENT_COUNTER

Not possible with function


groups, without additional
effort!

COUNTER

GET_COUNTER

17

msg systems ag, 15.01.2013

2. Introduction Procedural Programming


Instantiation in object-oriented languages

n. Instance, Class 1
1. Instance, Class 1
SchnittDatea
stelle
Interface Data

n. Instance, Class m
1. Instance, Class m
SchnittDatea
stelle
Interface Data

ABAP Program with Data

Several instances
(Objects)
of the same Blueprint
(Type or better Class)
are a fundamental
property of objectoriented languages.

Internal session of an ABAP program

External Session

18

msg systems ag, 15.01.2013

2. Introduction to Object-Oriented Programming

19

Procedural Programming
Object-Oriented Programming
Goals of the Programming Language ABAP Objects

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming

Real world

Data and functions

Encapsulation of data
and functions
Functions

Data

Data

Data

Daten

Functions
Functions

Data

Data

Kran
Functions

Daten

Functions
Functions

Functions

Functions

Functions

Functions

Functions
Functions Functions

Functions

Functions

Daten

Functions
Functions

20

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Characteristics of the Object-Oriented approach
Real world

Objects are a direct abstraction of the real


world.

Processes can be implemented realistically.

Objects are units made up of data and the


functions belonging to that data.

Improved software structure and consistency


in the development process.

Reduced maintenance effort.

Integration of the customer/user into the


analysis, design, and maintenance process.

Kran

21

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Additional Concepts of the Object-Oriented Programming Model

Encapsulation of data and functions


(Hiding the implementation details)

Functions

Data
Functions

Father

Inheritance
Child 1

Child 2

Code 2

Polymorphism for support of generic


programming

display_data
Code 1

Code 3

Code 4

22

Event controlling

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Additional Concepts of the Object-Oriented Programming Model

Encapsulation of data and functions

Telephone

Telephone
myNumber
doCall

register
doCall

beCalled
myNumber

23

beCalled

register

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Additional Concepts of the Object-Oriented Programming Model

Inheritance

Telephone

24

OfficePhone

CellPhone

myNumber

myContacts

mySMS

register

addContact

sendSMS

doCall

callContact

getSMS

beCalled

deleteContact
msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Additional Concepts of the Object-Oriented Programming Model

Polymorphism for support of generic programming

beCalled

beCalled

beCalled
MP3

25

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Additional Concepts of the Object-Oriented Programming Model

Event controlling

msg
register_me

Phone
Center

26

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Client/server behavior

Objects behave towards each other just liker client/server


systems.

Every object can essentially fulfill either role.

Responsibilities must be specified between the objects.

Methods
Data

call_it

Client

27

CALL METHOD server->do_it

Methods
do_it

Data

Server

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Compatibility and design goals
* ABAP Objects Program

DATA: counter TYPE i,


wa TYPE kna1.
...

CLASS lcl_car DEFINITION.


...
ENDCLASS.
*--- main program --------CREATE OBJECT ...
counter = counter + 1.
MOVE wa TO ...

28

Compatibility:

ABAP Objects is a compatible


extension of ABAP

ABAP Objects statements can be


used in procedural ABAP
programs

Objects (classes) contain, among


other, classical ABAP statements
Design goals:

As simple as possible

only object-oriented concepts that


have proved useful

Increased use of type checks

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


ABAP Main Memory and Encapsulation
Internal session
Global data objects

Modularization units

Modularization units for


encapsulating functions

Working with global data of


the main program

Internal session

Global data objects


Functions
Data

Data and business logic


are encapsulated in
objects

Functions
Data

29

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


The Software Development Process

Requirement,
Idea

Test

Mentioned in the course!

Iteration

Analysis and
Design

30

Implementation
(ABAP Objects)

msg systems ag, 15.01.2013

2. Introduction Object-Oriented Programming


Summary

You should now be able to:


explain the differences between procedural and objectoriented programming models
explain the goals of the programming language ABAP
Objects

31

msg systems ag, 15.01.2013

3. Analysis and Design

32

Classification of Objects
UML the Modeling Standard
Class diagrams
Sequence diagrams

msg systems ag, 15.01.2013

3. Analysis and Design Classification


Class creation / modeling
Excerpt of the
real world

lcl_vehicle
33

lcl_people
msg systems ag, 15.01.2013

3. Analysis and Design Classification


Classes as Abstraction Forms

Scrap-merchant

Car enthusiast

Price
price

Make
make

lcl_vehicle
- set

Configuration Model
configuration

- ...
- price
- configuration

Color
color

Weight
weight

Doors

model

- make

doors

Wheels
wheels

PS

lcl_vehicle

Scrap value
scrapvalue

- ...
- weight
- scrapvalue

HP

34

msg systems ag, 15.01.2013

3. Analysis and Design Classification


Classes and Objects

Class:
General description of
object (blueprint)

Specifies status data


(attributes) and behavior
(methods)

lcl_vehicle

lcl_vehicle

or

Object (Instance):
Representation of section
of the real world
Concrete instance of a
class

(5) lcl_vehicle

35

or

msg systems ag, 15.01.2013

3. Analysis and Design

36

Classification of Objects
UML the Modeling Standard
Class diagrams
Sequence diagrams

msg systems ag, 15.01.2013

3. Analysis and Design Modeling in UML

UML stands for Unified Modeling Language

is a worldwide standard
it is a language used for the specification, construction,
visualization, and documentation of models for software systems

Diagram types:
Class diagrams
Behavior diagrams, e.g. the Sequence diagram
Component diagrams
Deployment diagrams
...

37

msg systems ag, 15.01.2013

3. Analysis and Design Classification


Class diagram in UML notation

lcl_vehicle

Class name

38

Attributes
(Properties)

make
model
.....
price
color

Methods
(Functions)

set_make()
display_attributes()
increases_speed()

Optional,
does not have
Assoziationsme
to be specified

Events +
Handlers

msg systems ag, 15.01.2013

3. Analysis and Design Modeling in UML


Example of a Class Diagram

lcl_rental

0..*

lcl_booking

0..* 1

lcl_customer

1..*

lcl_vehicle

lcl_car

39

lcl_truck

1..*

lcl_wheel

lcl_bus

msg systems ag, 15.01.2013

3. Analysis and Design Modeling in UML


Association
A customer can
rent any amount of
cars

lcl_booking

A booking refers
to a single
customer

0..*

books

Common cardinalities
* or 0..*
1
1..*
0..1

40

lcl_customer

Description of the
association

any amount
exactly one
at least one
no more than one

msg systems ag, 15.01.2013

3. Analysis and Design Modelling with UML


Aggregation und Composition

Aggregation Symbol

Aggregation is a special
case of association,
a whole-part relationship.

1..1

lcl_vehicle

Composition is a special
case of aggregation, an
existence-dependent
a whole-part relationship.

lcl_wheel

Composition Symbol
1..1

lcl_vehicle

41

0..*

1..*

lcl_booking

msg systems ag, 15.01.2013

3. Analysis and Design Modelling with UML


Generalization and Specialization

lcl_car

lcl_truck

Generalization

Arrow for Inheritance

Specialization

lcl_vehicle

lcl_vehicle

lcl_car
42

lcl_truck

Generalization

Specialization

or

msg systems ag, 15.01.2013

3. Analysis and Design Modelling with UML


Sequence diagram: Example

Object
:Class

Create Object
Time

Process
description
(optional)

Object
:Class

1:Method(parameter)

Lifeline
of the object

Return value

Control focus

43

msg systems ag, 15.01.2013

3. Analysis and Design Modelling with UML


Sequence diagram : Delegation

:lcl_driver

:lcl_car

:lcl_tank

1: get_fuel_level( )

2: get_fuel_level( )
re_level
re_level

re_level =
tank->get_fuel_level( ).

44

re_level =
fuel / fuel_max * 100.

msg systems ag, 15.01.2013

3. Analysis and Design Summary

You should now be able to


name the most important diagram types in UML
create class diagrams
create sequence diagrams

45

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Analysis and Design
UML Class Diagrams

46

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming

47

Classes, Attributes and Methods


Objects, Instances of Classes
Visibility / Encapsulation
Constructor
Other fundamental elements

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Example of a Class

lcl_vehicle
Public
components
get_make()
Public access:
Methods
Events

set_make()

Private
components
make
model

Private Access:
Data types
Attributes

Methods:
Implementation

48

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Defining Classes

ABAP-Coding
CLASS lcl_periode DEFINITION.

ENDCLASS.

lcl_vehicle
Public
Components

Private
Components

CLASS lcl_periode IMPLEMENTATION.


Method
Implementation

ENDCLASS.

49

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Attributes

Attribute can have


any data type:

(5) lcl_car

C, N, I, P, ..., STRING
dictionary types

Public

self defined types


TYPE REF TO
defines a reference
to an object, here r_car

Private
make TYPE STRING,
...
r_motor
TYPE REF TO ...

(5) lcl_rental
r_car

50

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Attributes, Types and Constants: Syntax
CLASS <Class_name> DEFINITION.
...

TYPES: <normal type_definition>.


CONSTANTS: mc_constant TYPE <Type> VALUE <Value>.
DATA: mv_var1
ms_var2
mv_var3
mv_var4
mv_var5
mo_var6
mo_var7

TYPE
TYPE
LIKE
TYPE
TYPE
TYPE
TYPE

<Type>,
<DDIC-Type>,
mv_var1,
<Type> VALUE <value>,
<Type> READ-ONLY,
REF TO <Class>,
REF TO <Interface>.

CLASS-DATA: mv_only_once TYPE ... .

ENDCLASS.

51

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Attributes and Visibility
CLASS lcl_vehicle DEFINITION.

Public attributes
generally visible
and changeable
direct access

Private attributes
only visible and
changeable within the
class

no direct access from


outside

PUBLIC SECTION.
DATA: make TYPE string.

PRIVATE SECTION.
ENDCLASS.

CLASS lcl_vehicle DEFINITION.


PUBLIC SECTION.
...

PRIVATE SECTION.
DATA: make TYPE string.
ENDCLASS.

52

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Accessing Private Attributes

Client

(5) lcl_vehicle
Public

Private
make

r_vehicle

53

set_make()
get_make()

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Instance attributes and static attributes

Instance attributes

exist per instance

CLASS lcl_vehicle DEFINITION.


PUBLIC SECTION.
...

Defined with DATA

PRIVATE SECTION.
DATA: make TYPE string,
...

54

Static attributes

exist per class

Defined with CLASS-DATA

are also defined as class


attributes

CLASS-DATA: n_o_vehicles TYPE i.


ENDCLASS.

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Instance attributes and static attributes
Internal session
Global data objects
n_o_vehicles

Static attributes
exist only once

Static attribute

(1) lcl_vehicle

r_vehicle1

(2) lcl_vehicle

Instances /
objects

r_vehicle2
(3) lcl_vehicle

r_vehicle3

...

55

...

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Methods: Syntax

CLASS <Class_name> DEFINITION.


...
METHODS:
<Method_name>
IMPORTING <im_par> TYPE <Type>
EXPORTING <ex_par> TYPE <Type>
CHANGING <ch_ar> TYPE <Type>
RETURNING value(<re_par>) TYPE <Type>
EXCEPTIONS <Exceptions (not class based)>
RAISING
<class based exceptions>.
ENDCLASS.
CLASS <Class_name> IMPLEMENTATION.
METHOD <Method_name>.
...
ENDMETHOD.
ENDCLASS.

56

Methods can
have a signature

The method
implementation governs
the behavior of the
objects .

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Accessing Private Methods

Client

(5) lcl_vehicle
Public

Private
make
...

r_vehicle

57

...
set_make()

init_make()

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Methods and Visibility

Public methods
can generally be called

Private methods
can only be called
within the class

58

CLASS lcl_vehicle DEFINITION.


PUBLIC SECTION.
METHODS: set_make
IMPORTING im_make TYPE string.
PRIVATE SECTION.
METHODS: init_make.
DATA:
make TYPE string.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
METHOD init_make.
make = 'No make'.
ENDMETHOD.
METHOD set_make.
IF im_make IS INITIAL.
init_make( )
ELSE.
make = im_make.
ENDIF.
ENDMETHOD.
ENDCLASS.
msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Instance methods and static methods

Rules

Instance methods
access to static but also instance components is possible in
their implementation
can be invoked through an instance

Static method
can use only static components in their implementation
are addressed with their class name

59

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Instance- and static methods: Example
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS:
set_make IMPORTING im_make TYPE string.
CLASS-METHODS: get_count EXPORTING ex_count TYPE i.
PRIVATE SECTION.
DATA:
make TYPE.
CLASS-DATA: n_o_count TYPE i.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
...
METHOD get_count.
ex_count = n_o_count.
ENDMETHOD.
ENDCLASS.

60

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Summary and UML Notation

lcl_vehicle
- make:string
- model:string
- n_o_vehicles:i

indicates public components

indicates private components

static components are underlined

+ set_make(im_make:string)
- init_make()
+ get_count():i

CLASS lcl_vehicle DEFINITION.


PUBLIC SECTION.
METHODS:
set_make IMPORTING im_make TYPE string.
CLASS-METHODS: get_count EXPORTING ex_count TYPE i.
PRIVATE SECTION.
DATA:
make TYPE.
CLASS-DATA: n_o_count TYPE i.
ENDCLASS.
61

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
OOP Fundamentals
Creating a class

62

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming

63

Classes, Attributes and Methods


Objects, Instances of Classes
Visibility / Encapsulation
The Constructor
Other fundamental elements

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Creating objects

Rules

Objects are created with the statement


CREATE OBJECT.

They can only be created and addressed using


reference variables

lcl_vehicle
-

make
model
ser_no
n_o_vehicles

+ set_make()
- init_make()
+ get_count()

64

(5) lcl_vehicle

CREATE OBJECT r_vehicle

Public

Private

set_make
get_count

make
modell
ser_no
...

r_vehicle

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Definition of Reference Variables
...
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
...
PRIVATE SECTION.
...
ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.


...
ENDCLASS.

DATA: r_vehicle1 TYPE REF TO lcl_vehicle,


r_vehicle2 TYPE REF TO lcl_vehicle.

What do these
reference variables
point to?

r_vehicle1
r_vehicle2

START-OF_SELECTION.
...
65

?
?
msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Creating Objects
DATA: r_vehicle1 TYPE REF TO lcl_vehicle,
r_vehicle2 TYPE REF TO lcl_vehicle.
START-OF-SELECTION.
CREATE OBJECT r_vehicle1.
CREATE OBJECT r_vehicle2.

(3) lcl_vehicle
Public

Private

r_vehicle1

r_vehicle2

(4) lcl_vehicle
Public

66

Private

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Garbage Collector
DATA: r_vehicle1 TYPE REF TO lcl_vehicle,
r_vehicle2 TYPE REF TO lcl_vehicle.
START-OF-SELECTION.
CREATE OBJECT r_vehicle1.
CREATE OBJECT r_vehicle2.

(3) lcl_vehicle
Public

Private

r_vehicle2 = r_vehicle2.
r_vehicle1

r_vehicle2

(4) lcl_vehicle
Public

67

Private

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Garbage Collector: Approach

All independent references from the global memory are checked. The
references point to 'living' objects, that are marked internally.
The references from class- and instance attributes to further objects are
tracked. These objects are also marked.
Unmarked objects are removed from the main memory.

(4) lcl_object

CLEAR r_object
(2) lcl_object

(5) lcl_object

68

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Reference Buffering
DATA: r_vehicle TYPE REF TO lcl_vehicle,
itab TYPE TABLE OF REF TO lcl_vehicle.
CREATE OBJECT r_vehicle.
APPEND r_vehicle TO itab.

CREATE OBJECT r_vehicle.


APPEND r_vehicle TO itab.

(2) lcl_object

(3) lcl_object

LOOP AT itab INTO r_vehicle.


" work with the current instance
ENDLOOP.
...

69

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Aggregation Example

(5) lcl_vehicle
Public

(2) lcl_wheel

Private
(3) lcl_wheel
itab_wheels

(4) lcl_wheel

(5) lcl_wheel

70

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
OOP Fundamentals
Object instantiation

71

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming

72

Classes, Attributes and Methods


Objects, Instances of Classes
Visibility / Encapsulation
The Constructor
Other fundamental elements

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Calling Methods

Client
(5) lcl_vehicle
r_vehicle

r_vehicle->motor_on( )
Reference

Public

Private

motor_on()

Method

Object component selector

73

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


EXPORTING and IMPORTING

When creating a method:


Main program

Method

IMPORTING
im_distance

ex_fuel
EXPORTING

Received values
Returned values

74

IMPORTING
EXPORTING

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


EXPORTING and IMPORTING

When creating a method:


Main program

EXPORTING
CALL METHOD
EXPORTING
im_distance = 500
IMPORTING
ex_fuel = lv_fuel.
IMPORTING

Method

IMPORTING
im_distance

ex_fuel
EXPORTING

EXPORTING in the IMPORTING parameter


IMPORTING in the EXPORTING parameter

75

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Calling Instance Methods: Syntax
Instance methods:

->
CALL METHOD <InstRef>-><InstMethod>
EXPORTING <InpParam>
= <Value>
IMPORTING <ExpParam>
= <Variable>
CHANGING
<ChgParam>
= <Variable>
RECEIVING <ReturnVal> = <Variable>
EXCEPTIONS <Exception> = <Nr>.

<InstRef>-><InstMethod>(
<Parameter> )
->
Shorter syntax:
(see online documentation)
No space before opening bracket
SPACE!
DATA: r_vehicle TYPE REF TO lcl_vehicle.
DATA: make_name TYPE string.
...
make_name = `the make of the car`.
CALL METHOD r_vehicle->set_make EXPORTING im_make = make_name.
* as of NW 6.1 keyword EXPORTING can be left out
r_vehicle->set_make
r_vehicle->set_make( make_name )
* now getting something back from method...
r_vehicle->get_make( IMPORTING ex_make = make_name ).
r_vehicle->get_make
76

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Calling Static Methods: Syntax

Static methods:
=>
CALL METHOD <KlassenName>=><KlassenMethode>(
[Parameter] )
<KlassenName>=><KlassenMethode>(
[Parameter] )
=>

DATA: r_vehicle TYPE REF TO lcl_vehicle.


DATA: make_name TYPE string,
count
TYPE i.
...

* get_count has one interface-parameter re_count


CALL METHOD lcl_vehicle=>get_count(
lcl_vehicle=>get_count IMPORTING re_count = count ).
* other variant, since 6.1 available, see documentation
lcl_vehicle=>get_count( IMPORTING re_count = count ).
lcl_vehicle=>get_count

77

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Functional Methods
Example

a = b +

Functional Method

Definition:
Precisely one RETURNING parameter
Otherwise, only IMPORTING parameters and exceptions are
possible

Call:
a) RECEIVING-Parameter, or ...
b) ... implicit calls possible in various expressions:
MOVE, CASE, LOOP
logical expressions (IF, ELSEIF, WHILE, CHECK, WAIT)
arithmetic and bit expressions (COMPUTE)
see example a = b + c

78

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Functional Methods: Example
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: get_average_fuel
IMPORTING im_distance TYPE s_distance,
im_fuel
TYPE ty_fuel
) TYPE ty_fuel,
RETURNING value(
value(re_fuel)
ENDCLASS.
DATA: r_vehicle1 TYPE REF TO lcl_vehicle,
r_vehicle2 TYPE REF TO lcl_vehicle,
avg_fuel TYPE ty_fuel.
...

* example for short syntax in aritmeth. operation


avg_fuel =
r_vehicle1->get_average_fuel( im_distance = 500 im_fuel = 50 )
+ r_vehicle2->get_average_fuel( im_distance = 600 im_fuel = 60 ).

79

msg systems ag, 15.01.2013

4. Fundamentals of Object-Oriented Programming


Non-class-based exceptions: Example
CLASS lcl_plane DEFINITION.
PUBLIC SECTION.
METHODS: get_tank_capacity
RETURNING value(rv_tankcap) TYPE saplane-tankcap
EXCEPTIONS unknown_tank_capacity
unknown_tank_capacity.
PRIVATE SECTION.
DATA: mv_planetype TYPE saplane-planetype.
ENDCLASS.
RAISE unknown_tank_capacity.
DATA: lo_plane
TYPE REF TO lcl_plane,
lv_tankcap TYPE saplane-tankcap.
* usage of non-class-based exceptions
lo_plane->get_tank_capacity( RECEIVING rv_tankcap = lv_tankcap
EXCEPTIONS unknown_tank_capacity = 1
= 2 ).
OTHERS
IF sy-subrc EQ 0.
"Normal operation.
ELSE.
"Exception handling.
ENDIF.
80

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


External Access to Public Attributes
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
DATA: make TYPE string READ-ONLY.
CLASS-DATA: n_o_vehicles TYPE i READ-ONLY.

"just a demo
"just a demo

...

ENDCLASS.
...

(2) lcl_vehicle

DATA: r_vehicle TYPE REF TO lcl_vehicle.


DATA: make_name TYPE
string,
count
TYPE
i.

make
...

START-OF-SELECTION.
*--------------------------------------* main program, think of a client !
CREATE OBJECT r_vehicle.
r_vehicle
make_name = r_vehicle->make
r_vehicle->make.
count
= lcl_vehicle=>n_o_vehicles
lcl_vehicle=>n_o_vehicles.

81

"only if public attr.


"only if public attr.

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Fundamentals of OOP
Method Calls

82

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming

83

Classes, Attributes and Methods


Objects, Instances of Classes
Visibility / Encapsulation
Constructor
Other fundamental elements

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Constructor

Special method for creating


objects with a defined initial state

Can only have IMPORTING


parameter and EXCEPTIONS

In each class there exists one


defined constructor
(explicit or implicit)

Will be invoked only once per


instance

lcl_vehicle
- make
- model
- n_o_vehicles

+ constructor()

CREATE OBJECT
METHODS:
constructor
IMPORTING <InpParam>
EXCEPTIONS <Exception>
RAISING
<class-based exceptions>.

84

(3) lcl_vehicle
Public

Private

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Constructor: Example
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING im_make
TYPE string
im_modell TYPE string.
PRIVATE SECTION.
DATA: make TYPE string, weight TYPE p.
CLASS-DATA n_o_vehicles TYPE i.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
constructor
METHOD constructor.
make
= im_make.
modell = im_modell.
ADD 1 TO n_o_vehicles.
ENDMETHOD.
ENDCLASS.

Automatic call of constructor

DATA r_vehicle TYPE REF TO lcl_vehicles.


...
CREATE OBJECT r_vehicle
EXPORTING im_make
= 'Ferrari'
im_modell = 'F40'.
85

Call from the


main program

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Overview: Static Constructor
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.

CLASS-METHODS: class_constructor.
class_constructor
PRIVATE SECTION.

CLASS-DATA: n_o_vehicles TYPE I.


ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
METHOD class_constructor
class_constructor.
...
ENDMETHOD. ...
ENDCLASS.

Special static method


Call when the class is accessed
for the first time during that
program run

Will be called only once per


program

Implicit constructor call BEFORE


actual processing

CREATE OBJECT r_vehicle


.
r_vehicle.
lcl_vehicle=>n_o_vehicles.
counter = lcl_vehicle=>
n_o_vehicles.

"if attribute is public

lcl_vehicle=>get_count( IMPORTING re_count = counter ).

86

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Fundamentals of OOP
Constructor

87

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming

88

Classes, Attributes and Methods


Objects, Instances of Classes
Visibility / Encapsulation
Constructor
Other fundamental elements

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Namespace inside a class

Same namespace for

89

attributes
methods
events
types
constants
ALIAS names

lcl_vehicle
Public
components
Method
make

STOP

Private
components
Attribute
make

Method
implementation

Inside the body of methods


there exists a local
namespace.

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


The Reference Variable me
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS constructor
IMPORTING im_make
TYPE string
im_modell TYPE string.
PRIVATE SECTION.
DATA make TYPE string. ...
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
METHOD constructor.
DATA make TYPE string VALUE 'Benz'.
CONCATENATE im_make make
me->make
INTO me->make.
ENDMETHOD.

lcl_vehicle
Public
components

Private
components
Attribute
mv_make
Method
implementation
me->make
me->mv_make

METHOD meth.
" inside class prefix 'me->' is not
" necessary for calling methods.
display_attributes( ).
ENDMETHOD.
ENDCLASS.
90

msg systems ag, 15.01.2013

4. Fundamentals of Object Oriented Programming


Summary

You should now be able to


define classes
generate objects
call methods
explain the function of the Garbage Collector

91

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Fundamentals of OOP
Calling a private method

92

msg systems ag, 15.01.2013

5. Inheritance

93

Generalization / Specialization of classes

msg systems ag, 15.01.2013

5. Inheritance
UML Example

lcl_vehicle
-

make
model
ser_no
color
n_o_vehicles

+ constructor()
+ display_attributes()
+ get_count()
is a
relationship

lcl_car

94

lcl_truck

lcl_bus

- car_type
- max_seats
- acceleration

- max_cargo
- n_o_tanks
- n_o_wheels

- max_passengers
- lavatory
- television

+
+ get_type()

+
+ get_cargo()

+
+ get_passengers()

msg systems ag, 15.01.2013

5. Inheritance
Multiple Inheritance?

lcl_super1

Generalization

lcl_super2

STOP

lcl_sub1

95

lcl_sub2

lcl_sub3

Specialization

no
multiple inheritance!

msg systems ag, 15.01.2013

5. Inheritance
Relationships between Super- and Subclasses

Common components are available only


once in superclass

lcl_super

New components in superclass are available


automatically in subclass
Scope of the new coding is reduced
(Programming by Difference)

Subclasses strongly coupled


to superclasses

new
components

96

Inherited
components

White Box Reuse:


Subclass must have detailed knowledge of
the implementation in its superclass.

lcl_sub

msg systems ag, 15.01.2013

5. Inheritance
Syntax
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: estimate_fuel
IMPORTING im_distance
TYPE s_distance
RETURNING value(re_fuel) TYPE ty_fuel.
PRIVATE SECTION.
DATA: make TYPE string,
...
ENDCLASS.

CLASS lcl_truck DEFINITION


INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS: get_cargo RETURNING VALUE(re_cargo) TYPE ty_cargo.

PRIVATE SECTION.
DATA: max_cargo TYPE ty_cargo.
...
ENDCLASS.
97

msg systems ag, 15.01.2013

5. Inheritance
Redefining Methods

CLASS lcl_vehicle DEFINITION.


PUBLIC SECTION.
METHODS: estimate_fuel
IMPORTING im_distance
TYPE ty_distance
RETURNING value(re_fuel) TYPE ty_fuel.
ENDCLASS.
CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS estimate_fuel REDEFINITION
REDEFINITION.
...
Signature must be
ENDCLASS.
retained!
CLASS lcl_truck IMPLEMENTATION.
METHOD estimate_fuel.
estimate_fuel
...
super
super->estimate_fuel(...)
ENDMETHOD.
ENDCLASS.
98

STOP

New implementation of
the inherited method
Refers to the superclass
component

msg systems ag, 15.01.2013

5. Inheritance
Inheritance and Redefinition of the constructor
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING
im_make TYPE string
string.
ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.


METHOD constructor.
make = im_make.
ENDMETHOD.
ENDCLASS.

CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle.


PUBLIC SECTION.
METHODS: constructor IMPORTING im_make TYPE string
ty_cargo
im_cargo TYPE ty_cargo.
PRIVATE SECTION.
DATA: max_cargo TYPE ty_cargo.
Signature may be
ENDCLASS.
changed!
CLASS lcl_truck IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor(
super->constructor im_make = im_make ).
max_cargo = im_cargo.
ENDMETHOD.
Call constructor of
ENDCLASS.
immediate superclass!

99

msg systems ag, 15.01.2013

5. Inheritance
Rules for Calling the Constructor
DATA: r_2 TYPE REF TO lcl_2,
r_3 TYPE REF TO lcl_3.

lcl_1
a1:I

CREATE OBJECT r_2 EXPORTING im_1 = 100.

constructor( im_1:i )

CREATE OBJECT r_3 EXPORTING im_1 = 100


im_2 = 1000.

lcl_2

Case 1
has constructor
Class of instance to be created has
supply its parameters.
Case 2
has no
no
Class of instance to be created has
constructor
Search in the inheritance tree for the next
highest superclass with a constructor and
supply its parameters.

100

lcl_3
a2:I
constructor( im_1:i
im_2:i )
msg systems ag, 15.01.2013

5. Inheritance
Inheritance and Visibility

Public components

101

visible within the class and


within all subclasses

Private components

PUBLIC SECTION.

...

direct access

Protected components

generally visible

CLASS lcl_vehicle DEFINITION.

only visible within the class


no access from outside or
from the subclasses

SECTION.
PROTECTED SECTION
DATA tank TYPE REF TO lcl_tank.
PRIVATE SECTION.
DATA make TYPE string.
ENDCLASS.

lcl_vehicle
- make
- model
# tank

+ public
# protected
- private
+ constructor() static
+ display_attributes()
+ get_count()

msg systems ag, 15.01.2013

5. Inheritance
Visibility of Protected Components

Addressable
for all clients
from the outside!

(5) lcl_bus
Public

Private

Geerbt
get_make()
set_make()
get_count()
display_attributes()
estimate_fuel()

Protected

Inherited
tank
Addressable
only inside the class

102

msg systems ag, 15.01.2013

5. Inheritance
Rules for Redefinition of Methods

Inherited methods can be redefined in subclasses.


Redefined methods must be newly implemented in subclasses
The signature of redefined methods cannot be changed
Static methods cannot be redefined

Static components will be distributed by inheritance: A class can be


divided into its non-private static attributes with all its subclasses

lcl_vehicle
+ estimate_fuel()

lcl_car
+ estimate_fuel()

lcl_truck
+ estimate_fuel()

lcl_bus
+ estimate_fuel()

The method will be implemented in different ways!


103

msg systems ag, 15.01.2013

5. Inheritance
Redefinition for Methods: Example

lcl_bus
- max_passengers

- max_cargo

+ constructor()
+ estimate_fuel()

+ constructor()
+ estimate_fuel()

METHOD estimate_fuel.
DATA: weight ...
weight = empty_load
average_weight.
+ max_passengers * average_weight
re_fuel = weight * im_distance
* factor.
ENDMETHOD.

104

lcl_truck

METHOD estimate_fuel.
DATA: weight ...
weight = empty_load + max_cargo
max_cargo.
re_fuel = weight * im_distance
* factor.
ENDMETHOD.

msg systems ag, 15.01.2013

5. Inheritance Summary

You should now be able to


define an inheritance relation between classes
redefine methods

105

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Inheritance
Class Hierarchies

106

msg systems ag, 15.01.2013

6. Casting

107

Upcast
Downcast
Inheritance and Polymorphism

msg systems ag, 15.01.2013

6. Casting
Types of Casts

lcl_vehicle
-

Upcast

lcl_car

108

make
model
ser_no
color
n_o_vehicles

+ constructor()
+ display_attributes()
+ get_count()

lcl_truck

Downcast

lcl_bus

- car_type
- max_seats
- acceleration

- max_cargo
- n_o_tanks
- n_o_wheels

- max_passengers
- lavatory
- television

+
+ get_type()

+
+ get_cargo()

+
+ get_passengers()

msg systems ag, 15.01.2013

6. Casting
Upcast Principle
DATA: r_vehicle TYPE REF TO lcl_vehicle,
r_truck
TYPE REF TO lcl_truck.

CREATE OBJECT r_truck.

(5) lcl_truck
Public

r_truck
r_vehicle

Inherited
Geerbt
get_make()
get_count()

Redefined
Redefiniert
* Upcast
r_vehicle = r_truck.
r_truck

r_vehicle

109

display_attributes()
set_attributes()
estimate_fuel()
get_cargo()

msg systems ag, 15.01.2013

6. Casting
Compatibility und Upcast

An instance of a subclass can be expected in any context, in which the instance


of a superclass can be used

Inherited components are addressable from the superclass

Generic access of the client!

DATA: r_vehicle
r_truck
level

TYPE REF TO lcl_vehicle,


TYPE REF TO lcl_truck,
TYPE
ty_level.

* Access via the subclass


level = r_truck->estimate_fuel(
1000 ).
->
* Upcast
r_vehicle = r_truck.
* Access via the superclass
level = r_vehicle->estimate_fuel(
1000 ).
r_vehicle

110

msg systems ag, 15.01.2013

6. Casting
Generic Access?
Client
1

1..*

lcl_rental

(1) lcl_rental
Public

(1) lcl_car

lcl_vehicles

lcl_truck

lcl_bus

Private

itab

(4) lcl_bus

(5) lcl_truck

(2) lcl_car

111

msg systems ag, 15.01.2013

6. Casting
Preparation for Generic Access (1)
Client

(2) lcl_bus

(1) lcl_rental
Public

Private
vehicle_list
(3) lcl_truck

add_vehicle()

(4) lcl_car

Up Cast when
handing over!

...
METHODS: add_vehicle
IMPORTING
im_vehicle TYPE REF TO lcl_vehicle.
...
112

(5) lcl_car

msg systems ag, 15.01.2013

6. Casting
Preparation for Generic Access(2)

CLASS lcl_rental IMPLEMENTATION.


METHOD add_vehicle.
APPEND im_vehicle TO vehicle_list.
ENDMETHOD.

(2) lcl_bus

vehicle_list

METHOD calc_estimated_fuel.
DATA:
r_vehicle TYPE REF TO lcl_vehicle.
re_fuel = 0.
LOOP AT vehicle_list
INTO r_vehicle.
re_fuel = re_fuel
+ r_vehicle->estimate_fuel(
im_distance ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.

113

(3) lcl_bus

lcl_bus
estimate_fuel()

(4) lcl_truck

(5) lcl_truck

lcl_truck
estimate_fuel()

msg systems ag, 15.01.2013

6. Casting
Polymorphism Generic Access

METHOD calc_estimated_fuel.
DATA: r_vehicle TYPE REF TO lcl_vehicle.
LOOP AT vehicle_list INTO r_vehicle.
ADD r_vehicle->estimate_fuel
r_vehicle->estimate_fuel( im_distance ) TO re_fuel.
ENDLOOP.
ENDMETHOD.

(5) lcl_truck
Public
Inherited/ redefined

estimate_fuel()

vehicle_list

(2) lcl_bus
Public

Inherited / redefined

estimate_fuel()

114

METHOD estimate_fuel
estimate_fuel.
total_weight = max_cargo + weight.
re_fuel = total_weight
* im_distance * factor.
ENDMETHOD.
METHOD estimate_fuel
estimate_fuel.
total_weight = max_passengers
* average_weight + weight.
re_fuel = total_weight
* im_distance * factor.
ENDMETHOD.
msg systems ag, 15.01.2013

6. Casting
Static and dynamic Type of a Reference

Static Type of a reference variable

DATA: r_vehicle TYPE


REF TO lcl_vehicle.

is defined using TYPE REF TO


remains the same through the program flow
defines which attributes and methods can be
addressed

Dynamic Type of a reference variable

(5) lcl_truck
Public
IInherited /
redefinied
estimate_fuel()

is determined by the assignment


can change during the program run

Private

estimate_fuel()

defines which implementations are carried out


for inherited methods

(2) lcl_bus
Public
Inherited /
redefinied
estimate_fuel()

Private

r_vehicle
estimate_fuel()

115

msg systems ag, 15.01.2013

6. Casting
Comparison to Procedural Programming
vehicle_list
name category

New vehicle category: truck


Procedural

Object Oriented

Extension e.g. of CASEConstructions

truck1

No change of coding needed. bus1

bus2
TYPE TABLE OF vehicle_list_type,
truck2
TYPE vehicle_list_type, ...
truck3

DATA: vehicle_list
vehicle
...
van1
LOOP AT vehicle_list INTO vehicle.
CASE vehicle-category.
WHEN 'TRUCK'.
PERFORM estimate_fuel_truck ...
WHEN 'BUS'.
ADD r_vehicle->estimate_fuel( )
PERFORM estimate_fuel_bus ...
TO needed_fuel.
WHEN 'VAN'.
estimate_fuel_van...
PERFORM estimate_fuel_van
...
ENDCASE.
ADD fuel TO needed_fuel.
ENDLOOP.

116

msg systems ag, 15.01.2013

6. Casting

117

Upcast
Downcast
Inheritance and Polymorphism

msg systems ag, 15.01.2013

6. Casting
Motivation for Downcast
Client

(2) lcl_bus

(1) lcl_rental
Public

(3) lcl_truck

max_cargo
= 30

Private
vehicle_list

get_max_cargo()

(4) lcl_car

(5) lcl_truck

max_cargo
= 50

METHOD get_max_cargo.
DATA: r_vehicle TYPE REF TO lcl_vehicle.
LOOP AT vehicle_list INTO r_vehicle.
" Problem: is this really a truck?
" Determine the max cargo
ENDLOOP.
ENDMETHOD.
118

Truck or other
vehicle?

msg systems ag, 15.01.2013

6. Casting
Example for Downcast
(2) lcl_bus

(1) lcl_rental
Public

Private
vehicle_
list

(3) lcl_truck

max_cargo
= 30

get_max_cargo()
(4) lcl_car

(5) lcl_truck

METHOD get_max_cargo.
DATA: r_vehicle TYPE REF TO lcl_vehicle,
r_truck TYPE REF TO lcl_truck.
LOOP AT vehicle_list INTO r_vehicle.
TRY.
r_truck ?= r_vehicle.
" put max cargo in variable re_cargo...
CATCH cx_sy_move_cast_error
cx_sy_move_cast_error.
" react on that cast error
ENDTRY.
ENDLOOP.
ENDMETHOD.
119

max_cargo
= 50

Try and catch


possible errors.

msg systems ag, 15.01.2013

6. Casting

120

Upcast
Downcast
Inheritance and Polymorphism

msg systems ag, 15.01.2013

6. Casting
Semantic and Use of Inheritance

Rules

121

Redefined methods must have a similar behavior as in the


superclass.

Any change of the signature (interface) is not possible.

Use of inheritance:

Generalization/specialization for better structuring of


software

Polymorphism generic programming


Omission of CASE-structures

msg systems ag, 15.01.2013

6. Casting
Semantic and Use of Inheritance

Rules

Inheritance is frequently misused:

For pure code reuse

Instead of an additional attribute/ aggregation /


role concepts
car

car_red

car_blue

Use of inheritance does not correspond in some cases


to the expectations from the real world
change_width
Superclass
Rectangular

122

change_height

Subclass
square
msg systems ag, 15.01.2013

6. Casting Summary

You should now be able to


use casts
use casts assignments with inheritance to develop generic
programs

123

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Casting
Polymorphism (Part 1)

124

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Casting
Polymorphism (Part 2)

125

msg systems ag, 15.01.2013

7. Interfaces

126

Interfaces
Working with Interfaces
Compound Interfaces
Polymorphism

msg systems ag, 15.01.2013

7. Interfaces
Motivation for Interfaces

Service 1
Service 2
Service 3
...

Interface

Shared components Services


lcl_carrier

lcl_hotel

lcl_rental

lcl_airplane

...

127

lcl_vehicles

...

...

...

msg systems ag, 15.01.2013

7. Interfaces
Services of the Interfaces

lcl_travel_agency

display_partner
check_availability
...

Interface

describes and
uses

Shared components Services


lcl_carrier

lcl_hotel

lcl_rental

lcl_airplane

...

128

lcl_vehicles

...

...

...

msg systems ag, 15.01.2013

7. Interfaces
UML Example

lcl_travel_agency

Client

uses

Implementation
Implementationof
more
than one
mehrerer
Interface possible!
Interfaces
mglich!

interface

interface

lif_partners

lif_license

implements

lcl_carrier

lcl_hotel

lcl_rental

lcl_airplane

...

129

lcl_vehicles

...

...

...

msg systems ag, 15.01.2013

7. Interfaces
Definition and Implementation of an Interface

Definition of the interface

Inclusion of the interface


in the server classes

130

Implementation is done in
classes
No visibility layers in
Interfaces; all
components are public

lif_partners
INTERFACE lif_partners.
METHODS: display_partner.
ENDINTERFACE.

CLASS lcl_rental DEFINITION.


PUBLIC SECTION.
INTERFACES lif_partners
lif_partners.
ENDCLASS.

CLASS lcl_rental IMPLEMENTATION.


METHOD lif_partners~display_partner .
"just call existing method that fits
display_attributes( ).
...
ENDMETHOD.
Interface
resolution
ENDCLASS.
operator
msg systems ag, 15.01.2013

7. Interfaces
Characteristics of Interfaces

Separation of signature (interface) and implementation (class)


Client defines protocol, server implements is
Black Box Principle: Client knows only the interface,
not the implementation
loosely coupling between client and server

Polymorphism

Code 2

Generic handling with object from different classes

display_partner
Code 1

Abstraction

Code 3

Code 4

Interfaces as generalizations of the implementing classes

super1

Simulation of multiple inheritance


sub1

131

interf

sub2

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Interfaces
Definition of an Interface

132

msg systems ag, 15.01.2013

7. Interfaces

133

Interfaces
Working with Interfaces
Compound Interfaces
Polymorphism

msg systems ag, 15.01.2013

7. Interfaces
Working with Interface Components
CLASS lcl_rental IMPLEMENTATION.
...
METHOD lif_partners~display_partner.
"special coding to display partners or
"just calling existing method that fits
display_attributes( ).
ENDMETHOD.

interface

lif_partners
display_partner()
check_availability()

ENDCLASS.

implements
DATA: r_rental TYPE REF TO lcl_rental.
...
CREATE OBJECT r_rental.
* syntactically possible but not useful:
~
r_rental->lif_partners~display_partner(
).

Interface resolution operator!

134

lcl_rental
- name
- vehicle_list

+ add_vehicle()
+ display_attributes()

msg systems ag, 15.01.2013

7. Interfaces
Upcast

interface
Upcast

lif_partners
Downcast

lcl_carrier

lcl_hotel

DATA: r_rental TYPE REF TO lcl_rental,


r_lif
TYPE REF TO lif_partners.
CREATE OBJECT r_rental.

lcl_rental

(1) lcl_rental
ffentlich

r_rental

r_lif
* Up Cast
r_lif = r_rental

Private

Interface
components
display_partner
check_availability
...

r_lif
135

msg systems ag, 15.01.2013

7. Interfaces
Use of Interfaces

(9) lcl_travel_agency
Public

(7) lcl_hotel

Private
partner_list

add_partner()

(1) lcl_rental

(6) lcl_carrier

Up Cast at
handing over!
...
lif_partner
METHODS: add_partner IMPORTING im_partner TYPE REF TO lif_partner.
...
136

msg systems ag, 15.01.2013

7. Interfaces
Use of Interfaces
METHOD display_agency_partners.
DATA: r_partner TYPE REF TO lif_partners.
LOOP AT partner_list INTO r_partner.
r_partner->display_partner( ).
ENDLOOP.
ENDMETHOD.
(7) lcl_hotel

partner_list

(1) lcl_rental

(6) lcl_carrier

137

METHOD ...~display_partner
display_partner .
display_hotel_attributes( ). ...
ENDMETHOD.

METHOD ...~display_partner
display_partner .
display_rental_data( ). ...
ENDMETHOD.

display_partner
METHOD ...~display_partner.
display_attributes( ). ...
ENDMETHOD.
msg systems ag, 15.01.2013

7. Interfaces
Interface References: Down Cast
(3) lcl_travel_agency
book_flight()

partner_list

r_carrier

(7) lcl_hotel

METHOD book_flight.
DATA: r_carrier TYPE REF TO lcl_carrier,

r_partner TYPE REF TO lcl_partner.


LOOP AT partner_list INTO r_partner.
TRY.
r_carrier ?= r_partner.
r_carrier->... "Perform necessary operations.
CATCH cx_sy_move_cast_error.
cx_sy_move_cast_error
"react on that cast error
ENDTRY.
ENDLOOP.
ENDMETHOD.
138

(1) lcl_rental

(6) lcl_carrier

msg systems ag, 15.01.2013

7. Interfaces

139

Interfaces
Working with Interfaces
Compound Interfaces
Polymorphism

msg systems ag, 15.01.2013

7. Interfaces
Compound Interfaces

Problem:
Extension of
the Interface?

interface

lif_partners

book_room()

implements

lcl_hotel

140

lcl_carrier

lcl_rental

msg systems ag, 15.01.2013

7. Interfaces
Compound Interfaces
Solution:
Compound
Interfaces
interface

lif_partners
implements

interfaces:
interface

lif_room_booking
implements

lcl_hotel

141

lcl_carrier

lcl_rental

msg systems ag, 15.01.2013

7. Interfaces
Compound Interfaces: Example
INTERFACE lif_partners.
METHODS display_partner.
ENDINTERFACE.

INTERFACE lif_room_booking.
INTERFACES lif_partners.
METHODS book_room.
ENDINTERFACE.

DATA: i_partner
i_room_book

CLASS lcl_hotel DEFINITION.


PUBLIC SECTION.
INTERFACES lif_room_booking.
ENDCLASS.
CLASS lcl_hotel IMPLEMENTATION.
METHOD lif_partner~display_partner.
ENDMETHOD.
METHOD lif_room_booking~book_room.
ENDMETHOD.
ENDCLASS.

TYPE REF TO lif_partners,


TYPE REF TO lif_room_booking,

...
i_partner = i_room_book.

"Narrowing Cast

i_room_book->lif_partner~display_partner( ).
* i_partner->display_partner( ) also possible
i_room_book ?= i_partner.
142

"Widening Cast
msg systems ag, 15.01.2013

7. Interfaces Summary

You should now be able to


define and implement
develop generic programs using polymorphism and
interfaces

143

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Interfaces
Use of Interfaces

144

msg systems ag, 15.01.2013

8. Events

145

Define and trigger events


Register and handle events

msg systems ag, 15.01.2013

8. Events Overview

Recipient / Handler
* main program
CREATE OBJECT r_car EXPORTING
Car rental company

Auto

Registration office

Sender
Event: vehicle_created

146

...

msg systems ag, 15.01.2013

8. Events
Properties

Looser coupling than using method invocation

Trigger object announces a change of state


Handler objects can register to event

Another communication model


Sender is not aware of the receiver;
the reaction of the receiver is of no interest for the sender.

Application

GUI implementation
Conformity to other object models:
COM, ActiveX Controls, OpenDoc ...

147

msg systems ag, 15.01.2013

8. Events
Triggering and Handling Events: Overview

Rules

Class defines an event


(EVENTS, CLASS-EVENTS)

Object or class triggers the event


(RAISE EVENT)

148

Trigger events

Handle events

Handler class defines and implements the handler method


([CLASS-]METHODS... FOR EVENT ... OF ...)

Handler object or handler class is registered to events of


specific objects/ classes at runtime
(SET HANDLER)

msg systems ag, 15.01.2013

8. Events
Defining and Triggering Events: Syntax
CLASS <classname> DEFINITION.
EVENTS: <event> EXPORTING VALUE(<ex_par>)
EVENTS
VALUE(
) TYPE <type>.
CLASS <classname> IMPLEMENTATION.
Typing of all parameters
METHOD <methodname>.
RAISE EVENT <event> EXPORTING <ex_par> = <act_par>.
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING ... .
1.
EVENTS vehicle_created.
...
ENDCLASS.

car

vehicle_created

CLASS lcl_vehicle IMPLEMENTATION.


METHOD constructor.
...
RAISE EVENT vehicle_created
2.
ENDMETHOD.
ENDCLASS.
149

msg systems ag, 15.01.2013

8. Events
Event handling and registering

Sender
car1

Handler
rental
vehicle_created

registration
office

car2
vehicle_created

carrier

truck

150

vehicle_created

msg systems ag, 15.01.2013

8. Events
Handling method
CLASS <class_handle> DEFINITION.
No Typing, only enumeration
METHODS: <on_event> FOR EVENT <event>
METHODS
OF <class/interfacename>
IMPORTING <ex_par1> ... <ex_parN> SENDER.
SENDER
CLASS lcl_rental DEFINITION.
...
PRIVATE SECTION.
METHODS: add_vehicle FOR EVENT vehicle_created OF lcl_vehicle
3.
IMPORTING SENDER.
SENDER
ENDCLASS.

rental

car1

add_vehicle
vehicle_created

151

msg systems ag, 15.01.2013

8. Events
Registering Event Handling: Syntax
SET HANDLER <ref_handle>-><on_event>
FOR <ref_sender> | FOR ALL INSTANCES
[ACTIVATION <var>].

CLASS lcl_rental DEFINITION.


...
PRIVATE SECTION.
METHODS: add_vehicle FOR EVENT vehicle_created OF ...
ENDCLASS.
CLASS lcl_rental IMPLEMENTATION.
METHOD constructor.
SET HANDLER add_vehicle FOR ALL INSTANCES
4.
INSTANCES.
ENDMETHOD.
METHOD ...
ENDCLASS.

car1

SET HANDLER

rental
add_vehicle

vehicle_created

152

msg systems ag, 15.01.2013

8. Events
Registration/Deregistration: Handler Tables

Handler table of car2


r_car
vehicle_created (Event)

car2

handling
method
add_vehicle
reg_vehicle

registered
object

registration
office

Handler table of car1


r_car
vehicle_created (Event)

car1

153

handling
method
add_vehicle
reg_vehicle

rental

registered
object
registration
office

msg systems ag, 15.01.2013

8. Events
Event Handling: Properties

Event handling is performed sequentially.


The order od invocation of handler methods is not defined.
Registered objects wont be erased by the Garbage Collector.
The visibility of an event sets who can handle an Event.
The visibility of a handler method sets who can use a SETHANDLER-instruction.
A handler method can have even though only the same or a more
restrictive visibility than the event related to it.

Event
Public
Private

154

Handler
Private
Public

msg systems ag, 15.01.2013

8. Events
Summary

You should now be able to


define and trigger events
handle events
register and deregister event handling
explain the key differences between explicit method calls
and event-controlled method calls

155

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Event Triggering
and Handling

156

msg systems ag, 15.01.2013

9. Global Classes/Interfaces

157

The Class Builder


Working with Global Classes/Interfaces
Application Example: ALV Grid Control

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Rewind: Local Classes/Interfaces
Program-1

Program-2

REPORT prog1.

REPORT prog2.

CLASS lcl_airplane DEFINITION.


...
ENDCLASS.
...

DATA: r_airplane
TYPE REF TO lcl_airplane
lcl_airplane.
...

Local Program Classes

158

Local classes are visible only in the program, in which they are defined.
No global access possible
Not created in Repository, no Where-Used List etc.

msg systems ag, 15.01.2013

9. Global Classes/Interfaces

Classes/Interfaces in ABAP-Repository (= Global classes)

Access from any program possible via TYPE REF TO

Client namespace for global classes and Interfaces:


Y*, Z* or a special customer namespace

Where-Used List is possible

Development tool in the ABAP-Workbench: Class Builder


Tool for creation, testing and maintenance of
global classes and interfaces
Takes on the creation of the whole framework coding
e.g. CLASS <name> DEFINITION
Manages the Include-Data, in which the code is stored
Graphical Editor

159

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Global Classes in SE80 Created in the Object Navigator

Creation of a Class using the


Object Navigator:

160

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Attributes

Defining the attributes of a class

161

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Methods
Redefinition
Undo Redefinition

Set the
signature parameter of the
selected method,
Here: CONSTRUCTOR

Defining methods of a class

162

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Definition of Method Signatures

Back to method list

Set the
signature parameter

163

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Exception Interface

Back to method list

Set the
possible exceptions

164

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Defining Interfaces
1) Declaring the global
interfaces in a global
class

2)

165

Implementation of methods
from a global class

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Implementation of Methods
Signature displayed to help
with the implementation of the
method

166

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Inheritance

Make
superclass
known

167

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: Testing Environment
Test
class

Create
instance

Call
methods

168

msg systems ag, 15.01.2013

9. Global Classes/Interfaces

169

The Class Builder


Working with Global Classes/Interfaces
Application Example: ALV Grid Control

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Global Classes in the Object Navigator

Right: Editing an ABAP Program in


the Editor

Left: Navigation Tree

170

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: CREATE OBJECT

Drag&Drop of the constructor or


the class name, generates
CREATE OBJECT.

Representation of a class in the


Navigation Tree enables the access
to the class components
171

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Class Builder: CALL METHOD

Drag&Drop of a method
generates a method call.

172

msg systems ag, 15.01.2013

9. Global Classes/Interfaces

173

The Class Builder


Working with Global Classes/Interfaces
Application Example: ALV Grid Control

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
The ALV Grid Control
Details

Sort

Filter

Search

Print

Sum

Chart

Export

Views
(Table, Grid, Excel)

CL_GUI_ALV_GRID

174

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Integrating a Grid-Control instance in a dialog program
For using the ALV Grid
Control, two classes are
needed.

Dynpro
Area
(1) cl_gui_custom_container
Public

Custom
Container
Control

Private

(3) cl_gui_alv_grid
Public

Private

ALV
Grid
Control

175

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Displaying data in an ALV-Grid
...
cl_gui_custom_container
DATA: go_container TYPE REF TO cl_gui_custom_container,
go_grid
TYPE REF TO cl_gui_alv_grid .
...
CREATE OBJECT go_container
EXPORTING container_name = 'CONTAINER_1'
...
CREATE OBJECT go_grid
EXPORTING i_parent = go_container
...

(1) cl_gui_custom...
Public

Private

(1) cl_gui_alv_grid
Public

Private

go_grid->set_table_for_first_display(
set_table_for_first_display
i_structure_name = 'SPFLI'
it_outtab
= gt_flights).
...

176

msg systems ag, 15.01.2013

9. Global Classes/Interfaces
Summary

You should now be able to:


describe the difference between local and global
classes/interfaces
create global classes/interfaces using the Class Builder

177

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Global Classes
Creating a Class

178

msg systems ag, 15.01.2013

10. Programming Techniques

179

Abstract and Final Classes


Visibility of Constructors
Strings
Friendship
Persistent Objects

msg systems ag, 15.01.2013

10. Programming Techniques


Abstract Classes

Abstract Classes cannot be instantiated themselves (but their


subclasses are)
References to such abstract classes can therefore be used for polymorphic
access to subclass instances.

Abstract (instance) methods are defined in a class, but not


implemented
They have to be redefined in the subclasses

CLASS lcl_vehicle DEFINITION ABSTRACT.


ABSTRACT
PUBLIC SECTION.
METHODS:
estimate_fuel_consumption ABSTRACT
IMPORTING ...
ENDCLASS.

Class cannot be
instantiated

Method is not
implemented in this
class

180

msg systems ag, 15.01.2013

10. Programming Techniques


Final Classes Final Methods

Final classes cannot have subclasses.

CLASS lcl_truck DEFINITION FINAL


INHERITING FROM lcl_vehicle.
...
ENDCLASS.

lcl_vehicle

lcl_truck

...

lcl_bus

...

...

Final methods cannot be redefined in subclasses.

CLASS lcl_bus DEFINITION INHERITING FROM lcl_vehicle.


PUBLIC SECTION.
METHODS:
FINAL
estimate_number_of_free_seats FINAL.
ENDCLASS.

181

msg systems ag, 15.01.2013

10. Programming Techniques

182

Abstract and Final Classes


Visibility of Constructors
Strings
Friendship
Persistent Objects

msg systems ag, 15.01.2013

10. Programming Techniques


Issue with multiple instantiation

Function 1

Function 3

(1) lcl_daten
Public

Private

Change

STOP

(2) lcl_daten
Change

Function 2

183

Public

Private

Function 4

msg systems ag, 15.01.2013

10. Programming Techniques


Who can instantiate classes?

The instantiation of classes can be restricted:


CLASS cl_class DEFINITION ...

CREATE PUBLIC
Each user (Client) can create class instances.
(Default)

CREATE PROTECTED
Only the class itself and all its subclasses can create instances of this
class.

CREATE PRIVATE
Only the class itself can create instances of itself..

184

Thus, the visibility of the constructor is


implicitly controlled.

Possible use:
Singleton design pattern

(1) lcl_singleton
Public

Private
mo_singl

get_instance

Client

msg systems ag, 15.01.2013

10. Programming Techniques


Singleton

Two possibilities to develop a Singleton:

lazy creation
Insantiating on first access

eager creation
Generation in the class constructor

lcl_singleton

lcl_singleton

- mo_single:lcl_singleton
+ get_instance():lcl_singleton

- mo_single:lcl_singleton
+ get_instance():lcl_singleton
+ class_constructor()

METHOD get_instance.
IF mo_single IS INITIAL.
CREATE OBJECT mo_single.
ENDIF.
ro_single = mo_single.
ENDMETHOD.

185

METHOD class_constructor.
CREATE OBJECT mo_single.
ENDMETHOD.

METHOD get_instance.
ro_single = mo_single.
ENDMETHOD.

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Programming Techniques
Singleton

186

msg systems ag, 15.01.2013

10. Programming Techniques

187

Abstract and Final Classes


Visibility of Constructors
Strings
Friendship
Persistent Objects

msg systems ag, 15.01.2013

10. Programming Techniques


ABAP-Strings
DATA: lv_str TYPE c LENGTH 20.
lv_str = 'Hello World!'.

188

!'.

DATA: lv_str TYPE string.


lv_str = `Hello World!`.

!`.

" Truncated (not wide enough!)


CONCATENATE lv_str lv_str
INTO lv_str.

" No truncation
lv_str = lv_str && lv_str.

" Syntax error (not wide enough!)


lv_str = lv_str+10(15).

" No syntax- but runtime error


lv_str = lv_str+10(15).

" Ugly: * no localization,


"
* space before '.',
"
* lengthy, error-prone.
lv_str = sy-datum.
CONCATENATE
'Today is ' lv_str '.'
INTO lv_str
SEPARATED BY space.
CALL METHOD lo->meth( lv_str ).

" Pretty: *
"
*
"
*
"
*
"
CALL METHOD
|Today is

localization,
space before '.',
short,
directly usable
in method call
lo_meth(
{ sy-datum
DATE = USER }.| ).

msg systems ag, 15.01.2013

10. Programming Techniques

189

Abstract and Final Classes


Visibility of Constructors
Strings
Friendship
Persistent Objects

msg systems ag, 15.01.2013

10. Programming Techniques


Friendship

In some cases, classes have to work together so closely that one or


both classes need access to the others protected and private
components.

High-performance direct access to the data in a friendship-grating class


Distribution of various services to common data over different classes
Formation of packages

Package
Friend
Grants friendship
(5) lcl_flight_data
Private
flight_list

190

(3) lcl_flight_factory
Public

Friend

(4) lcl_flight_planer
Public

create_flight()
get_flight()
plan_flight()
delete_flight()
schedule()

msg systems ag, 15.01.2013

10. Programming Techniques

191

Abstract and Final Classes


Visibility of Constructors
Strings
Friendship
Persistent Objects

msg systems ag, 15.01.2013

10. Programming Techniques


Persistence Services

ABAP Program

Transient Objects

Persistence Service
Read / write objects

Database

Persistent data in
transparent tables

192

msg systems ag, 15.01.2013

10. Programming Techniques


Characteristics of Persistence Services

The Objects of a running ABAP program are transient.

The Persistence Services allows working with persistent objects.

Persistent classes are created


in the class builder

Tasks of Persistence Services:

193

Loading objects from the database


Managing object changes
Saving objects in the database

msg systems ag, 15.01.2013

10. Programming Techniques


The Class Agent

Within the Persistence Services, the Class Agent takes over the
management of the persistent objects.

The actor provides several services (method calls), for managing the objects and
the encapsulated object data

The class actor is a singleton instance, addressed with the public static attribute
agent.

...
DATA: r_carrier TYPE REF TO cl_carrier,
r_agent
TYPE REF TO ca_carrier,
carrname TYPE
s_carrname.

TRY.

The agent (actor), a


singleton of the class
CA_CARRIER and
friend of the persistent
classCL_CARRIER

agent
r_agent = ca_carrier=>agent.
r_carrier = r_agent->get_persistent( i_carrid = 'LH' ).
carrname
= r_carrier->get_carrname( ).
WRITE: 'LH: ', carrname.
CATCH cx_os_object_not_found.
ENDTRY.
194

msg systems ag, 15.01.2013

10. Programming Techniques


Summary

You should now be able to


Create and describe abstract and final classes
Describe the visibility of the constructors
Define friend relationships between classes
Describe the Persistence Services

195

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Programming Techniques
Friendship

196

msg systems ag, 15.01.2013

11. Exception Handling

197

Predefined and Selfdefined Class-based Exceptions


Exception raising, handling and propagating

msg systems ag, 15.01.2013

11. Exception Handling


Class-Based Exceptions Sequence Diagram

:lcl_driver

:lcl_car

:lcl_tank

get_fuel_level( )
get_fuel_level( )
break
[Fill level
sensor
failed ]

exception

sensor_failed
alert( )
re_level

re_level

198

msg systems ag, 15.01.2013

11. Exception Handling


Class-Based Exceptions Sequence Diagram (Propagation)

:lcl_driver

:lcl_car

:lcl_tank

get_fuel_level( )
get_fuel_level( )
break
[Fill level
sensor
failed ]

exception

sensor_failed
pit_stop( )
re_level
re_level

199

msg systems ag, 15.01.2013

11. Exception Handling


Class-Based Exceptions Overview
zcx_exception

Exception class
(predefined or
your own)

ABAP Program

...

RAISE EXCEPTION TYPE cx_exception


[EXPORTING attr1 = ...
attr2 = ...].
...
* Exception handling
...

ABAP Runtime System


200

Exception raised
using the ABAP
statement or by the
runtime environment
msg systems ag, 15.01.2013

11. Exception Handling


Handling Exceptions
TRY.

" Perform normal operation


...
CATCH cx_...

cx_...

...

[INTO lx_exc1].

...
CATCH cx_...

... [INTO lx_exc2].

...

Statements for which


exceptions are to be
handled

Behandler
Handlers fr
fordie
the
angegebenen
specified exception
Ausnahmeklassen
classes and their
und
deren
subclasses
Unterklassen

CATCH cx_root [INTO lx_root].


...

Handlers for all


exception objects of
other classes

CLEANUP.
...

ENDTRY.

201

Clean up, if there is no


exception handler
available

msg systems ag, 15.01.2013

11. Exception Handling


Exception Classes: The Inheritance Hierarchy

cx_root
- textid
- kernel_errid
...
+ get_text()
+ get_source_position()

cx_no_check

cx_dynamic_check

cx_sy_arithmetic_error

cx_static_check

cx_sy_move_cast_error

cx_sy_arithmetic_overflow
202

msg systems ag, 15.01.2013

11. Exception Handling


Example: Handling a Predefined Exception
PARAMETERS: int1
int2

TYPE i,
TYPE i.

DATA: result TYPE i,


lx_exc TYPE REF TO cx_root.

If an overflow error occurs, the


runtime system raises the
cx_sy_arithmetic_overflow
exception

...
TRY.
result = int1 * int2.
WRITE result.
CATCH cx_sy_arithmetic_overflow INTO lx_exc.
MESSAGE lx_exc TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.

...

203

msg systems ag, 15.01.2013

11. Exception Handling


Example: Using your own exceptions
CLASS lcl_airplane DEFINITION.
...
METHODS get_technical_attributes
IMPORTING im_type
TYPE saplane-planetype
EXPORTING ex_weight TYPE s_plan_wei
ex_tankcap TYPE s_capacity.
...
ENDCLASS.
CLASS lcl_airplane IMPLEMENTATION.
...
METHOD get_technical_attributes.
SELECT SINGLE weight tankcap FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc <> 0.
ex_weight = 100000.
ex_tankcap = 10000.
ENDIF.
Program adaptation: If there
ENDMETHOD.
...
ENDCLASS.
204

are no table entries raise


and handle own exception

msg systems ag, 15.01.2013

11. Exception Handling


Creating Exception Classes

205

msg systems ag, 15.01.2013

11. Exception Handling


Define your own Attributes and Exception Texts

Your own attributes


provide additional
information
Exception texts
describe exception
in more detail

206

msg systems ag, 15.01.2013

11. Exception Handling


Raising and Handling selfdefined exceptions
METHOD get_technical_attributes.
DATA: lx_exc TYPE REF TO cx_root,
text
TYPE string.

Example:
im_type = 727-301

TRY.
SELECT SINGLE weight tankcap FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE zcx_wrong_planetype
EXPORTING planetype = im_type.
ENDIF.
" Operate on valid values
" in ex_weight and ex_tankcap
CATCH zcx_wrong_planetype INTO lx_exc.
text = lx_exc->get_text( ).
MESSAGE text TYPE 'I'.
ENDTRY.
ENDMETHOD.

207

msg systems ag, 15.01.2013

11. Exception Handling


Propagating Exceptions
CLASS class DEFINITION.
...
METHODS meth IMPORTING
EXPORTING
RAISING
...
ENDCLASS.

<im_parameter>
<ex_parameter>
cx_exception
cx_exception.

REPORT propagate_exceptions.
...
DATA r_obj TYPE REF TO class.
...

CLASS class IMPLEMENTATION.


...
METHOD meth.
...
cx_exception
RAISE EXCEPTION TYPE cx_exception.
ENDMETHOD.
ENDCLASS.

TRY.
r_obj->meth( EXPORTING ...
IMPORTING ... ).
CATCH cx_exception .
...

Exceptions that occur in


procedures do not necessarily
need to be handled there; they
can be passed along to the
calling program.

ENDTRY.

208

msg systems ag, 15.01.2013

11. Exception Handling


Taking up exceptions in the method signature

209

msg systems ag, 15.01.2013

11. Exception Handling


Resumable Exceptions Sequence Diagram

:lcl_driver

:lcl_car

:lcl_tank

get_fuel_level( )

get_fuel_level( )
break
[ Fill level
sensor
failed ]

re_level

210

resumable_exception
sensor_failed
RESUME ...

re_level

msg systems ag, 15.01.2013

11. Exception Handling


Example: Propagating Exceptions
CLASS lcl_airplane DEFINITION.
METHODS display_attributes.
METHODS get_technical_attributes
IMPORTING im_type
TYPE saplane-planetype
EXPORTING ex_weight
TYPE s_plan_wei
ex_tankcap
TYPE s_capacity
RAISING
zcx_wrong_planetype .
ENDCLASS.

METHOD display_attributes.
DATA: lx_exc TYPE REF TO cx_root,
text
TYPE string.
...
TRY.
get_technical_attributes(
EXPORTING im_type = planetype
IMPORTING ex_weight = weight
ex_tankcap = cap ).
WRITE: /'Gewicht:'(003), weight,
'Tankkapazitt:'(004), cap.
CATCH zcx_wrong_planetype INTO lx_exc
lx_exc.
MESSAGE lx_exc TYPE 'I'.
ENDTRY.
ENDMETHOD.
211

METHOD get_technical_attributes.

SELECT SINGLE weight tankcap


FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc <> 0.

RAISE EXCEPTION
TYPE zcx_wrong_planetype
im_type
EXPORTING pl_type = im_type.
ENDIF.
ENDMETHOD.
msg systems ag, 15.01.2013

11. Exception Handling


Declarable Exceptions

cx_root

cx_no_check

212

These exceptions can be


handled, otherwise they
are forwarded
automatically; no explicit
forwarding using RAISING
addition possible;

No review by the syntax


check

cx_dynamic_check

cx_static_check

These exceptions need to be


handled or explicitly
forwarded using the RAISING
addition;

These exceptions need to


be handled or explicitly
forwarded using the
RAISING addition;

No review by the syntax


check

Review by the syntax


check

msg systems ag, 15.01.2013

11. Exception Handling


Summary

You should now be able to


Create exception classes
Trigger exceptions in programs
Handle exceptions
Propagate exceptions

213

msg systems ag, 15.01.2013

Exercise

HOURS

MIN

Exercise
Exception Handling
Class-based Exceptions

214

msg systems ag, 15.01.2013

12. Dynamic Programming

215

Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming

msg systems ag, 15.01.2013

12. Dynamic Programming


Field Symbols
FIELD-SYMBOLS: <fs>
<fs>
<fs>
<fs>

TYPE
<Structure>,
TYPE TABLE OF <Structure>,
TYPE
ANY,
TYPE
ANY TABLE.

Field symbols

216

point to data, table entries, structure components (fields)

are "assigned".

can be of data type ANY.

can be identified by the notation with angle brackets <fs>.

INTO vs. ASSIGNING

resp.

IS INITIAL vs. IS ASSIGNED

<fsob>

Index

Name

Fname

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

1.800

Berg

Lisa

35

2.400

<fs2>

Rei

Tim

24

1.800

<fs3>

<fs1>

msg systems ag, 15.01.2013

12. Dynamic Programming


Field Symbols ASSIGNING vs. INTO

DATA: tab1 TYPE TABLE OF struct,


tab2 TYPE TABLE OF struct,
tab3 TYPE TABLE OF struct.

FIELD-SYMBOLS: <fs> TYPE struct.


* make <fs> point onto
* first record in a table
READ TABLE tab1 INDEX 1
ASSIGNING <fs>.
<fs>
1
* make <fs> point onto
* first record in a another table
READ TABLE tab2 INDEX 2
ASSIGNING <fs>.
<fs>
2
* copy record from a third table into
* the table row pointed at by <fs>
READ TABLE tab3 INDEX 1
<fs>.
INTO <fs>
3

217

Index

Name

Fname

Huber

Lotte

Maier

Klaus

Index

Name

Fname

Berg

Lisa

<fs>

2
Kraus

Rei
Karl

Tim

Index

Name

Fname

Kraus

Karl

Moser

Hans

msg systems ag, 15.01.2013

12. Dynamic Programming


Field Symbols Casting on ASSIGN

TYPES: BEGIN OF st_date,


year(4) TYPE n,
month(2) TYPE n,
day(2)
TYPE n,
END OF st_date.
* Option 1: Implicitly
FIELD-SYMBOLS <fs> TYPE st_date.
ASSIGN sy-datum TO <fs> CASTING
CASTING.

The access is interpreted


as though the data object
had the data type of the
field symbol.

In addition, he casting
type can be explicitly
specified.

* Option 2: Explicit
FIELD-SYMBOLS <fs> TYPE ANY
ANY.
ASSIGN sy-datum TO <fs>
CASTING TYPE st_date.

20120221
* Access after cast:
WRITE: / <fs>-year, <fs>-month, <fs>-day.

<fs>

218

msg systems ag, 15.01.2013

12. Dynamic Programming

219

Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming

msg systems ag, 15.01.2013

12. Dynamic Programming


Statements with Dynamic Attributes

Replacing a literal with a variable:


CALL TRANSACTION 'T_CODE'.

lv_tcode = 'T_CODE'.
CALL TRANSACTION lv_tcode
lv_tcode.

Replacing an identifier with a variable in


brackets (without blanks):
SUBMIT report.

lv_report_name = 'REPORT'.
(lv_report_name)
SUBMIT (lv_report_name).

Replacing an enumeration with an internal table:


SET PF-STATUS 'STATUS_NAME'
EXCLUDING 'FC1' 'FC2' ... .

* fill internal table lt_exclude


* with function codes
SET PF-STATUS 'STATUS_NAME'
EXCLUDING lt_exclude
lt_exclude.

Replacing conditions with an internal table :


LOOP AT table ASSIGNING <fs>
WHERE age = 16 OR age = 17
AND
salary <= 1000 ... .

220

* ... fill internal table


* lt_cond with conditions.
LOOP AT table INTO wa
WHERE lt_cond
lt_cond.

msg systems ag, 15.01.2013

12. Dynamic Programming


Calling methods dynamically

TYPE-POOLS: abap.

DATA: meth TYPE abap_methname,


abap_methname
ptab TYPE abap_parmbind_tab,
abap_parmbind_tab
etab TYPE abap_excpbind_tab.
abap_excpbind_tab
...
meth = 'METHOD_NAME'.
* Fill ptab and etab
...
CALL METHOD ref->(meth)
PARAMETER-TABLE ptab
EXCEPTION-TABLE etab
etab.

Transfer type

ptab

NAME

KIND

Exception
name

etab

221

Reference to the
actual parameter
TYPE REF TO

Formal
parameter
name

NAME

VALUE

sy-subrc
value
VALUE

msg systems ag, 15.01.2013

12. Dynamic Programming

222

Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming

msg systems ag, 15.01.2013

12. Dynamic Programming


Data References
TYPES reftype

DATA

ref

{ TYPE REF TO type_name |


LIKE REF TO do_name
|
TYPE REF TO data }.

Any completely
specified type

{ TYPE REF TO type_name |


LIKE REF TO do_name
|
TYPE REF TO data }.

Generic type

GET REFERENCE OF dataobject INTO ref.

DATA: lr_ref TYPE REF TO i,


lv_num TYPE i VALUE 15.
GET REFERENCE OF lv_num INTO lr_ref.

lr_ref
lv_num

15

lr_ref
lv_num

15

Get reference to a
data object
223

msg systems ag, 15.01.2013

12. Dynamic Programming


Example: Dynamic Method Calls
CLASS lcl_vehicle DEFINITION.
...
METHODS get_make
EXPORTING ex_make TYPE string.
...
ENDCLASS.
TYPE-POOLS abap.
DATA: ptab
TYPE
wa_ptab LIKE
meth
TYPE
gd_make TYPE

abap_parmbind_tab,
LINE OF ptab,
abap_methname,
string VALUE `VOLVO`.

CLASS lcl_vehicle IMPLEMENTATION.


...
METHOD get_make.
ex_make = make.
ENDMETHOD.
...
ENDCLASS.

Situation after the dynamic method call:

wa_ptab-name = 'EX_MAKE'.
GET REFERENCE OF gd_make
INTO wa_ptab-value = ref.
INSERT wa_ptab INTO TABLE ptab.

ptab

meth = 'GET_MAKE'.
CALL METHOD r_truck->(meth)
PARAMETER-TABLE ptab.

gd_make

NAME

KIND

VALUE

EX_MAKE

`VOLVO`

WRITE gd_make.

224

msg systems ag, 15.01.2013

12. Dynamic Programming


Creating Data Objects at Runtime

DATA ref
{ TYPE REF TO type_name | LIKE REF TO var
CREATE DATA ref.

DATA ref
CREATE DATA ref

}.

Typing can also


take place
dynamically
TYPE REF TO data.
TYPE { type_name | itab_type_def }.

PARAMETERS pa_tab
TYPE dd021-tabname.
DATA
ref_itab TYPE REF TO data.

ref_itab

CREATE DATA ref_itab TYPE STANDARD TABLE


OF (pa_tab)
WITH DEFAULT KEY.

Standard table with default key


and row type corresponding to
the content of the data object
pa_tab
225

msg systems ag, 15.01.2013

12. Dynamic Programming


Dereferencing data references
ASSIGN ref->* TO <fs> [CASTING ... ].

PARAMETERS pa_tab
DATA
ref_itab

You can address the contents


of the internal table using the
field symbol.

TYPE dd021-tabname.
TYPE REF TO data.

ref_itab

fs_itab

FIELD-SYMBOLS <fs_itab> TYPE ANY TABLE.


ASSIGN ->*

CREATE DATA ref_itab TYPE STANDARD TABLE


OF (pa_tab)
WITH DEFAULT KEY.
ASSIGN ref_itab->*
->* TO <fs_itab>.
SELECT * FROM (pa_tab) INTO TABLE <fs_itab>.

226

msg systems ag, 15.01.2013

12. Dynamic Programming


Example: Output the contents of an arbitrary database table
PARAMETERS pa_dbtab TYPE dd02l-tabname
DEFAULT 'SFLIGHT'.
DATA d_ref TYPE REF TO data.
FIELD-SYMBOLS: <fs_wa>
TYPE ANY,
<fs_comp> TYPE ANY.
START-OF-SELECTION.

1.

Define input parameter for


selection screen.

2.

Generic data object.


Field symbols for record and
record components.

3.

4.

Dynamically generate the data


object for storing the query result,
and assign it to a field symbol.

5.

Successively assign all structure


components to a field symbol.
(reached the end change the
row and move to the next record)
Output component content.

CREATE DATA d_ref TYPE (pa_dbtab).


ASSIGN d_ref->* TO <fs_wa>.
SELECT * FROM (pa_dbtab) INTO <fs_wa>.
DO.
ASSIGN COMPONENT sy-index
OF STRUCTURE <fs_wa> TO <fs_comp>.
IF sy-subrc NE 0. SKIP. EXIT. ENDIF.
WRITE <fs_comp>.
ENDDO.
ENDSELECT.

227

6.
7.

msg systems ag, 15.01.2013

12. Dynamic Programming

228

Field Symbols
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming

msg systems ag, 15.01.2013

12. Dynamic Programming


Determine properties of internal tables
DATA: no_of_lines
TYPE i,
initial_lines TYPE i,
table_kind
TYPE c.

DESCRIBE TABLE itab


LINES no_of_lines
OCCURS initial_lines
KIND
table_kind.
* other variant:
no_of_lines = lines( itab ).

Number of rows in
the internal table

Number of initial
lines

Table kind

table_kind
Constants for
evaluating the
return values

229

TYPE-POOL: sydes.
sydes_kind-undefined
sydes_kind-standard
sydes_kind-sorted
sydes_kind-hashed
msg systems ag, 15.01.2013

12. Dynamic Programming


RTTI Run Time Type Information

Hierarchy of description classes:


CL_ABAP_TYPEDESCR

Rules
CL_ABAP_DATADESCR
CL_ABAP_ELEMDESCR
CL_ABAP_REFDESCR

Determining the type attributes of a


data object at runtime: :

Transfer the data object to the


static method
DESCRIBE_BY_DATA of class
CL_ABAP_TYPEDESCR

Get the reference to the


appropriate description object

Evaluate of the type attributes


using the public attributes or
methods of this description
object

CL_ABAP_COMPLEXDESCR
CL_ABAP_STRUCTDESCR
CL_ABAP_TABLEDESCR

CL_ABAP_OBJECTDESCR
CL_ABAP_CLASSDESCR
CL_ABAP_INTFDESCR

230

msg systems ag, 15.01.2013

12. Dynamic Programming


Example for dynamic type description
...
DATA:
descr_ref TYPE REF TO cl_abap_structdescr,
wa_comp
TYPE abap_compdescr.
...

START-OF-SELECTION.
...
"get reference to type description object by widening cast:
descr_ref ?= cl_abap_typedescr=>describe_by_data( <fs_wa> ).
...

TOP-OF-PAGE.
LOOP AT descr_ref->components INTO wa_comp.
WRITE wa_comp-name.
ENDLOOP.

231

msg systems ag, 15.01.2013

12. Dynamic Programming

232

Field Symbol
Dynamic Access
Data References
Dynamic Determination of Object Properties
Code Construct with Dynamic Programming

msg systems ag, 15.01.2013

12. Dynamic Programming


Conventional Data Operations

Changing a single, unique record


DATA: itab TYPE TABLE OF structure,
wa
TYPE
structure.
READ TABLE itab INTO wa
WITH KEY age = 24.
IF sy-subrc = 0.

After reading a record, we have to


check if the query was successful.

The changes made to the work area


wa have to be written back to the
internal table itab.

wa-wage
= '1.900'.
MODIFY itab FROM wa.
ENDIF.

233

Index

Name

FName

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

1.800

Berg

Lisa

35

2.400

Rei

Tim

27

2.000

Index

Name

FName

Age

Wage

Maier

Klaus

24

1.900

msg systems ag, 15.01.2013

12. Dynamic Programming


Implementation with Field Symbols

Changing a single, unique record


DATA: itab TYPE TABLE OF structure.
FIELD-SYMBOLS: <fs> TYPE structure.
READ TABLE itab ASSIGNING <fs>
WITH KEY age = 24.
IF sy-subrc = 0.

After reading a record, we have to


check if the query was
successful.

The changes are made directly in


the table.

<fs>-wage = '1.900'.

ENDIF.

234

Index

Name

FName

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

1.900
1.800

Berg

Lisa

35

2.400

Rei

Tim

27

2.000

<fs>

msg systems ag, 15.01.2013

12. Dynamic Programming


Conventional Data Operations

Changing multiple, similar records


DATA: itab TYPE TABLE OF structure,
wa
TYPE
structure.
LOOP AT itab INTO wa
WHERE age = 24.
wa-wage

= '1.900'.

MODIFY itab FROM wa.

No verification needed, since the


code is executed only when
records are found.

The changes made to the work area


wa have to be written back to the
internal table itab.

ENDLOOP.

235

Index

Name

FName

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

1.800

Berg

Lisa

35

2.400

Rei

Tim

24

2.000

Index

Name

FName

Age

Wage

Maier

Klaus

24

1.900

Index

Name

FName

Age

Wage

Rei

Tim

24

1.900

msg systems ag, 15.01.2013

12. Dynamic Programming


Implementation with Field Symbols

Changing multiple, similar records


DATA: itab TYPE TABLE OF structure.
FIELD-SYMBOLS: <fs> TYPE structure.
LOOP AT itab ASSIGNING <fs>
WHERE age = 24.
<fs>-wage = '1.900'.

No verification needed, since the


code is executed only when
records are found.

The changes are made directly in


the table.

ENDLOOP.

236

Index

Name

FName

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

1.900
1.800

Berg

Lisa

35

2.400

Rei

Tim

24

1.900

<fs>

msg systems ag, 15.01.2013

12. Dynamic Programming


Conventional Data Operations

Changing a common field in different internal tables


DATA: itab1
wa1
itab2
wa2

TYPE TABLE OF structure1,


TYPE
structure1,
TYPE TABLE OF structure2,
TYPE
structure2.

LOOP AT itab1 INTO wa1.


wa1-kz = 'U'.
MODIFY itab FROM wa1.

Each internal table has to be


processed separately.

The changes made to the work area


wa have to be written back to the
internal tables.

Unmanageable number of data


statements.

Great effort for extension.

ENDLOOP.
LOOP AT itab2 INTO wa2.
wa2-kz = 'U'.
MODIFY itab FROM wa1.
wa2.

ENDLOOP.

237

msg systems ag, 15.01.2013

12. Dynamic Programming


Implementation with Field Symbols

Changing a common field in different internal tables


DATA: s_tab
s_wa
itab1
itab2

TYPE TABLE OF string,


TYPE
string,
TYPE TABLE OF structure1,
TYPE TABLE OF structure2.

FIELD-SYMBOLS: <fst> TYPE ANY TABLE,


<fsw> TYPE ANY,
<fsk> TYPE ANY.

100% dynamic.

Easy to extend

Very complex!

APPEND 'itab1' TO s_tab.


APPEND 'itab2' TO s_tab.
LOOP AT s_tab INTO s_wa.
UNASSIGN <fst>.
ASSIGN (s_wa) TO <fst>.
IF <fst> IS ASSIGNED.

ENDIF.
ENDLOOP.

238

LOOP AT <fst> ASSIGNING <fsw>.

ASSIGN COMPONENT 'KZ'


OF STRUCTURE <fsw>
TO <fsk>.
IF <fsk> IS ASSIGNED.
<fsk> = 'U'.
ENDIF.
ENDLOOP.
msg systems ag, 15.01.2013

12. Dynamic Programming


Termination after accessing a field symbol which was not assigned

Record not found!


DATA: itab TYPE TABLE OF structure.
FIELD-SYMBOLS: <fs> TYPE structure.
READ TABLE itab ASSIGNING <fs>
WITH KEY age = 20.

If no record is found, the field


symbol points to nothing

The runtime environment


responds the next usage of the
field symbol with a short dump.

= 0.
IF sy-subrc
<fs> IS ASSIGNED.

239

<fs>-wage = '1.400'.

ENDIF.

Index

Name

FName

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

1.800

Berg

Lisa

35

2.400

Rei

Tim

27

2.000

Check 'IS ASSIGNED'


or sy-subrc
RAISE EXCEPTION

<fs>

msg systems ag, 15.01.2013

12. Dynamic Programming


Erroneous write access to pre-assigned field symbol

Record not found on the second query!


DATA: itab TYPE TABLE OF structure.
FIELD-SYMBOLS: <fs> TYPE structure.
READ TABLE itab ASSIGNING <fs>
WITH KEY age = 24.
<fs>-wage
'1.900'.
IF <fs> IS=ASSIGNED.
READ TABLE itab ASSIGNING <fs>
WITH KEY age = 20.
ENDIF.
"!!!
<fs> still points onto Klaus
<fs>-wage = '1.400'.

240

Index

Name

FName

Age

Wage

Huber

Lotte

18

1.200

Maier

Klaus

24

Berg

Lisa

35

1.900
1.800
1.400
2.400

Rei

Tim

27

2.000

If no record is found, the field


symbol continues to point to the
last entry.

The next time you use it, the last


value is overwritten.

UNASSIGN before each reading


operation.

<fs>

msg systems ag, 15.01.2013

12. Dynamic Programming


Summary

You should now be able to


Use field symbols and data references
Call methods dynamically
Determine properties of a data object at runtime

241

msg systems ag, 15.01.2013

Bibliography

242

Horst Keller, Sascha Krger: ABAP Objects ABAP


Programmierung mit SAP NetWeaver, Galileo Press, Bonn (2006),
ISBN 978-3-89842-358-8.
Roland Schwaiger: Schrdinger programmiert ABAP, Galileo Press,
Bonn (2013), ISBN 978-3-8362-1858-0.
Horst Keller, Wolf Hagen Thmmel: Official ABAP Programming
Guidelines, Galileo Press, Bonn (2009), ISBN 978-1-59229-290-5.
Andreas Wiegenstein, Markus Schumacher, Sebastian Schinzel,
Frederik Weidemann: Sichere ABAP-Programmierung, Galileo
Press, Bonn (2009), ISBN 978-3-8362-1357-8.

msg systems ag, 15.01.2013

Thank
you for
Vielen Dank
fryour
Ihreattention!
Aufmerksamkeit

Hubert Englmaier
Senior IT Consultant
Insurance

Telefon: +49 89 96101-4568


Hubert.Englmaier@msg-systems.com
www.msg-systems.com

www.msg-systems.com

243

msg systems ag, 15.01.2013

S-ar putea să vă placă și