Академический Документы
Профессиональный Документы
Культура Документы
%&
$%$3 2EMHFWV
SAP AG 2002
SAP R/3
2002/Q3
&RS\ULJKW
SAP AG 2002
1RWHV RQ 7UDGHPDUNV
n
Some software products marketed by SAP AG and its distributors contain proprietary software
components of other software vendors.
Microsoft, WINDOWS, NT, EXCEL, Word, PowerPoint, and SQL Server are registered
trademarks of Microsoft Corporation.
IBM, DB2, OS/2, DB2/6000, Parallel Sysplex, MVS/ESA, RS/6000, AIX, S/390,
AS/400, OS/390, and OS/400 are registered trademarks of IBM Corporation.
INFORMIX-OnLine for SAP and INFORMIX Dynamic ServerTM are registered trademarks
of Informix Software Incorporated.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
HTML, DHTML, XML, and XHTML are trademarks or registered trademarks of W3C, World
Wide Web Consortium, Massachusetts Institute of Technology.
JAVASCRIPT is a registered trademark of Sun Microsystems, Inc., used under license for
technology developed and implemented by Netscape.
SAP, SAP Logo, R/2, RIVA, R/3, ABAP, SAP ArchiveLink, SAP Business Workflow,
WebFlow, SAP EarlyWatch, BAPI, SAPPHIRE, Management Cockpit, mySAP.com Logo, and
mySAP.com are trademarks or registered trademarks of SAP AG in Germany and several other
countries all over the world. All other products mentioned are trademarks or registered trademarks of
their respective companies.
&RXUVH 3UHUHTXLVLWHV
l 3URJUDPPLQJ H[SHULHQFH
l 6$37(&
P\6$3 7HFKQRORJ\ 6ROXWLRQ 2YHUYLHZ
l %&
$%$3 :RUNEHQFK )RXQGDWLRQV DQG &RQFHSWV
l 3URJUDPPLQJ H[SHULHQFH LQ $%$3
SAP AG 2002
7DUJHW *URXS
SAP AG 2002
The training materials are QRW DSSURSULDWH IRU VHOIVWXG\. 7KH\ FRPSOHPHQW WKH FRXUVH
LQVWUXFWRU
V H[SODQDWLRQV. There is space for you to write down additional information on the
sheets.
There may not be enough time during the course to complete all the exercises. The exercises are
intended as additional examples of the topics discussed during the course. Participants can also use
them as an aid to enhancing their knowledge after the course has finished.
&RXUVH 2YHUYLHZ
&RQWHQWV
l &RXUVH JRDO
l &RXUVH REMHFWLYHV
l &RXUVH FRQWHQW
SAP AG 2002
SAP AG
BC401
1-1
&RXUVH *RDO
SURJUDPPLQJ
$%$3 2EMHFWV
SAP AG 2002
SAP AG
BC401
1-2
&RXUVH 2EMHFWLYHV
n &ODVVHVLQKHULWDQFHLQWHUIDFHV
n ([FHSWLRQ KDQGOLQJ
n '\QDPLF SURJUDPPLQJ
SAP AG 2002
SAP AG
BC401
1-3
&RXUVH &RQWHQW
3UHIDFH
Unit 1
Unit 2
Unit 3
Unit 4
Unit 5
Unit 6
Unit 7
Unit 8
Unit 9
Unit 10
&RXUVH 2YHUYLHZ
,QKHULWDQFH
&DVWLQJ
,QWHUIDFHV
SAP AG 2002
SAP AG
BC401
1-4
&RXUVH &RQWHQW
Unit 11
Unit 12
Unit 13
Unit 14
Unit 15
(YHQWV
'\QDPLF 3URJUDPPLQJ
$SSHQGL[
SAP AG 2002
SAP AG
BC401
1-5
SAP AG 2002
SAP AG
BC401
1-6
SAP AG 2002
SAP AG
BC401
1-7
7HFKQLFDO 1DPHV
l 3DFNDJH %&
6$3%&B[[['B
n 0RGHO VROXWLRQV
6$3%&B[[[6B
n &RS\ WHPSODWHV
6$3%&B[[[7B
SAP AG 2002
Unit codes:
Unit 2:
Unit 3:
Unit 4:
Unit 7:
Unit 8:
Unit 9:
Unit 10:
Unit 11:
Unit 12:
Unit 13:
Unit 14:
Unit 15:
SAP AG
DTO
TAB
CAL
AIR
INH
CAS
INT
EVE
CLS
SPC
EXC
DYN
BC401
1-8
SAP AG 2002
SAP AG
BC401
2-1
SAP AG 2002
SAP AG
BC401
2-2
SAP AG
BC401
2-3
SAP AG 2002
B6 38 2 6 9353%)0(
A @ 8 7 6 42 1
&' &'
&
& $
$%#"!!
,QWURGXFWLRQ
,QWURGXFWLRQ
SAP AG 2002
SAP AG
BC401
2-4
3UHGHILQHG
$%$3 W\SHV
G
W
L
S
I
Q
VWULQJ F
[VWULQJ [
*OREDO
W\SHV
Data objects are usually defined with the '$7$ statement as follows. After the name of the data
object, a a fully-specified type is assigned to it using the 7<3( addition. The type is linked to the
data object statically and cannot be changed at runtime.
There are other syntax variants available (for historical reasons). Note however, that some of these
historical variants are no longer supported in ABAP Objects. For further information, refer to the
keyword documentation for the '$7$ statement.
All types are based on predefined ABAP types, which will be discussed in greater detail in the
following slide. Some of these ABAP types are fully specified and can be used to type data objects
directly. Other types need to be modified to include the length, and in some cases the number of
decimal places so that they are fully specified. These are the simplest examples of user-defined
types.
You can create complex structured types of any level of complexity, based on elementary types.
You can also define types centrally in the $%$3 'LFWLRQDU\ You can then use these JOREDO W\SHV in
all appropriate Repository objects in the system.
SAP AG
BC401
2-5
)L[HG OHQJWK
L
I
S
/HQJWK LQ E\WHV
,QWHJHU
3DFNHG QXPEHU
Q
F
G
W
+H[DGHFLPDO
9DULDEOH
OHQJWK
'HVFULSWLRQ
1XPEHU VHTXHQFH
7LPH
+H[DGHFLPDO FRGH
VWULQJ
[VWULQJ
&KDUDFWHU VHTXHQFH
+H[DGHFLPDO FRGH
$WWULEXWHV
'LIIHU LQ
9DOXH UDQJH
$ULWKPHWLF XVHG
%LW RSHUDWLRQV
5XQWLPH V\VWHP DGMXVWV OHQJWK
G\QDPLFDOO\
SAP AG 2002
The following ABAP types are fully specified and can be used directly to type data objects: G, W, L,
I, VWULQJ, and [VWULQJ. A special feature of the VWULQJ and [VWULQJ types is that their
length is adjusted dynamically at runtime. Thus, for these two types, length is not a component of the
type.
The following ABAP types are not fully specified, so you must include additional information before
you use them to define data objects:
F, Q, and [
Length needs to be added. Permitted values: to characters.
If you do not declare a length when you define a data object, the system assigns the default length
of .
S
You must extend the type to include the length, and number of decimal places, Permitted length:
to bytes. If you do not declare a length when you define a data object, the system assigns the
default length of bytes (that is, 15 digits) and decimal places.
The type also contains information on how the data is stored, what range of values is allowed, what
operations can be performed on the data, and what sort of arithmetic is used (if the data object is
suitable for use in calculations). We will deal with this topic in greater detail later in this unit.
SAP AG
BC401
2-6
8VLQJ D FRQVWDQW
$%$3 SURJUDP
7<3(322/6 ]P\WS
7<3(322/6 ]P\WS
> '$7$ YDU 7<3( ]P\WS
]P\WSBW\SH @
,) YDU
(1',)
]P\WS
]P\WSBFRQVWBQDPH
SAP AG 2002
You must use a type group to define global constants. The name of the type group can contain up to
5 characters.
You define constants in the type group using the &2167$176 statement, just as in an ABAP
program. You only have to adhere to the following namespace convention:
All constants (and type names) must have the name of the type group as a prefix.
You can use either a global Dictionary type or a predefined ABAP type as a valid type.
To be able to use the types of a type group in a program, you must refer to the type group using the
7<3(322/6 statement.
After this line in the program, you can then use all the constants in the type group.
You can also define global data types in a type group. Before SAP R/3 Basis Release 4.5, this was
the only way to define complex global data types.
SAP AG
BC401
2-7
,QWURGXFWLRQ
,QWURGXFWLRQ
SAP AG 2002
SAP AG
BC401
2-8
l
l
LQW
,QWHJHU $ULWKPHWLF
LQW
LQW
LQW
LQW
',9
02'
$GGLWLRQ
6XEWUDFWLRQ
0XOWLSOLFDWLRQ
'LYLVLRQ
,QWHJHU GLYLVLRQ
5HPDLQGHU LQWHJHU GLY
3RZHU
SAP AG 2002
In integer arithmetic, the system always rounds to the appropriate decimal place. So, for example:
'$7$ LQW 7<3( L
LQW
" result: 0
LQW
" result: 1
Calculations performed using integer arithmetic are faster than calculations using fixed point or
floating point arithmetic.
For further information on calculations performed on integers, refer to the keyword documentation
for the &20387( statement.
SAP AG
BC401
2-9
1XPEHU RI GHFLPDO
SODFHV
)L[HG SRLQW DULWKPHWLF
OLNH XVLQJ SDSHU DQG SHQFLO
1XPEHU
,QWHUQDO UHSUHVHQWDWLRQ
l 3HUPLWWHG OHQJWK WR E\WHV WR GLJLWV
i
pH F h
TQgf#Qb`W
P e dcaY X
VSUSQIH GEC
T R PF F D
The length of packed numbers is given in bytes. The connection between value range and length is
derived from the internal representation: Each decimal digit is represented by a half-byte. The last
byte is reserved for the plus or minus sign.
The number of decimal places is defined using the '(&,0$/6 addition. The maximum number of
decimal places is either 15 or the length of the number minus 1 (that is, there must be at least one
digit before the comma).
In user dialogs, decimal numbers are formatted according to the settings in the user master record.
Decimal point-aligned fixed point arithmetic is used as default for calculations. Packed numbers are
thus well-suited to business calculations, where the correct rounding for the specified number of
decimal places is very important. The algorithm for this arithmetic is similar to using "pencil and
paper".
The system always uses packed numbers of maximum length for interim results.
You can switch off fixed point arithmetic in the program attributes. If you do, the '(&,0$/6
addition when defining a packed number only affects the output of the number. Internally, all
numbers are interpreted as integers, regardless of the position of the decimal point. The fixed point
arithmetic option is always selected by default. You should DOZD\V accept this value and use packed
numbers for business calculations.
SAP AG
BC401
2-10
_
-
1,5 = 120 + 12 1
1
=1 +
2
_ _
-3
_
_ _
-6
-7
-10
-11
0,15 = 12 + 12 + 12 + 12 + 12 +
1
1
1
1
1
=
+
+
+
+
+
8
128 1024 2048
64
= 0,125 +
=
=
=
0,140625 +
2QO\ IRU
DSSUR[LPDWLRQV
0,1484375 +
0,1494140625 +
01499023437
=
SAP AG 2002
Unlike packed numbers, floating point numbers are represented using sums of binary fractions.
Floating point numbers are also normalized, and both the exponent and the mantissa are stored in
binary form. This representation complies with the IEEE norm for double precision floating point
numbers. The floating point operations of the relevant processors are used for calculations.
Since algorithms are converted to binary, inaccuracies can occur. Therefore, floating point numbers
are QRW suitable for business calculations.
([DPSOH
You want to calculate 7.72% of 73050 and display the result accurate to two decimal places. The
answer should be 5310.74 (73050 * 0.0727 = 5310.7735). The program, however:
'$7$ IORDW 7<3( I SDFN 7<3( S '(&,0$/6
IORDW
UHVXOW (
SDFN
IORDW :5,7( SDFN
UHVXOW
n
n
<RX VKRXOG WKHUHIRUH RQO\ XVH IORDWLQJ SRLQW QXPEHUV IRU DSSUR[LPDWLRQV :KHQ \RX FRPSDUH
QXPEHUV DOZD\V XVH LQWHUYDOV DQG DOZD\V URXQG DW WKH HQG RI \RXU FDOFXODWLRQV
The DGYDQWDJH RI IORDWLQJ SRLQW QXPEHUV is the large value range: It comprises numbers from is,
from 2,2250738585072014E-308 to 1,7976931348623157E+308 including both positive and
negative numbers as well as zero. In addition, you must use floating point numbers for special
aggregation functions of the 6(/(&7 statement.
SAP AG
BC401
2-11
5HTXLUHG
,QWHJHUV RQO\
7\SH S
7\SH )
SAP AG 2002
1RWH
The results of the following functions have the data type I:
SAP AG
BC401
2-12
l )ORDWLQJ SRLQW DULWKPHWLF LV XVHG LI DW OHDVW RQH FRPSRQHQW LV D IORDWLQJ SRLQW QXPEHU
l $OO FRPSRQHQWV DUH FRQYHUWHG WR WKH UHOHYDQW GDWD W\SH
([DPSOH
S '(&,0$/6
(
(
(
,QWHULP UHVXOW
(
&RQYHUVLRQ DIWHU
S '(&,0$/6
SAP AG 2002
An arithmetic expression may contain any data types that are convertible into each other and into the
type of the result field.
The system converts all the values into one of the three numeric data types (L, S, or I), depending on
the data types of the operands. The ABAP runtime system contains an arithmetic for each of the
three data types. The system then performs the calculation and converts it into the data type of the
result field.
This may mean that the same arithmetic expression leads to different results when performed on
different combinations of data types.
It is also possible for an arithmetic expression to have RQO\ FKDUDFWHU VWULQJ W\SH data objects, as
long as their contents are appropriate. The values are converted to numeric type objects. Bear in
mind that conversions affect performance. Wherever possible, choose a data type that does QRW
require runtime conversion.
If an arithmetic expression contains numeric literals, the choice of arithmetic depends on the size of
the number: If the number is within the value range for the data type L, the numeric literal is
interpreted as an integer. If the value of the literal is greater than 2147483647, it is interpreted as a
packed number.
([DPSOH
'$7$ LQW 7<3( L LQW
"result: 9
LQW
"result: 10
SAP AG
BC401
2-13
'HVFULSWLRQ
/HQJWK
9DOXH UDQJH
&DOFXODWLRQV
)RUPDWWLQJ
RSWLRQV
7\SH W
7\SH G
7LPH
'DWH
GLJLWV
++0066
GLJLWV
<<<<00'' FKDUDFWHUV
%\ FORFN
7LPH
DULWKPHWLF
++0066
7\SH Q
7\SH F
6HTXHQFH )L[HGOHQJWK
RI GLJLWV FKDU VWULQJ
%\ *UHJRULDQ
FDOHQGDU
'DWH
DULWKPHWLF
'LJLWV
&RQYHUVLRQ
FKDUDFWHUV
7\SH VWULQJ
&KDU VWULQJ
RI YDULDEOH
OHQJWK
9DULDEOH
'HSHQGV RQ FRGHSDJH
&RQYHUVLRQ
&RQYHUVLRQ
%DVHG RQ
XVHU GHIDXOW
YDOXHV
SAP AG 2002
The value range of each string depends on the code page, which contains all the supported characters
in form of a table. Internally, each character is represented by a code number. When the system
outputs the character, it uses the code page to convert this number. To find the code page valid in a
given system, choose 7RROV &&06 6SRRO $GPLQLVWUDWLRQ )XOO $GPLQLVWUDWLRQ &KDUDFWHU
6HWV.
The initial value of each character string with fixed length is a space character.
Numeric strings are represented internally as character strings. Note, however, that only digits are
permissible as characters. When character strings are assigned to numeric strings, the system ignores
letters and only copies the digits (right-aligned). Missing characters are filled with zeros.
n
n
n
Only sequences of digits are valid for values of type G. These digits form a meaningful date, which
complies with the Gregorian calendar. The first four digits represent the year, the next two the month
and the last two the date. For performance reasons however, the object is only checked if it is an
input field on a screen or selection screen.
The initial value of a date is
. The formatting options are determined by the user settings.
For values of type W, a sequence of digits is only valid if it can be interpreted as a time on the 24-hour
clock. The rules for interpreting the value are analogous to those used for dates.
SAP AG
BC401
2-14
5(3/$&(
75$16/$7(
6+,)7
&21'(16(
29(5/$<
&21&$7(1$7(
63/,7
g`y9x fvusq
t y w t r
),1'
"
$%$3
$%$3
$%$3
$%$3
$3
$% 3
$$$$
$ % $ 3
$%&$3
%%$3
D E DS
%$3
$3
$%$3
$%$3
$%
$3
0RYH
SAP AG 2002
n
n
n
n
n
For every statement, the operands are treated like type F fields, regardless of their actual field type.
No internal type conversions take place.
All of the statements apart from 75$16/$7( and &21'(16( set the system field V\VXEUF.
(6($5&+ also sets the system field V\IGSRV with the offset of the character string found.)
All of the statements apart from 6($5&+ are case-sensitive.
To find out the occupied length of a string, use the standard function 675/(1
For the 63/,7 statement there is the variant 63/,7 ,172 7$%/( <itab>, which you
can use to split the character string dynamically. You do not need to specify the number of parts into
which the string should be split.
SAP AG
BC401
2-15
5(3257
3$5$0(7(56
SDBVWU /2:(5 &$6(
SDBSRV 7<3( L
SDBOHQ 7<3( L
G``ssQ#`S`s
G```G
SAP AG 2002
In DQ\ statement that operates on a FKDUDFWHUW\SH field, you can address part of the field or
structure by specifying a starting position and a number of characters. If the field lengths are
different, the system either truncates the target or fills it with initial values. The source and target
fields must have the type [, F, Q, G, W, or 675,1* You can also use structures.
([DPSOH
029( <field1>+<off1><len1> 72 <field2>+<off2><len2>
This statements assigns len1> characters of field <field1> starting at offset <off1> to
<len2> characters of <field2> starting at offset <off2>.
SAP AG
BC401
2-16
,QWURGXFWLRQ
,QWURGXFWLRQ
SAP AG 2002
SAP AG
BC401
2-17
7<3(6
7<3(6
%(*,1 2) VBQDPHBW\SH
VBQDPHBW\SH
SUHQDPH 7<3( F
VXUQDPH 7<3( F
WLWOH
7<3( F
(1' 2) VBQDPHBW\SH
'$7$
%(*,1 2) VBQDPH
VBQDPH
SUHQDPH 7<3( F
VXUQDPH 7<3( F
WLWOH
7<3( F
VBQDPH
(1' 2) VBQDPH
'$7$
VBQDPH 7<3( VBQDPHBW\SH
$OWHUQDWLYHV
67$572)6(/(&7,21
VBQDPHSUHQDPH
6PLWK
VBQDPH
VBQDPHVXUQDPH
-RKQ
VBQDPH
SAP AG 2002
As with elementary data objects, you can define structures in two ways:
Define the data object directly using the '$7$ statement. The syntax is similar to the definition of
a structure type. If you use this option, the corresponding structure type is defined implicitly at the
same time.
n
In both cases, the type is defined locally. Bear in mind that you can also use globally defined types
instead.
SAP AG
BC401
2-18
6WUXFWXUH W\SH
VBQDPHBW\SH
SUHQDPH
VXUQDPH
WLWOH
'$7$
%(*,1 2) VBDGGUHVV
QDPH
7<3( VBQDPHBW\SH
VBQDPHBW\SH
VWUHHW 7<3( F
FLW\
7<3( F
(1' 2) VBDGGUHVV
VBDGGUHVVQDPHVXUQDPH
VBDGGUHVVFLW\
VBDGGUHVV
6PLWK
/RQGRQ
1HVWHG VWUXFWXUH
QDPH
VWUHHW
FLW\
SUHQDPH
VXUQDPH
WLWOH
$SSOLFDWLRQ
/RJLFDO VXEJURXSLQJ
RI GDWD
SAP AG 2002
You can define nested structures by assigning a structure type to a component ZLWKLQ a structure type.
You can address this substructure as a whole using the component name:
structure_namesubstructure_name.
You can also integrate components of a dynamic type in a structure. This can either be an elementary
data object of variable length (VWULQJ or [VWULQJ),an internal table, or a reference. These
structures are known as GHHS structures.
There are constraints on how such deep structures can be used. For instance, a deep structure cannot
be used DV D ZKROH in the ,172 clause of the 6(/(&7 statement. (Instead, each component must be
listed separately). Offset operations are also not appropriate. For more information, refer to SAP
Note 176336.
SAP AG
BC401
2-19
6WUXFWXUH W\SH
VBQDPHBW\SH
SUHQDPH
VXUQDPH
WLWOH
'$7$
%(*,1 2) VBDGGUHVV
,1&/8'( 6758&785( VBQDPHBW\SH
,1&/8'( 6758&785(
$6 QDPH
'$7$
VWUHHW 7<3( F
FLW\
7<3( F
(1' 2) VBDGGUHVV
VBDGGUHVVQDPHVXUQDPH
6PLWK
RU
VBDGGUHVVVXUQDPH
6PLWK
SUHQDPH
VXUQDPH
WLWOH
VWUHHW
FLW\
QDPH
8VH
/RJLFDO VXEJURXS GHVLUHG EXW QHVWHG
VWUXFWXUHV QRW WHFKQLFDOO\ SRVVLEOH
SAP AG 2002
In situations like these, you may still want to group parts of the structure and address it as a whole.
You do this using named includes:
You can include a substructure in another structure, and give this substructure a name:
'$7$ %(*,1 2) structure_name
,1&/8'( 6758&785( substructure_type $6 name
'$7$
(1' 2) structure_name
You can address this substructure using structure_name-name. You can address the fields in
the substructure using structure_name-name-comp_name or directly using
structure_name-comp_name. Technically, however, this structure is QRW nested.
If naming conflicts occur - for example, if you want to include the same substructure twice - you
can append another name to the component names using 5(1$0,1*. For further information,
refer to the keyword documentation for the ,1&/8'( 6758&785( statement.
SAP AG
BC401
2-20
,QWURGXFWLRQ
,QWURGXFWLRQ
SAP AG 2002
SAP AG
BC401
2-21
8QLFRGH 2YHUYLHZ
3UHYLRXV SUREOHPV
l
l
D
"
8QLFRGH
"
"
6ROXWLRQ 8QLFRGH
SAP AG 2002
To be able to work with Unicode, you must have a Unicode-compatible SAP System installed that
itself has a corresponding operating system and database. The ABAP programs must also be
Unicode-compatible.
In Unicode programs, other syntax rules apply than in non-Unicode programs. This is due to the
difference between the length in bytes and the number of characters in a character set in Unicode.
Existing programs are affected by a conversion to Unicode if an explicit or implicit assumption is
made about the internal length of a character. To execute the relevant syntax checks, you must check
8QLFRGH &KHFNV $FWLYH under program attributes.
In a Unicode system, you can only execute programs that have the Unicode flag set. If the Unicode
flag is set for a program, the syntax check and program are executed in accordance with the rules
described in the Unicode online help (irrespective of whether it is a Unicode or a non-Unicode
system).
If the Unicode flag is not set, the program can only be executed in a non-Unicode system. For such
programs, the Unicode-specific changes of syntax and semantics do not apply. However, you can
use all language enhancements introduced in connection with the conversion to Unicode.
SAP AG
BC401
2-22
G
F
E\WHW\SH
&KDUDFWHUW\SH
VWUXFWXUH W\SHV
[VWULQJ
VWULQJ
z yv w vu
IGx00`t
||||z y
0%%%{evx
rsqnom
p
j h #e
i gf d
~d
d
r}p
j Sh glk
i f d
SAP AG 2002
The character string processing (for which, previously, all arguments were implicitly interpreted as
type F fields) is separated into string processing for character-type and byte-type arguments.
In the case of the string processing for character-type arguments, the single fields must be type F, Q,
G, W, or VWULQJ; character-type structures are also permitted. If arguments of another type are
transferred, this triggers a syntax or runtime error.
The ; variants of the string statement are distinguished from the character string commands by the
,1 %<7( 02'( addition. For the character string variants, ,1 &+$5$&7(5 02'( is an optional
addition. If the ,1 %<7( 02'( addition is specified, only ; fields and ; strings are permitted as
arguments. If arguments of another type are transferred, this triggers a syntax or runtime error.
SAP AG
BC401
2-23
)XQFWLRQV
&RPSDULVRQ
RSHUDWRUV
&KDUDFWHUW\SH W\SH
675/(1
675/(1
/HQJWK LQ
E\WHV
%\WHW\SH W\SH
;675/(1
%<7(&2 %<7(&$
%<7(&6 %<7(&1
%<7(1$ %<7(16
([DPSOHV
`uG
fU9fp ps{f
GpB`I`p`IG
GBgsf`ugep`p{S
fggfgsf`uge `p{S` G
s
gsgusgG
susgU9ffgfsfs
SAP AG 2002
The operators &2, &1, &$, 1$, &6, 16, &3, 13 are available for comparing the contents of character
string type data objects (for syntax and semantics, see keyword documentation). As with the
statements for string processing, these operators require single fields of type F Q, G, W, or VWULQJ
as arguments. Again, character-type structures are also permitted.
The ; variants of the string comparison operators are distinguished from the character string variants
by the %<7(- prefix. For this operation, only ; fields and fields of the type [VWULQJ are allowed
as arguments.
The 675/(1 function only works with character-type fields and returns the length in characters.
With F fields, only the so-called occupied length is relevant, that is, unlike with strings, trailing
blanks are not counted.
The ;675/(1 function is available for the length of byte sequences. For ; strings, ;675/(1
returns the current length and for ; fields, the defined length in bytes, where null bytes at the end of
fields are counted.
SAP AG
BC401
2-24
fI
SAP AG 2002
If two data types are not compatible but there is a conversion rule, the system converts the source
object into the type of the target object when you assign values, perform calculations, or compare
values.
For a full list of all conversion rules, refer to the ABAP syntax documentation for the 029(
statement.
If there is no conversion rule defined for a particular assignment, the way in which the system reacts
depends on the program context.
If the types of the objects involved are defined VWDWLFDOO\, a V\QWD[ HUURU occurs.
([DPSOH:
'$7$ GDWH 7<3( G 9$/8(
WLPH 7<3( W
),(/'6<0%2/6 IVBGDWH! 7<3( G IVBWLPH! 7<3( W
$66,*1 GDWH 72 IVBGDWH! WLPH 72 IVBWLPH!
IVBWLPH!
IVBGDWH!
In the case of G\QDPLF typing a UXQWLPH HUURU occurs, because the field symbols are not assigned
types until the assignment of the data objects at runtime.
([DPSOH:
),(/'6<0%2/6 IVBGDWH! 7<3( $1< IVBWLPH! 7<3( $1<
(Rest as above)
SAP AG
BC401
2-25
%`
u
u
%g b 0
u
Gt" y
u
%b0
05
Gt" y
Gt" y
%ug
$VVLJQPHQW LV SRVVLEOH
%`
usffgusff 9{
'HFLVLYH FULWHULRQ
)UDJPHQW YLHZ
G" y
t
|w
0Q
For some data types there are, for technical reasons, specific alignment requirements that depend on
the individual platforms. (In the memory, fields of this type must begin and end on specific
addresses - for example, a memory address divisible by four.)
Within a structure, the runtime system, if necessary, inserts bytes before or after these components
with alignment requirements to achieve the alignment needed. These bytes are known as
$OLJQPHQW.
To check whether the conversion is even permitted, the system first creates the Unicode fragment
view of the structures by grouping adjacent components and alignment gaps (one group each for
character-type types [F, Q, G, W], byte-type types, and types L, I, and S).
Adjacent character-type components of a structure are therefore only grouped together if there are
no alignment gaps between these components. Adjacent byte-type components are grouped in the
same way.
If the fragments of the source and initial structures match the type and length in the length of the
shorter structure, the conversion is allowed.
If the target structure is longer than the source structure, the character-type components of the
remainder are filled with space characters. All other components in the remainder are filled with the
type-specific initial value, alignment gaps are filled with null bytes.
SAP AG
BC401
2-26
b
v
u
%g
%f
Gt" y
w Bf
GB y
t
pGGffpsg 9{
'HFLVLYH FULWHULRQ
)UDJPHQW YLHZ
w g Bf
$VVLJQPHQW LV SRVVLEOH
&RQYHUVLRQ EHWZHHQ VWUXFWXUHV DQG VLQJOH ILHOGV LV SRVVLEOH LI
WKH VWUXFWXUH EHJLQV ZLWK D FKDUDFWHUW\SH IUDJPHQW DQG WKLV
IUDJPHQW LV DW OHDVW DV ORQJ DV WKH VLQJOH ILHOG
SAP AG 2002
The following rules apply for the conversion of a structure to a single field and vice versa:
If the single field is type F, but the structure isnt completely character-type, the conversion is only
allowed if the structure begins with a character-type group and this group is at least as long as the
single field. The conversion then takes place between the first character-type group of the structure
and the single field. If the structure is the target field, the character-type parts of the remainder are
filled with space characters and all other components with the type-specific initial value.
The conversion is not allowed if the structure is not purely character-type and the single field is not
type F.
SAP AG
BC401
2-27
029( VBGDWH 72 SBGDWH
g
w
&KDUDFWHUW\SH
VWUXFWXUH
gSy
w
SAP AG 2002
VBGDWH is structured in such a way that the first four characters of a variable specify the year, the
next two specify the month, and the last two the day. If a date is assigned to this structure using the
029( statement, the characters are then copied left-aligned. You can then determine the year, month,
and day directly using the components of the structure, without having to use offsets.
SAP AG
BC401
2-28
w BfS
uEuGpGg9
U3IsuGgGs {
sp9g{
w Bf
l
l
,Q WKH FDVH E\WHW\SH ILHOGV WKH YDOXHV IRU RIIVHW DQG OHQJWK DUH
RI
WDNHQ LQ E\WHV
SAP AG 2002
n
n
Offset/length accesses are permitted on character-type single fields, single fields with the type
VWULQJ, and single fields of the types [ and [VWULQJ.
For character-type fields and type VWULQJ fields, offset and length are interpreted character by
character. Only with types [ and [WULQJ are the values for offset and length taken in bytes.
SAP AG
BC401
2-29
|z
%%
w
}0Q
%e b
w
f
9 ih f S
pgsuu
G
gu9gg
G
gf
GuG3IgGI9
GuG3Ig
G
p"sg
gpsuuSIg f
gpBggpgsuISgIIpI`{Gus
6WUXFWXUH PXVW VWDUW ZLWK D FKDUDFWHUW\SH IUDJPHQW
SAP AG 2002
Offset and length accesses to structures are only permitted in Unicode programs if the structure is
flat and the offset and length specifications only contain character-type fields from the beginning of
the structure. This means, the access will cause an error if the offset and length area contains both
character-type and non-character-type components.
If an offset/length access is allowed in a Unicode program, both offset and length specifications are
interpreted as characters.
5HFRPPHQGDWLRQ
Only use offset and length accesses if it is necessary or useful. Bear in mind that processing
component by component and using character string operations is generally safer and more
readable.
SAP AG
BC401
2-30
SAP AG 2002
SAP AG
BC401
2-31
([HUFLVH
8QLW 'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO
7RSLF
'HILQLQJ 'DWD 7\SHV DQG 'DWD 2EMHFWV
%DVLF 6WDWHPHQWV
3URFHVVLQJ &KDUDFWHU 6WULQJV
7HPSODWH
0RGHO VROXWLRQ
=%&B
B63/,7B675,1*
6$3%&B'727B63/,7B675,1*
6$3%&B'726B63/,7B675,1*
1-1
1-2
Familiarize yourself with the main body of the program. Pay special attention to the
content of the data object datastring after the function module call. Use the
Debugger to do this, and/or display the character string in a list. (The function
module itself is here seen as a black box. For this exercise, it is not necessary to
understand its construction.)
1-3
To be able to split the character string into its components you must first remove
the ## characters. Remove the two leading separators from the character string first.
Then copy the initial part up to the closing separators to the auxiliary variable
set_string. For this, set_string has to be defined appropriately.
SAP AG
BC401
2-32
1-4
Now use the separators to split the contents of the auxiliary variable set_string
into the structure wa_flight_c. The latter is typed with the local program
structure type st_flight_c. You still have to comment out the components of
this structure type and assign them an appropriate type.
1-5
1-6
In the list displayed in exercise 1-5, you should have observed that some of the
fields were displayed without formatting for example, the PRICE field. Your next
step is to change this.
To do this, convert the data you have extracted by copying it to data objects with
suitable types. Also, not all components of wa_flight_c are to be displayed.
For this purpose, a structure wa_flight has already been defined. It is typed with
the structure type st_flight. You must comment out the components of
st_flight and find appropriate types for these components for the formatting.
Then copy the identically-named components of the character-type structure
wa_flight_c to the fields of the structure wa_flight.
Display the contents of the structure wa_flight in a list. Use the appropriate
formatting options for the WRITE statement for the fldate and price
components.
SAP AG
BC401
2-33
6ROXWLRQ
8QLW 'DWD 7\SHV DQG 'DWD 2EMHFWV LQ 'HWDLO
7RSLF
'HILQLQJ 'DWD 7\SHV DQG 'DWD 2EMHFWV
%DVLF 6WDWHPHQWV
3URFHVVLQJ &KDUDFWHU 6WULQJV
REPORT
sapbc401_dtos_split_string.
TYPES:
BEGIN OF st_flight_c,
mandt(3)
TYPE c,
carrid(3)
TYPE F,
TYPE Q,
connid(4)
TYPE Q,
fldate(8)
TYPE F,
price(20)
TYPE F,
currency(5)
TYPE F,
planetype(10)
TYPE Q,
seatsmax(10)
TYPE Q,
seatsocc(10)
paymentsum(22) TYPE F,
seatsmax_b(10) TYPE Q,
seatsocc_b(10) TYPE Q,
seatsmax_f(10) TYPE Q,
seatsocc_f(10) TYPE Q,
END OF st_flight_c,
BEGIN OF st_flight,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate
TYPE G,
price(9)
currency(5)
TYPE S '(&,0$/6 ,
TYPE F,
planetype(10) TYPE F,
seatsmax
seatsocc
TYPE L,
TYPE L,
END OF st_flight.
SAP AG
BC401
2-34
DATA:
datastring
TYPE string,
VHWBVWULQJ
7<3( VWULQJ
TYPE st_flight.
START-OF-SELECTION.
CALL FUNCTION BC401_GET_SEP_STRING
* EXPORTING
*
IM_NUMBER
= 1
IM_TABLE_NAME
= SFLIGHT
IM_SEPARATOR
= #
IM_UNIQUE
= X
IMPORTING
ex_string
= datastring
EXCEPTIONS
no_data
= 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE a038(bc401).
ENDIF.
,1 GDWDVWULQJ
,) V\VXEUF !
(1',)
63/,7 GDWDVWULQJ $7
63/,7 VHWBVWULQJ $7
,172
ZDBIOLJKWBFPDQGW
ZDBIOLJKWBFFDUULG
ZDBIOLJKWBFFRQQLG
ZDBIOLJKWBFIOGDWH
ZDBIOLJKWBFSULFH
ZDBIOLJKWBFFXUUHQF\
ZDBIOLJKWBFSODQHW\SH
SAP AG
BC401
2-35
ZDBIOLJKWBFVHDWVPD[
ZDBIOLJKWBFVHDWVRFF
ZDBIOLJKWBFSD\PHQWVXP
ZDBIOLJKWBFVHDWVPD[BE
ZDBIOLJKWBFVHDWVRFFBE
ZDBIOLJKWBFVHDWVPD[BI
ZDBIOLJKWBFVHDWVRFFBI
ZDBIOLJKWSODQHW\SH
ZDBIOLJKWVHDWVPD[
ZDBIOLJKWVHDWVRFF
SAP AG
BC401
2-36
SAP AG 2002
SAP AG
BC401
3-1
SAP AG 2002
SAP AG
BC401
3-2
SAP AG
BC401
3-3
SAP AG 2002
R QI H
(R PU I R UVSTPPGF
X W
DE ! " )
8
D C )@9 !
3 %1 " #BA(#67! ! " #
45#! 20' & % "
3 1) $ !
SAP AG 2002
SAP AG
BC401
3-4
6WUXFWXUHV
$%$3
SURJUDP
,QWHUQDO WDEOH
$W UXQWLPH WKH UXQWLPH V\VWHP DOORFDWHV
PHPRU\ IRU WKH OLQHV LQ WKH WDEOH DV QHHGHG
G\QDPLF WDEOH H[WHQVLRQ
SAP AG 2002
Internal tables are data objects that allow you to store datasets with a fixed structure in memory. The
data is stored line by line in memory. Each line has the same structure.
You can also refer to each component in a line as a column in the internal table. You refer to each
line in the internal table as a table line or table entry.
Internal tables are G\QDPLF GDWD REMHFWV - that is, they can hold any number of lines of a fixed type.
The number of lines in an internal table is limited only by the capacity of the specific system with
which you are working.
You can define the line type of an internal table to include elementary, structured, or even table
types, and can be as complex as you want to make it.
SAP AG
BC401
3-5
/LQH W\SH
$$
0,
/+
.0
/+
4)
64
8$
.0
0,
0,
0,
64
,QGH[ DFFHVV
.H\ DFFHVV
7DEOH NLQG
Standard table
Sorted table
Hashed table
.H\ GHILQLWLRQ
( .H\ ILHOGV
( 6HTXHQFH
( 8QLTXHQHVV
SAP AG 2002
7DEOH NLQG
There are three table kinds in ABAP. Each kind has different attributes, which specify whether the
key is unique, how the index is managed, and how the table data is addressed internally. The table
kind specifies the possible accesses. The purpose of this is to provide particular support for special
access types from the runtime system (will be discussed later).
SAP AG
BC401
3-6
STANDARD TABLE
+DVKHG WDEOH
SORTED TABLE
HASHED TABLE
,QGH[ DFFHVV
Q
i b
gg
hT%ff
h g
7g 7
r e h Y c Y ` u r e h Y
gsp gieysxYwvtsp iie
ip dte
r e h Y
V 29T
7edVa
r e h Y e c Y `
dqp igfdbaY
VT 2wb
$FFHVV E\
l7ii w
l
k T
7g
k%
Tg g j j
k
8QLTXHQHVV
f 2V
i
f 29i
l n p p(
7 #oo
l
gam
x
.H\ DFFHVV
SAP AG 2002
You can only perform an index access (the fastest type of access) on STANDARD and SORTED
tables.
The runtime system implements key accesses of SORTED and HASHED tables in a way that
optimizes runtime performance. Key access of STANDARD tables is by WDEOH VFDQ - that is,
comparing the field contents with the search key line by line, in a loop.
The runtime system implements key accesses of SORTED tables, for fully or partly qualified keys
(left-aligned, no gaps, all fields filled with an "=") in a special way to optimize runtime.
The same applies to partial sequential loops, where the loop condition is specified using the key
fields. With the runtime-optimizing access, the runtime system keeps SORTED tables sorted by key
fields,
In all other cases, the system performs a WDEOH VFDQ.
The runtime optimizes key accesses of HASHED tables IRU IXOO\ TXDOLILHG NH\V RQO\. The table
entries are addressed by means of a special hash algorithm. This minimizes access time for reading
single records with fully qualified keys. The access time is thus independent of the number of lines
in the table.
In all other cases, the system performs a WDEOH VFDQ. This means that you should only use HASHED
tables if you want to access table entries using a fully-qualified key.
SAP AG
BC401
3-7
SAP AG 2002
SAP AG
BC401
3-8
7<3(6
ORFDOBW\SH
JOREDOBW\SH
7DEOH
DWWULEXWHV
,QWHUQDO WDEOH
'$7$
WBOLVW 7<3(
ORFDOBW\SH
ORFDOBW\SH
7DEOH
DWWULEXWHV
,QWHUQDO WDEOH
'$7$
WBOLVW 7<3(
7DEOH
DWWULEXWHV
,QWHUQDO WDEOH
'$7$
WBOLVW 7<3(
JOREDOBW\SH
JOREDOBW\SH
SAP AG 2002
The third option is to define an internal table using a global table type (defined in the ABAP
Dictionary).
n
The tables you define using the first or second option are visible only in the program in which you
define them. Whenever you need table type visible from all programs, create a global table type (that
is, use the third option).
SAP AG
BC401
3-9
'$7$
$WWULEXWHV
OLQHBW\SH
RU
( 7DEOH NLQG
:,7+ NH\BGHI
NH\BGHI
( .H\
$WWULEXWHV
JOREDOBW\SH
( 7DEOH NLQG
SAP AG 2002
To define a table type (explicitly or implicitly), you must give the type (or data object) a name, as
well as specifying a WDEOH NLQG OLQH W\SH and NH\
If you are defining a local table type, enter the kind after TYPE and the line type after OF. You must
list the key fields after WITH.
You create and edit global types in the ABAP Dictionary. If you are defining a global table type,
specify the same information on the maintenance screens.
For table types defined in a program, you can enter the number of lines that the runtime system
should reserve when it initializes the data object, after the INITIAL SIZE addition. This makes
sense if you know exactly how many lines you will want in your table when you create it. However,
if your table needs more lines at runtime, it will not be limited in size by this addition, since the
runtime system frees the necessary memory dynamically.
(Internal tables are G\QDPLF data objects).
SAP AG
BC401
3-10
7<3(6
7<3(6
%(*,1 2) OLQHBW\SH
OLQHBW\SH
(1' 2) OLQHBW\SH
OLQHBW\SH
/RFDO VWUXFWXUH
'$7$
'$7$
7DEOH W\SH
OLQHBW\SH
OLQHBW\SH
:,7+ NH\BGHI
VWUXFWXUH
2)
:,7+ NH\BGHI
SAP AG 2002
To specify the line type of an internal table, you can use all the local and global data types or data
objects.
Internal tables are most frequently used to display contents of the database tables. Normally, nonnested structured data types are used for this.
If you use a line type, you must use a statement in the form: 7<3( WDEOHBNLQG 2)
line_type, where WDEOHBNLQG is the kind of table (to be discussed later) and line_type is
the name of the structure type you are using.
If you use structure (data object), you must use a statement in the form: /,.( WDEOHBNLQG 2)
line_type, where structure is the name of the structure object you are using.
SAP AG
BC401
3-11
7<3(6
LWDEW\SH 7<3( WDEOHBNLQG
2)
OLQHBW\SH
:,7+ NH\BGHI
NH\BGHI
NH\B
WDEOHBNLQG
67$1'$5' 7$%/(
6257('
+$6+('
7$%/(
7$%/(
NH\BGHI
NH\B
>12181,48(@ ^ .(< FRO FROQ _
'()$8/7 .(< `
^81,48( _ 12181,48(` .(< FRO FROQ
81,48( .(< FRO FROQ
SAP AG 2002
There are three parameters that you use to specify a table kind for your internal table: STANDARD
TABLE, SORTED TABLE and HASHED TABLE
You specify the table key with the :,7+ NH\BGHI addition. NH\BGHI includes the names of all
the key fields in order and specifies whether the key is to be UNIQUE or NON-UNIQUE.
The combination of the table kind and the key definition is very significant, because of the special
support that certain table kinds receive with specific types of read access. You can use any of the
following combinations:
For STANDARD tables:
Either create a user-defined key by naming the key fields after NON-UNIQUE KEY, or specify the
standard key using the WITH DEFAULT KEY addition. The standard key consists of all the fields
with character-type data types (c, n, d, t, x, string, xstring).
For SORTED tables:
List the key fields after WITH UNIQUE KEY or NON-UNIQUE KEY as appropriate.
For HASHED tables:
List the key fields after WITH UNIQUE KEY.
Alternatively, use the pseudo-component table_line, if you are specifying a table without a
structured line type, or if the entire table line is being used as the key. This will be discussed in more
detail later in the unit.
SAP AG
BC401
3-12
7<3(6
VBGLVWDQFHBW\
%(*,1 2) VBGLVWDQFHBW\
FDUULG
7<3( VBFDUUBLG
FRQQLG
7<3( VBFRQQBLG
GLVWDQFH 7<3( VBGLVWDQFH
GLVWLG
7<3( VBGLVWLG
(1' 2) VBGLVWDQFHBW\
7DEOH W\SH
7<3(6
WWBGLVWDQFHBW\ 7<3( 67$1'$5' 7$%/(
2) VBGLVWDQFHBW\
:,7+ 12181,48(
.(< GLVWDQFH GLVWLG
,QWHUQDO WDEOH
'$7$
WWBGLVWDQFHBW\
WWBGLVWDQFHBW\
WWBGLVWDQFH 7<3( WWBGLVWDQFHBW\
WWBGLVWDQFH
,QWHUQDO WDEOH
RI WKH WDEOH NLQG
67$1'$5' 7$%/(
SAP AG 2002
The above example shows the definition of an internal table (tt_distance) using a local table
type (tt_distance_ty), which itself uses a local line type (s_distance_ty).
The internal table defined here is a STANDARD table with the line type s_distance_ty. It
contains the columns carrid, connid, distance and distid.
The distance and distid fields are key fields. The key is not unique.
SAP AG
BC401
3-13
6RUWHG WDEOH
'$7$
GLVWDQFHBUDQF
7<3( 6257(' 7$%/(
VBGLVWDQFHBW\
2) VBGLVWDQFHBW\
:,7+ 12181,48( .(<
GLVWLG GLVWDQFH
FDUULG
6HTXHQFH
/+
/+
$$
40
FRQQLG
GLVWDQFH GLVWLG
.0
.0
0,
0,
6HTXHQFH
+DVKHG WDEOH
FDUULG
'$7$
GLVWDQFHBEXIIHU
GLVWDQFHBEXIIHU
7<3( +$6+(' 7$%/(
VBGLVWDQFHBW\
2) VBGLVWDQFHBW\
:,7+ 81,48( .(<
FDUULG FRQQLG
/+
$$
40
/+
FRQQLG
GLVWDQFH GLVWLG
.0
0,
0,
.0
SAP AG 2002
The above example shows the definition of a SORTED table and a HASHED table. Both tables have
the same line type as the STANDARD table on the previous slide.
Note that the contents of the table are in a different order. For SORTED tables, the sequence of the
entries in the internal table is determined by the sequence of fields in the key definition.
SAP AG
BC401
3-14
2) OLQHBW\SH
,QWHUSUHWHG E\
WKH V\VWHP DV
'HILQLWLRQ E\
VWDQGDUG WDEOH W\SH
'$7$
67$1'$5'
WBQDPH 7<3( 67$1'$5' 7$%/(
2) OLQHBW\SH
([DPSOH
'$7$
SAP AG 2002
A standard type exists for defining standard tables with bound types. (Table type STANDARD
TABLE, key WITH DEFAULT KEY). When you create such a table, you can omit the STANDARD
and WITH DEFAULT KEY additions, since the runtime system supplies them automatically.
Note however, that the standard table type exists for data objects only. If you are defining a table
type that you want to use to provide a type for data objects, you must specify all its attributes
completely.
Incomplete table types are known as JHQHULF. You can only use them to assign types to table-type
interface parameters.
SAP AG
BC401
3-15
ZDBILHOG
7<3( GGOILHOGQDPH
)LOO
.H\
DFFHVV
63)/,
&,7<)520
SAP AG 2002
Use an unstructured line type if you need a single-column internal table. The slide shows an example
of a single-column table with the line type field_name.
You can use internal tables with an unstructured line type in the following ABAP statement (among
others):
SET PF-STATUS .. EXCLUDING itab. (deactivate function codes dynamically)
SPLIT .. INTO TABLE itab. (split a string dynamically)
You can use a key access, for example, to ascertain whether a specific entry H[LVWV.
SAP AG
BC401
3-16
SAP AG 2002
SAP AG
BC401
3-17
2SHUDWLRQV RQ RQH RU
PRUH OLQHV
(GLWLQJ OLQHV
LQ D ORRS
RU
029(
( &RS\
&/($5
.HHS PHPRU\ DYDLODEOH
( ,QLWLDOL]H
)5((
5HOHDVH PHPRU\
( ,QLWLDOL]H
( ,QVHUW
( 5HDG
5($'
( &KDQJH 02',)<
( 'HOHWH
'(/(7(
( ,QVHUW VXPPDWHG
( (GLW OLQHV
VHTXHQWLDOO\
/223
FRPPDQG EORFN
(1'/223
&2//(&7
!! !
( 6RUW
,16(57
6257
2SHUDWLRQV RQ OLQHV:
You can use the INSERT, READ, MODIFY, DELETE, and COLLECT statements to perform
operations on one or more lines in the internal table. The syntax of the first four of these statements
is dealt with in the slides that follow. For the syntax of the COLLECT statement, refer to the online
documentation.
/RRS SURFHVVLQJ
You can use the LOOP AT ... ENDLOOP statement to process the lines of the internal table
sequentially. The exact syntax for this is dealt with in the slides that follow.
SAP AG
BC401
3-18
7DEOH NH\
/LQH LQGH[ IRU LQGH[ WDEOHV
Q
FRQGLWLRQ!
> Q Q @
&RS\LQJ SURFHVV
7DEOH
)LHOG V\PERO
'LUHFW DFFHVV E\
GHUHIHUHQFHG SRLQWHU
,QVHUW
XVLQJ WDEOH
SAP AG 2002
Almost all line operations and loop processing statements offer you several options for specifying
the target set of records to be processed. You can specify these target records by declaring a key or
index, by formulating a condition that applies to some of the columns, or by specifying an index
interval. Your use of these options is limited only by the table kind you have chosen.
Example: You cannot specify a line in a hashed table using an index.
When accessing table rows, you have two possibilities for a number of line operations and for loop
processing: You can either copy the data into a structure with the same type as the line type of the
internal table, or you can set a pointer to a table line and access the data in that line directly.
In particular, when you insert table lines, you can pass the data to be inserted using another internal
table.
Note that all statements either set a return value or trigger a runtime error. If a return code has been
set, it is stored in the sy-subrc field after the statement has been executed. Runtime errors are only
triggered if the data passed at runtime are crucial in determining whether or not the operation can be
performed.
Example:
You try to insert a new line into a SORTED table using an index. If you insert the new line in exactly
the right place in a sorted table, the system performs the operation. If not, the system returns a
runtime error.
SAP AG
BC401
3-19
YDUBD
IV!
YDUBD
IV!
IV!
IV!
YDUBD
7LPH
SAP AG 2002
You can create a pointer to a data object in ABAP using a field symbol.
First, declare a data object using the ),(/'6<0%2/6 statement. This data object can contain a
pointer to another data object at runtime. Where possible, you should give the field symbol the same
type as the data object (7<3( L, in this example).
Note that the angle brackets (<>) are part of the name of the field symbol: In this example, the name
is <fs>.
To point a field symbol at a data object, you must assign it to the object GDWDBREMHFW using the
$66,*1 GDWDBREMHFW 72 IV! statement.
You can use the field symbol to access the content of the data object to which it points - either to
read or to change this content.
You can "redirect" a field symbol to a different data object at runtime using the $66,*1 statement.
SAP AG
BC401
3-20
y w v
7 xsu uwlPsBtsV
i
k
j
r
h g xq
g
{ g
zk
IV!
LWBVIOLJKW
IV!
LWBVIOLJKW
:5,7( IV!FRQQLG
IV!FDUULG
/+
IV!
LWBVIOLJKW
SAP AG 2002
In the above example, a field symbol is assigned the line type of an internal table. This makes it
possible to assign a table line to this field symbol. The syntax required for this is discussed later in
this unit.
After a field symbol has been assigned a line, it is also possible to access the individual column
values of the assigned line.
As well as being able to read the data contents, you can also change the contents of the individual
components.
SAP AG
BC401
3-21
,QVHUW
.H\ IURP ZD
VSHFLILHV WKH OLQH
LWDE
ZD
ILOO ZRUNDUHD
ZD
ILOO ZRUNDUHD
ZD
ZD
LWDE
>Q@
>Q@
LWDE
Q
> )520 Q 72 Q @
WDUJHW
2QO\ LI LWDE LV
DQ LQGH[ WDEOH
WDUJHW
Q
SAP AG 2002
,172 7$%/(
,172 7$%/( LWDE
Using INSERT wa INTO TABLE itab, you insert the line whose data are stored in wa into the
internal table. The record is inserted with reference to the table key. In STANDARD tables, the new
line is appended to the end of the table.
Using INSERT wa INTO itab INDEX n, you insert the data from ZD into the internal table at
the line with the index Q. If Q is an index value within the internal table, the lines with the index
value greater than or equal to Q are pushed downwards. If n-1 is greater than the number of lines in
the internal table, the line is not inserted.
Always add new lines to a SORTED table using the key. If you try to insert a line into a SORTED
table using the index, you will not succeed unless the line has exactly the same index number as it
would have when correctly sorted. Otherwise, a runtime error occurs.
Use INSERT LINES OF itab1 INTO TABLE itab2 to insert the lines of the internal table
itab1 into the internal table itab2. Also, if itab1 is an index table, you can limit the number of
lines to be inserted using an index interval (FROM n1 TO n2).
If your target table is an index table, you can use INSERT LINES OF itab1 INTO itab2 to
insert the lines of the internal table itab1 into the internal table itab2. You can then specify the
line index from which the lines are to be inserted using INDEX n. Also, if itab1 is an index table,
you can limit the number of lines to be inserted using an index interval (FROM n1 TO n2).
SAP AG
BC401
3-22
LWDE
Q
,QGH[
,172
5($' 7$%/( LWDE ,1'(; Q ,172 ZD > RSWLRQV @
,1'(;
> RSWLRQV @
&203$5,1*
FRPSBOLVW
75$163257,1* WUDQVBOLVW
FRPSBOLVW
%HOLHELJH )HOGOLVWH
$Q\ ILHOG OLVW
( I IP
( $// ),(/'6
WUDQVBOLVW
( I IN
( 12 ),(/'6
SAP AG 2002
You can read single table lines from index tables using the READ TABLE itab INDEX n INTO
wa statement. After INDEX, enter the index of the line you want to read. If the system was able to
read the line, it sets the return code to sy-subrc = 0 and stores the data from the line in wa. The
total number of lines is then in sy-tfill, the length of the lines in sy-tleng. If the system
could not read the line, the return code sy-subrc is unequal to 0. In this case, the content of wa is
not changed.
Use the option TRANSPORTING addition to specify the columns for which you want to transport
data:
If you do not want to read any data, but simply want to ascertain whether or not line n exists
(evaluating the return value sy-subrc), use the TRANSPORTING NO FIELDS addition.
If you want to read some of the columns in the line only, specify them after TRANSPORTING.
Separate each column name with a space.
You can use the optional addition COMPARING I ... IP to ascertain whether or not the line to
be read Q has specific column contents: To do this, copy the value of all columns that point to the
records you want to read, into wa and list the columns after the COMPARING addition to the READ
statement. If the system was able to read the line and if all the columns listed contain the values
stored in ZD, it sets the return code sy-subrc to zero. If the system was able to read the line, but if
one or more of the columns does not contain the value stored for it in ZD, it sets the return code sysubrc to two. If it could not read the line, it sets the return code to greater than two.
The COMPARING ALL FIELDS addition provides a shorter syntax for comparing all the columns.
SAP AG
BC401
3-23
LWDE
ZD
$V LQ LQGH[
DFFHVV
VHDUFKBFODXVH
)520 ZD
7DEOH NH\
$Q\ ILHOG
FRQWHQWV
I NQ
IQ
IP
SAP AG 2002
You can read individual lines from any kind of table using READ TABLE itab NH\ INTO wa.
Use either a table key, or a comparison for some of the columns, as the search criterion NH\.
You can specify a formulated search condition to be applied to any columns using READ TABLE
itab WITH KEY ....
However, you can only use dynamically formulated read accesses using the key and the READ
TABLE itab FROM wa statement.
SAP AG
BC401
3-24
IV!
LWDE
.H\
RU
,QGH[
,1'(; Q
NH\
$66,*1,1* !
IV!
,QGH[
.H\
You can use either a work area or a field symbol to access the individual lines in a table you want to
read.
To use a field symbol, first define it using FIELD-SYMBOLS <fs>. Give it the same type as the
line type of the internal table that you want to read.
Example: FIELD-SYMBOLS <fs> LIKE LINE OF itab.
Set the pointer to the correct line using the ASSIGNING <fs> addition instead of INTO. You can
use any search criterion you want.
You can access the components of the structure directly using the component names in the line type:
You can directly address elementary components in the line type using <fs>-field_1.
If the line type contains structured components (a structure or internal table),
<fs>-component_name points to the entire structured component. To access the subcomponents of this structured component, you need another field symbol.
Note:
If you are searching for a specific string in an internal table, you can also use the SEARCH statement.
For more details, refer to the keyword documentation for the SEARCH statement.
SAP AG
BC401
3-25
LWDE
ZD
ZD
LWDE
ZD
ZD
ILOO ZRUNDUHD
ILOO ZRUNDUHD
LWDE
:+(5(
ORJBH[SU
ZD
75$163257,1* I I
:+(5( ORJBH[SU
SAP AG 2002
The MODIFY TABLE itab FROM wa statement allows you to change the content of RQH line of
the internal table itab. The runtime system specifies the line to be changed using the key values
from the work area ZD and changes the non-key fields using the other fields. If your table has a nonunique key, the system changes the first entry only (using a linear search algorithm in STANDARD
tables, and a binary search algorithm in SORTED tables). If you want to change only some of the
fields (that is, columns) in a line, you must specify these after TRANSPORTING.
You can change the Qth line in an index table using MODIFY itab FROM wa INDEX n. Note
that all the fields of the structure wa will be copied. Since changes to key fields in SORTED and
HASHED tables can cause non-catchable runtime errors, you PXVW use the TRANSPORTING addition
in this case.
If you want to make the same changes to several lines in a table, use the MODIFY itab FROM wa
TRANSPORTING f1 f2 ... WHERE log_expr statement. Specify all the columns to be
changed after TRANSPORTING. You must place the new values for these columns in the work area
wa. Specify a condition for the line using the WHERE clause.
SAP AG
BC401
3-26
.H\
LWDE
IV!
RU
IV!ILHOGB
,QGH[
)6!ILHOGB
IV!ILHOGB
IV!ILHOGB
,1'(; Q
NH\
$66,*1,1* IV!
,QGH[
.H\
SAP AG 2002
Instead of changing an individual line in a table using MODIFY, you can also change it using field
symbols. First use READ TABLE ... ASSIGNING <fs> to assign a field symbol to the line to
be changed. Then change the components directly using the field symbol components <fs>field_1 = ....
When you assign the field symbol using READ TABLE ... ASSIGNING you can specify the line
to be read using a key (WITH TABLE KEY), index (for index tables), or a condition (WITH KEY).
Note that you cannot change key fields in SORTED or HASHED tables. - trying to do so causes a
runtime error.
SAP AG
BC401
3-27
'HOHWH
ILOO ZRUNDUHD
7DEOH NH\
LWDE
ZD
NH\
([SOLFLW
( )520 ZD
( :,7+ 7$%/( .(<
N
I NQ
LWDE
Q
ZD
:+(5(
ORJBH[SU
IQ
SAP AG 2002
Use the READ TABLE itab INDEX n INTO wa statement to delete single table lines from
index tables. Specify the line you want to delete using the table key. There are two ways of doing
this:
Copy the key field values of the entry you want to delete into the work area wa and use DELETE
TABLE itab FROM wa.
Fill the key fields directly in the DELETE statement using the WITH TABLE KEY k = f
... kQ = fQ addition.
You can delete the Qth line from the index table itab using DELETE itab INDEX n
If you want to delete several lines from an internal table, use the DELETE itab WHERE
log_expr statement. The condition that specifies the lines you want to delete is declared in
ORJBH[SU, in the WHERE clause (where ORJBH[SU is any logical expression applied to the
columns).
SAP AG
BC401
3-28
/RRS 3URFHVVLQJ
UHVXOW
ZD
LWDE
UHVXOW
( ,172 ZD
( $66,*1,1* IV!
IV!
(1'/223
(1'/223
,QGH[ WDEOH
V\WDEL[ FRQWDLQV OLQH
LQGH[ RI FXUUHQW OLQH
SAP AG 2002
Use the LOOP AT itab UHVXOW ... ENDLOOP statement to perform loop processing on the
lines of the internal table itab. The system then executes the statement block between LOOP AT
itab ... and ENDLOOP for each loop pass. UHVXOW stands for:
INTO wa:
The system copies the table line it has processed in each loop pass into the work area wa
ASSIGNING <fs>:
The relevant line is assigned to the field symbol <fs>.
n
With both variants you can use the TRANSPORTING NO FIELDS addition. No data is copied. Use
this addition if, for example, you simply want to determine the number or indexes of the lines
processed by the loop.
Specify the number of lines that the loop is to process using a WHERE clause (condition for any
columns).
In an index table, limit the number of lines processed in the loop by declaring an index interval.
While the loop is being processed, the system field sy-tabix contains the index of the table row of
the current loop pass. The total number of lines is in sy-tfill, the length of the lines in sytleng.
Note that in index tables, you can perform all the line operations (which you previously performed
on the line specified in the INDEX n addition) ZLWKRXW this addition in the loop. The operation is
then performed on the current line.
SAP AG
BC401
3-29
2YHUYLHZ RI 2SHUDWLRQV
~} }
799A5y|
~} ~ ~
A79Pwf9}d|
w f
g 9i
9ih T p ww
p
Th
02',)<
'(/(7(
/223 $7
V V
99e
5($' 7$%/(
9
VqBsTV%9a
i
9w9i
Va
,16(57
9999i
i
,16(57
799fg
~} ~
6257
5($' 7$%/(
02',)<
'(/(7(
/223 $7
SAP AG 2002
Index accesses (such as APPEND, INSERT ... INDEX, LOOP AT ... FROM ... TO) are
possible for standard and sorted tables. However, caution is advised when using INSERT or
APPEND on sorted tables; this would violate the sort sequence and cause a runtime error.
You can use the SORT statement to sort standard and hashed tables. Sorted tables are sorted by the
runtime system.
You can use key accesses with any table type, but their effect differs. A key access with INSERT has
the same effect on standard tables as an APPEND, that is the relevant line is appended to the end of
the table. In a sorted table however, the record is inserted in accordance with the sort sequence. In
the case of a hashed table, the line is appended, but the system also changes the hash index
internally.
SAP AG
BC401
3-30
1RWHV RQ SHUIRUPDQFH
1RWHV RQ SHUIRUPDQFH
SAP AG 2002
SAP AG
BC401
3-31
:RUN DUHD
KHDGHU
LWDE
LWDE
%RG\
As well as the internal tables we have discussed, you can define LQWHUQDO WDEOHV ZLWK D KHDGHU.
These consist of a pair of components - the internal table itself (the ERG\) and the work area
(KHDGHU), with the same line type. The header and the body have the same name, which sometimes
simplifies the syntax for table accesses.
There are several possible syntax variants used to define internal tables with header lines, some of
which are shown in the slide.
Internal tables with header lines are the oldest form of internal table.
SAP AG
BC401
3-32
6(/(&7237,216
6(/(&7237,216
'$7$
VRBFDUU /,.(
5$1*( 2) FDUULG
5$1*( 2)
w 77T
Ve
w %T7
9f7
%Pq797
nT
e2qT9ne
SAP AG 2002
Use the 6(/(&7237,21 statement to create an internal table with a header, for which the runtime
system automatically creates an input dialog for value sets for a selection screen. The system
automatically inserts the appropriate entries in the internal table, from the user input. For more
details, refer to the keyword documentation for the 6(/(&7237,216 statement.
As of SAP R/3 Basis Release 4.6A onwards, you can use the 5$1*( 2) addition to the 7<3(6 and
'$7$ statements to define a corresponding (internal) table without a header line.
SAP AG
BC401
3-33
1RWHV RQ SHUIRUPDQFH
1RWHV RQ SHUIRUPDQFH
SAP AG 2002
SAP AG
BC401
3-34
7DEOH NLQG
67$1'$5'
0XVW EH FRPSOHWHO\
TXDOLILHG
6257('
+$6+('
7DEOH VFDQ
%LQDU\
VHDUFK
+DVK
DOJRULWKP
7DEOH NLQG
67$1'$5'
&RPSOHWHSDUW NH\
OHIWDOLJQHG ZLWKRXW JDSV
%\ TXDOLILHG NH\
6257('
+$6+('
%LQDU\
VHDUFK
7DEOH VFDQ
$Q\ FRPSRQHQW
FRQGLWLRQ
7DEOH
VFDQ
7DEOH
VFDQ
SAP AG 2002
Whenever you want to read individual table lines by declaring a FRPSOHWH key, use the READ
TABLE ... WITH 7$%/( KEY statement (fastest single record access by key). The runtime
system supports this syntax variant especially for SORTED and HASHED tables. If the table is a
STANDARD table, the runtime system performs a WDEOH VFDQ.
The same applies if you have copied the values from DOO key fields of the entry to be read into the
work area wa and are then use READ TABLE itab FROM wa.
The runtime system carries out the syntax variant READ TABLE ... WITH KEY (read an entry
after applying DQ\ condition) using a WDEOH VFDQ
The only exception to this rule applies to SORTED tables, if you fill the first Q key fields with "="
(no gaps), where Q <= number of key fields.
With standard tables however, you can also sort correspondingly using SORT and then use the
BINARY SEARCH addition.
6XPPDU\
n
Whenever possible, use READ TABLE ... WITH 7$%/( KEY or the variant with a
correspondingly-filled work area.
If you need to use READ TABLE ... WITH KEY, make your internal table a SORTED table.
SAP AG
BC401
3-35
7DEOH NLQG
67$1'$5'
7DEOH VFDQ
6257('
%LQDU\ VHDUFK IRU
VWDUWLQJ SRLQW
WKHQ ORRS RQO\
WKURXJK JURXS
OHYHO
+$6+('
7DEOH VFDQ
7DEOH VFDQ
SAP AG 2002
The runtime system generally processes loops with a WHERE clause by performing a WDEOH VFDQ that is, determining whether the condition in the WHERE clause is true for each line in the table.
SORTED tables are the only exception to this rule. For these, the runtime system optimizes the
runtime under the following condition:
In the WHERE clause, the first Q key fields are filled with a "=" (no gaps). (Q is less than or equal to
the number of all key fields). As a result, the loop is only performed on the lines that match the
condition in the WHERE clause. Since the table is sorted, the first line can be specified to optimize
performance at runtime (using a binary search).
SAP AG
BC401
3-36
&$55,'
$$
/+
&211,' &,7<)520
/+
8$
8$
)5$1.)857
1(: <25.
)5$1.)857
)5$1.)857
75$163257,1* 12 ),(/'6
%,1$5< 6($5&+
VWDUWOLQH
V\WDEL[
1(: <25.
75$163257,1* 12 ),(/'6
%,1$5< 6($5&+
HQGOLQH
V\WDEL[
SAP AG 2002
/+
8$
8$
$$
%(5/,1
)5$1.)857
)5$1.)857
,QGH[FRQWUROOHG ORRS
/223 $7 ,172
)520 VWDUWOLQH WR HQGOLQH
(1'/223
7R UHFDS
If you program a loop through a standard table to match a specific field criterion, the runtime system
always executes a WDEOH VFDQ.
You can use the algorithm described here to program the runtime behavior of sorted tables:
First you must sort the standard table by the desired criterion, so that you can subsequently ascertain
the starting and end points in a ELQDU\ VHDUFK.
(The line indexes are available in the system field sy-tabix.)
Finally, you can use these values to program an index-controlled loop.
Since the SORT and READ TABLE statements require additional runtime, this procedure is only
useful if the loop can be repeated several times according to the field criterion.
6XPPDU\Use SORTED tables if you want to implement partial sequential loops on internal tables
(where the first Q key fields are filled with "=") or use the above algorithm.
SAP AG
BC401
3-37
IV!
&RS\
ZD
IV!
&RS\
FRO
FRO
FRO
FRO
LWDE
FRO
FRO
FRO
FRO
SAP AG 2002
Instead of READ TABLE ... INTO, you can use the READ TABLE ... ASSIGNING variant.
This offers better performance at runtime for pure read accesses with a line width greater than or
equal to 1000 bytes. If you then change the read line using MODIFY, READ ... ASSIGNING
already improves runtime with a line width of 100 bytes.
The same applies to LOOP ... INTO in comparison with LOOP ... ASSIGNING. The LOOP
... ASSIGNING variant offers better performance at runtime for any loop of five loop passes or
more.
Both field symbol variants are much faster than work area variants, in particular when you use nested
internal tables. This is because, if you use work areas instead, the whole inner internal table is copied
(unless you prevent this by using a TRANSPORTING addition).
Always assign a type to field symbols, if you know their static type (again, for performance reasons).
1RWH
If you use READ TABLE ... ASSIGNING the field symbol points to the originally assigned
table line, even DIWHU the internal table has been sorted.
Note that when using field symbols, you cannot change key fields in SORTED or HASHED tables.
Trying to do so causes a runtime error.
SAP AG
BC401
3-38
SAP AG 2002
SAP AG
BC401
3-39
([HUFLVH
8QLW 8VLQJ ,QWHUQDO 7DEOHV
7RSLF
7HPSODWH
0RGHO VROXWLRQ
=%&B
B63/,7B,7$%
6$3%&B'726B63/,7B675,1*
6$3%&B7$%6B63/,7B,7$%
1-1
1-2
1-3
1-4
1-5
SAP AG
Copy your solution for the exercise in the chapter 'DWD 7\SHV DQG 'DWD 2EMHFWV LQ
'HWDLO, =%&B B63/,7B675,1*, or the corresponding model solution
6$3%&B'726B63/,7B675,1* and give it the new name
=%&B B63/,7B,7$%.
Ensure that the function module %&B*(7B6(3B675,1* creates a character
string consisting of 30 data records. For this you must assign a suitably typed
constant (suggested name FBQXPEHU) to the parameter LPBQXPEHU.
Define a single-column internal table of the type 6WDQGDUG. The column component
is to have the data type VWULQJ (suggested name: LWBVHWV).
Change the 63/,7 statement so that always one partial character string that
contains a data record is placed into a row of its internal table.
Ensure that your internal table containing the partial character strings is used for the
subsequent splitting of the individual data record and output of the components in a
loop.
BC401
3-40
([HUFLVH 2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV
7RSLF
7DEOH 7\SHV
7HPSODWH
0RGHO VROXWLRQ
=%&B
B7$%.,1'
6$3%&B7$%6B63/,7B,7$%
6$3%&B7$%6B7$%.,1'
2-1
2-2
2-3
Define an internal table in such a way that it can contain flight data sorted by
airline, flight number, and flight date (suggested name LWBIOLJKWV).
Insert the separated data records (contents of the structure ZDBIOLJKW) into this
internal table, instead of displaying them. (You can still use the output statement
later.)
Make use of the return value V\VXEUF for the ,16(57 statement to ascertain
whether or not a data record appears twice. For this, you must have defined the key
of your internal table accordingly.
Define an additional internal table for the data records that appear twice (suggested
name LWBGRXEOHV).
Insert the duplicate data records into this internal table.
2-4
Display the contents of both internal tables with the flight data.
SAP AG
BC401
3-41
([HUFLVH 2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV
7RSLF
7HPSODWH
0RGHO VROXWLRQ
=%&B
B352&(66B'$7$
6$3%&B7$%6B7$%.,1'
6$3%&B7$%6B352&(66B'$7$
3-1
3-2
Define a selection screen parameter for entering the key date (suggested name
SDBGDWH).
Specify the default value of the key date as 30 days in the future.
The user must still be able to choose the key date. Error message of the
message class EF is to be displayed if the selected key date is in the past.
3-3
Make sure that the data is first displayed sorted by flight date.
3-4
Ensure that the individual flight dates of the internal table LWBIOLJKWV are
displayed in color according to the following criteria: Use the )250$7 &2/25
FROB statement and load the type group FRO. (You can reverse any color set
using the )250$7 5(6(7 statement.)
SAP AG
Display flights that occurred in the past with the background color
col_negative.
Display flights that occurred between todays data and the key date with the
background color col_total.
BC401
3-42
3-5
Display flights that occurred in the past with the background color
col_positive.
Define a selection screen check box for displaying the data with the standard tool
SAP Grid Control (suggested name SDBDOY, addition $6 &+(&.%2;).
Ensure that the data is only displayed in the usual ABAP list if the user does QRW
check the SAP Grid Control box.
Otherwise, proceed as follows:
3-5-1 Call the function module %&B$/9B/,67B287387. Terminate the
program if the function module raises an exception. (In this case, there is a
problem with the system configuration.)
Detailed information on using the SAP Grid Control is not part of
this course. For this reason, you use a function module that
encapsulates all the necessary technical details, which you can treat
as a black box.
For more information on the SAP Grid Control, see:
3-5-2 Find out about the types of the two internal tables that can/must be
transferred to the function module as a parameter.
Define two corresponding internal tables and an appropriate work area in
your program (suggested names LWBFROBIOLJKWV, LWBFROBGRXEOHV,
and ZDBFROBIOLJKW).
3-5-3 The line type of the internal table that you must/can transfer contains a
column &2/25, in which you can set the color values FROBQHJDWLYH,
FROBWRWDO, FROBSRVLWLYH, and FROBEDFNJURXQG for every single
line.
Copy the contents of the tables LWBIOLJKWV and LWBGRXEOHV to the
internal tables LWBFROBIOLJKWV and LWBFROBGRXEOHV respectively.
Use two loops to do this, and to fill the &2/25 column.
LWBIOLJKWV:
/RJLFDO FRQGLWLRQ
&RORU YDOXH
col_negative
col_total
col_positive
BC401
3-43
6ROXWLRQ
8QLW 8VLQJ ,QWHUQDO 7DEOHV
7RSLF
5(3257
VDSEFBWDEVBVSOLWBLWDE
TYPES:
BEGIN OF st_flight_c,
mandt(3)
TYPE c,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate(8)
TYPE n,
price(20)
TYPE c,
currency(5)
TYPE c,
planetype(10)
TYPE c,
seatsmax(10)
TYPE n,
seatsocc(10)
TYPE n,
paymentsum(22) TYPE c,
seatsmax_b(10) TYPE n,
seatsocc_b(10) TYPE n,
seatsmax_f(10) TYPE n,
seatsocc_f(10) TYPE n,
END OF st_flight_c,
BEGIN OF st_flight,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate
TYPE d,
price(9)
TYPE p DECIMALS 2,
currency(5)
TYPE c,
planetype(10) TYPE c,
seatsmax
TYPE i,
seatsocc
TYPE i,
END OF st_flight.
&2167$176 FBQXPEHU 7<3( L 9$/8(
SAP AG
BC401
3-44
DATA:
datastring
TYPE string,
set_string
TYPE string,
TYPE st_flight.
'$7$
START-OF-SELECTION.
CALL FUNCTION BC401_GET_SEP_STRING
EXPORTING
LPBQXPEHU
FBQXPEHU
IM_TABLE_NAME
= SFLIGHT
IM_SEPARATOR
= #
IM_UNIQUE
= X
IMPORTING
ex_string
= datastring
EXCEPTIONS
no_data
= 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE a038(bc401).
ENDIF.
SAP AG
BC401
3-45
SAP AG
BC401
3-46
6ROXWLRQ 2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV
7RSLF
5(3257
7DEOH 7\SHV
VDSEFBWDEVBWDENLQG
TYPES:
BEGIN OF st_flight_c,
mandt(3)
TYPE c,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate(8)
TYPE n,
price(20)
TYPE c,
currency(5)
TYPE c,
planetype(10)
TYPE c,
seatsmax(10)
TYPE n,
seatsocc(10)
TYPE n,
paymentsum(22) TYPE c,
seatsmax_b(10) TYPE n,
seatsocc_b(10) TYPE n,
seatsmax_f(10) TYPE n,
seatsocc_f(10) TYPE n,
END OF st_flight_c,
BEGIN OF st_flight,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate
TYPE d,
price(9)
TYPE p DECIMALS 2,
currency(5)
TYPE c,
planetype(10) TYPE c,
seatsmax
TYPE i,
seatsocc
TYPE i,
END OF st_flight.
CONSTANTS c_number TYPE i VALUE 30.
SAP AG
BC401
3-47
DATA:
datastring
TYPE string,
set_string
TYPE string,
TYPE st_flight.
DATA:
it_sets TYPE STANDARD TABLE OF string
WITH NON-UNIQUE DEFAULT KEY
INITIAL SIZE c_number,
LWBIOLJKWV 7<3( 6257(' 7$%/( 2) VWBIOLJKW
START-OF-SELECTION.
CALL FUNCTION BC401_GET_SEP_STRING
EXPORTING
im_number
= c_number
IM_TABLE_NAME
= SFLIGHT
IM_SEPARATOR
= #
,0B81,48(
VSDFH
IMPORTING
ex_string
= datastring
EXCEPTIONS
no_data
= 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE a038(bc401).
ENDIF.
SAP AG
BC401
3-48
(1',)
ENDLOOP.
* output:
*********
/223 $7 LWBIOLJKWV ,172 ZDBIOLJKW
:5,7(
ZDBIOLJKWFDUULG
ZDBIOLJKWFRQQLG
ZDBIOLJKWIOGDWH ''00<<<<
SAP AG
BC401
3-49
ZDBIOLJKWFXUUHQF\
ZDBIOLJKWSODQHW\SH
ZDBIOLJKWVHDWVPD[
ZDBIOLJKWVHDWVRFF
(1'/223
6.,3
:5,7(
GXSOLFDWH GDWD UHFRUGV
GRE &2/25 &2/B+($',1*
/223 $7 LWBGRXEOHV ,172 ZDBIOLJKW
:5,7(
ZDBIOLJKWFDUULG
ZDBIOLJKWFRQQLG
ZDBIOLJKWIOGDWH ''00<<<<
ZDBIOLJKWSODQHW\SH
ZDBIOLJKWVHDWVPD[
ZDBIOLJKWVHDWVRFF
(1'/223
SAP AG
BC401
3-50
6ROXWLRQ 2SWLRQDO
8QLW 8VLQJ ,QWHUQDO 7DEOHV
7RSLF
5(3257 VDSEFBWDEVBSURFHVVBGDWD
TYPE-POOLS col.
TYPES:
BEGIN OF st_flight_c,
mandt(3)
TYPE c,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate(8)
TYPE n,
price(20)
TYPE c,
currency(5)
TYPE c,
planetype(10)
TYPE c,
seatsmax(10)
TYPE n,
seatsocc(10)
TYPE n,
paymentsum(22) TYPE c,
seatsmax_b(10) TYPE n,
seatsocc_b(10) TYPE n,
seatsmax_f(10) TYPE n,
seatsocc_f(10) TYPE n,
END OF st_flight_c,
BEGIN OF st_flight,
carrid(3)
TYPE c,
connid(4)
TYPE n,
fldate
TYPE d,
price(9)
TYPE p DECIMALS 2,
currency(5)
TYPE c,
planetype(10) TYPE c,
seatsmax
TYPE i,
seatsocc
TYPE i,
END OF st_flight.
SAP AG
BC401
3-51
TYPE string,
set_string
TYPE string,
TYPE st_flight.
DATA:
it_sets TYPE STANDARD TABLE OF string
WITH NON-UNIQUE DEFAULT KEY
INITIAL SIZE c_number,
it_flights TYPE SORTED TABLE OF st_flight
WITH UNIQUE KEY fldate carrid connid
INITIAL SIZE c_number,
it_doubles TYPE SORTED TABLE OF st_flight
WITH NON-UNIQUE KEY fldate carrid connid
INITIAL SIZE c_number,
$6 &+(&.%2; '()$8/7
;
/2$'2)352*5$0
SDBGDWH
V\GDWXP
$7 6(/(&7,216&5((1
,) SDBGDWH V\GDWXP
0(66$*( HEF
(1',)
SAP AG
BC401
3-52
START-OF-SELECTION.
CALL FUNCTION BC401_GET_SEP_STRING
EXPORTING
im_number
= c_number
IM_TABLE_NAME
= SFLIGHT
IM_SEPARATOR
= #
im_unique
= space
IMPORTING
ex_string
= datastring
EXCEPTIONS
no_data
= 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE a038(bc401).
ENDIF.
SAP AG
BC401
3-53
* output:
*********
,) SDBDOY
;
FROBQHJDWLYH
ZDBFROBIOLJKWFRORU
FROBWRWDO
ZDBFROBIOLJKWFRORU
FROBSRVLWLYH
(1',)
(1'/223
FROBEDFNJURXQG
(1'/223
&$// )81&7,21
%&B$/9B/,67B287387
(;3257,1*
LWBOLVW
LWBOLVW
(;&(37,216
FRQWUROBHUURU
27+(56
,) V\VXEUF !
LWBFROBIOLJKWV
LWBFROBGRXEOHV
(1',)
SAP AG
BC401
3-54
(/6(
LOOP AT it_flights INTO wa_flight.
,) ZDBIOLJKWIOGDWH V\GDWXP
)250$7 &2/25
FROBQHJDWLYH
)250$7 &2/25
FROBWRWDO
)250$7 &2/25
FROBSRVLWLYH
(1',)
WRITE: /
wa_flight-carrid,
wa_flight-connid,
wa_flight-fldate DD/MM/YYYY,
wa_flight-price CURRENCY wa_flight-currency,
wa_flight-currency,
wa_flight-planetype,
wa_flight-seatsmax,
wa_flight-seatsocc.
ENDLOOP.
)250$7 5(6(7
SKIP.
WRITE: / duplicate data records:(dob) COLOR COL_HEADING.
LOOP AT it_doubles INTO wa_flight.
WRITE: /
wa_flight-carrid,
wa_flight-connid,
wa_flight-fldate DD/MM/YYYY,
wa_flight-price CURRENCY wa_flight-currency,
wa_flight-currency,
wa_flight-planetype,
wa_flight-seatsmax,
wa_flight-seatsocc.
ENDLOOP.
SAP AG
BC401
3-55
SAP AG 2002
SAP AG
BC401
4-1
SAP AG 2002
SAP AG
BC401
4-2
SAP AG
BC401
4-3
SAP AG 2002
Y X VU T
c b
7X (a U X a `(W(SR
## !" $
& 95 B(8 A
9@() 75 1) 31) (%
8 6 4 2 0 '&
9' 84 5 C
Q H
') & 4 @5 IPB 48 8 C
D
FG) 8 & 5 E7@A 48 8 C
&DOOLQJ SURJUDPV
&DOOLQJ SURJUDPV
0HPRU\ PDQDJHPHQW
0HPRU\ PDQDJHPHQW
3DVVLQJ GDWD
3DVVLQJ GDWD
SAP AG 2002
SAP AG
BC401
4-4
&DOOLQJ 3URJUDPV
7LPH
0DLQ
PHPRU\
0DLQ
PHPRU\
,QVHUW SURJUDP
5HVWDUW
(QG LQVHUW
1HZ SURJUDP
,QVHUWLRQ
3URJUDP
3URJUDP
SAP AG 2002
There are two ways of starting an ABAP program from another ABAP program that is already
running:
The called program is inserted, that is the current program is interrupted to run the new one.
The called program is executed, and afterwards, processing returns to the program that called it.
Complete ABAP programs within a single user session can only run sequentially. We refer to this
technique as VHTXHQWLDO FDOOLQJ.
If you want to run functions in parallel, you must use function modules. For further information
about this technique, refer to the course %& &RPPXQLFDWLRQ ,QWHUIDFHV LQ $%$3 or the
documentation for the &$// )81&7,21 67$57,1* 1(: 7$6. statement.
SAP AG
BC401
4-5
68%0,7 SURJBQDPHB
$1' 5(7851
3URJUDP
5HVWDUW
SURJBQDPHB
352*5$0
,QVHUWLRQ
,QVHUWLRQ
68%0,7 SURJBQDPHB
9,$ 6(/(&7,216&5((1
$1' 5(7851
SURJBQDPHB
352*5$0
/LVW
)
/LVW
)
6HOHFWLRQ 6FUHHQ
SURJBQDPHB
/LVWH
352*5$0
)
)
SAP AG 2002
SAP AG
BC401
4-6
&DOOLQJ D 7UDQVDFWLRQ
3URJUDP
3URJUDP 7UDQVDFWLRQ
5HVWDUW
6FUHHQ
/($9( 72 75$16$&7,21 T_CODE
>$1' 6.,3 ),567 6&5((1@
6FUHHQ
)
,QVHUWLRQ
&$// 75$16$&7,21 T_CODE
>$1' 6.,3 ),567 6&5((1@
6FUHHQ
6FUHHQ
)
TCODE
6$30 WBQDPH
/($9( 352*5$0
6$30 WBQDPH
/($9( 352*5$0
SAP AG 2002
SAP AG
BC401
4-7
&DOOLQJ SURJUDPV
&DOOLQJ SURJUDPV
0HPRU\ PDQDJHPHQW
0HPRU\ PDQDJHPHQW
3DVVLQJ GDWD
3DVVLQJ GDWD
SAP AG 2002
SAP AG
BC401
4-8
3URJUDP
$%$3 PHPRU\
$%$3 PHPRU\
6$3 PHPRU\
SAP AG 2002
The way in which the main memory is organized from the programs point of view can be
represented in the above logical model. There is a distinction between internal and external
sessions:
Generally, an H[WHUQDO VHVVLRQ is connected to an R/3 window. You can create a new session
by choosing 6\VWHP 1HZ 6HVVLRQ or by entering R<T_CODE> in the command field. You
can have up to six external sessions open simultaneously in one terminal session.
External sessions are subdivided into LQWHUQDO VHVVLRQV (placed on a VWDFN). Each program
that you run occupies its own internal session. Each external session can contain up to nine
internal sessions.
Data for a program is RQO\ YLVLEOH ZLWKLQ an internal session. The visibility of the data is generally
restricted to the relevant program.
The following slides illustrate how the stack inside an external session changes with various
program calls.
SAP AG
BC401
4-9
,QVHUWLQJ D 3URJUDP
ABAP memory 1
ABAP memory 2
SAP memory
SAP AG 2002
SAP AG
BC401
4-10
,QVHUWLQJ D 3URJUDP
User terminal session
,QVHUWLRQ
3URJUDP
ABAP memory 2
ABAP memory 1
SAP memory
SAP AG 2002
When you insert a program, the system creates a new internal session, which in turn creates a new
program context.
The new session is placed on the stack The program context of the calling program also remains
intact.
SAP AG
BC401
4-11
ABAP memory 1
ABAP memory 2
SAP memory
SAP AG 2002
When the called (inserted) program finishes, its internal session (the top one in the stack) is deleted.
Processing is resumed in the next-highest internal session in the stack.
SAP AG
BC401
4-12
3URJUDP
ABAP memory 2
ABAP memory 1
SAP memory
SAP AG 2002
When you end a program and start a new one, there is a distinction between calling an executable
program and calling a transaction, with regard to memory areas.
SAP AG
BC401
4-13
3URJUDPP
ABAP memory 2
ABAP memory 1
SAP memory
SAP AG 2002
If you call an executable program using its program name (terminating the calling program), the
system deletes the internal session of the program that you are terminating (the top one from the
stack).
The system creates a new internal session, which in turn creates the program context of the called
program.
The new session is placed on the stack Existing program contexts remain intact. The topmost
internal session on the stack is replaced.
SAP AG
BC401
4-14
5HVWDUWLQJ D 7UDQVDFWLRQ
User terminal session
3URJUDP
ABAP memory 2
$%$3 PHPRU\
SAP memory
SAP AG 2002
SAP AG
BC401
4-15
5HVWDUWLQJ D 7UDQVDFWLRQ
User terminal session
5HVWDUW
FRPSOHWH
LQLWLDOL]DWLRQ
ABAP memory 2
$%$3 PHPRU\
,QWHUQHU 0RGXV
3URJUDP
SAP memory
SAP AG 2002
If you start a program using its WUDQVDFWLRQ FRGH (that is, if one was defined), DOO of the internal
sessions on the stack are deleted.
The system creates a new internal session, which in turn creates the program context of the called
program.
After the call, the ABAP memory is LQLWLDOL]HG.
SAP AG
BC401
4-16
&DOOLQJ SURJUDPV
&DOOLQJ SURJUDPV
0HPRU\ PDQDJHPHQW
0HPRU\ PDQDJHPHQW
3DVVLQJ GDWD
3DVVLQJ GDWD
SAP AG 2002
SAP AG
BC401
4-17
6$3 PHPRU\
6(7*(7 SDUDPHWHUV
3
$%$3 PHPRU\
3URJUDP $
,QWHUIDFH
1
3URJUDP %
DB
5
SAP AG 2002
There are various ways of passing data to programs running in separate internal sessions:
Database tables
The following slides deal with the first three of these methods.
For further information regarding the passing of data using database tables or the VKDUHG EXIIHU,
refer to the documentation for the (;3257 and ,03257 statements.
For further information on transferring data between an ABAP program and a presentation server,
refer to the documentation for the function modules *8,B83/2$' and *8,B'2:1/2$'.
SAP AG
BC401
4-18
3URJUDP %
Data
3URJUDP $
SAP AG 2002
When you call ABAP programs that have a standard selection screen, you can pass data for the
input fields in the call.
There are two ways to do this:
By specifying a variant for the selection screen when you call the program
By specifying values for the input fields when you call the program
SAP AG
BC401
4-19
e@ivuht$sq`$hfd
p qq r p i g e
wPPw
w
wPPw
w
ww
PPw
(G
y @ 1x
y y
The :,7+ addition to the 68%0,7 statement allows you to preassign values for parameters and
selection options on a standard selection screen of the called executable program. The abbreviations
"(4, 1(, ...; ,, (" have the same meanings as with selection options.
If you want to pass several selections for a selection option, you can use the 5$1*( statement
instead of individual :,7+ additions. The RANGES statement creates a selection table, which you
can fill as though it were a selection option. You then pass the whole table to the executable
program.
If you want to display the standard selection screen when you call the program, use the 9,$
6(/(&7,216&5((1 addition.
When you use the 68%0,7 statement, use the Pattern function in the ABAP Editor to insert an
appropriate statement pattern for the program you want to call. It automatically supplies the names
of the parameters and selection options that are available on the standard selection screen.
For further information about working with variants and about other syntax variants of the :,7+
addition, refer to the documentation for the 68%0,7 statement.
SAP AG
BC401
4-20
3URJUDP
$%$3 PHPRU\
$%$3 PHPRU\
3URJUDP
You can use SAP memory and ABAP memory to pass data between programs.
6$3 PHPRU\ is a user-specific memory area for storing ILHOG YDOXHV. It is only of limited value for
passing data between internal sessions. Values in SAP memory are retained for the duration of the
users terminal session. The memory can be used between sessions in the same terminal session. You
can use the contents of SAP memory as default values for screen fields. All H[WHUQDO VHVVLRQV can
use the SAP memory.
$%$3 PHPRU\ is also user-specific. There is a local ABAP memory for each external session. You
can use it to exchange DQ\ $%$3 YDULDEOHV (fields, structures, internal tables, complex objects)
between the LQWHUQDO sessions in any one external session.
When the user exits an external session (L in the command field), the corresponding ABAP
memory is automatically initialized or released.
SAP AG
BC401
4-21
$%$3 PHPRU\
0<B,'
352*5$0 S
'$7$ SBVSIOL 7<3( VSIOL
LWBVSIOL 7<3( 67$1'$5' 7$%/(
2) VSIOL
(;3257
ZDBIOL )520 SBVSIOL
ZDB
LWBVSIOL
72 0(025< ,'
0<B,'
ZDBIOL
LWBVSIOL
,QWHUQDO VHVVLRQ
0<B,'
352*5$0 S
'$7$ SBVSIOL 7<3( VSIOL
LWBVSIOL 7<3( 67$1'$5' 7$%/(
2) VSIOL
,03257
,03257
ZDBIOL 72 SBVSIOL
ZDB
LWBVSIOL
)520 0(025< ,'
0<B,'
SAP AG 2002
The (;3257 72 0(025< statement allows you to copy any number of ABAP data objects
with their current values to the ABAP memory (data cluster).
The ,' addition enables you to identify different clusters (maximum of 60 characters).
If you use a new (;3257 72 0(025< statement for an existing data cluster, the new one will
overwrite the old.
The ,03257 )520 0(025< ,' statement allows you to copy the data from the
ABAP memory into corresponding data objects of your ABAP program.
It is also possible to only import parts of data clusters using ,03257.
The data objects that are to receive the data from the ABAP memory cluster must have the same
types in both the calling and the called programs.
To release a data cluster, use the )5(( 0(025< ,' statement.
Bear in mind that when you call programs using transaction codes, you can RQO\ use the ABAP
memory to pass data ZKHQ LQVHUWLQJ (&$// 75$16$&7,21).
SAP AG
BC401
4-22
Program A
$LUOLQH
RU
/+
&RQQHFWLRQ
6(7
&21
),(/' VG\QBFRQQFRQQLG
&$// 75$16$&7,21
6$3 PHPRU\
&$5
&21
/+
Program B
/+
&RQQHFWLRQ
$LUOLQH
*(7
RU
&21
),(/' VG\QBFRQQFRQQLG
SAP AG 2002
You can define memory areas (parameters) in the SAP memory in various ways:
By creating input/output fields with reference to the ABAP Dictionary. These take the
parameter name of the data element to which they refer.
Alternatively, you can enter a name in the attributes of the input/output fields.
Here, you can also choose whether the entries from the field should be transferred to the
parameter (SET), or whether the input field should be filled with the value from the parameter
(GET).
To find out about the names of the parameters assigned to input fields, display the field help for
the field (F1), then choose 7HFKQLFDO LQIR.
You can also fill the memory areas directly using the 6(7 3$5$0(7(5 ,' PAR_ID
),(/' var statement and read them using *(7 3$5$0(7(5 ,'
PAR_ID
),(/'
var
Finally, you can define parameters in the 2EMHFW 1DYLJDWRU and let the user fill them with
values.
SAP AG
BC401
4-23
3URJUDP 7UDQVDFWLRQ
'$7$
ELBLWDE 7<3( 7$%/( 2) EGFGDWD
ELBZD
7<3( EGFGDWD
T_CODE
ELBLWDE
ILOO ELBLWDE
FDOO RWKHU SURJUDP
&$// 75$16$&7,21
7B&2'(
86,1* ELBLWDE
,) V\VXEUF
(/6(
VW VFUHHQ
QG VFUHHQ
)
6$30 WBQDPH
/($9( 352*5$0
SAP AG 2002
You are processing in the foreground, but the input fields have not been filled using GET
parameters
You want to process the transaction in the background. In this case, you normally have to pass
function codes as well.
This technique is also RQH of the possible ways of transferring data from non-SAP systems.
To do so the internal table in the EGFGDWD format must be filled completely.
SAP AG
BC401
4-24
IQDP
IYDO
i d t e d tp p
g`fevs
p i w d v e e i ip
xeu`o t
e ip o g pp o i
`ihn`$serqh$np
qle lj e h
g m k i
e i ip g e pp o i
`hneo$ssqhnp
q gm k i
xe xj e h
i k k g e i
ez q hg
i g
$$t
{ {$g P7e
i k p i q
e i ip
e`neo$g
e pp o i
rqnrhnp
q g m p ~ {
$} {|
1RWH ZKHQ
ILOOLQJ
G\QEHJLQ
pp ip gpk
no$sq$f`y
'HVFULSWLRQ
G\QSUR
i t e i k
gd$exp `i `y
/HQJWK
SURJUDP
Wt$gp `i `y
i vi z i k
)LHOG QDPH
SAP AG 2002
Each screen that is to be processed and filled automatically in the transaction must be identified
by a line, in which only the fields SURJUDP, G\QSUR and G\QEHJLQ are filled.
After the record that identifies the screen, use a separate EGFGDWD record for each field you
want to fill. These records use the table fields IQDP and IYDO. The following fields can be
filled:
-
You also use the &$// 75$16$&7,21 technique to transfer GDWD IURP H[WHUQDO V\VWHPV.
Further information on this topic is available in the course %& 'DWD 7UDQVIHU and in the
online documentation.
SAP AG
BC401
4-25
G\QEHJLQ
IQDP
IYDO
SURJUDP
`s`hes$`eh `rh
3h`r`e`rr`$ `$
ee`hr`h`
3`r 3`
86,1* LWBEGFGDWD
02'(
1
,) V\VXEUF !
0(66$*( :,7+ V\VXEUF
(1',)
r`
'$7$
EGFGDWD
ZDBEGFGDWD 7<3( EGFGDWD
LWBEGFGDWD /,.( 7$%/( 2) ZDBEGFGDWD
6DYH
SAP AG 2002
This example refers to the transaction %&B&$/'B&7$B8. If you request the creation of a new
customer entry here, the transaction %&B&$/'B&5(B&867 is inserted. This transaction has
not implemented an import from the ABAP memory, and its input fields are not set as *(7
parameters. The customer data is therefore passed using an internal table and the transaction
processed in the background.
If the operation is successful, the new customer data record can be entered in the waiting list.
The relevant internal table in EGFGDWD format is shown above. current_name is the
customer name adopted from the input field at runtime, current_city is the city.
You address the command field using %'&B2.&2'(. Here you enter the function code that is
triggered by the user choosing a function key, pushbutton, or menu entry during the dialog flow (or
by entering a code directly in the command field).
SAP AG
BC401
4-26
SAP AG 2002
SAP AG
BC401
4-27
l 2EMHFWRULHQWHG SURJUDPPLQJ
SAP AG 2002
SAP AG
BC401
5-1
SAP AG 2002
SAP AG
BC401
5-2
SAP AG
BC401
5-3
SAP AG 2002
y w
% 6 6 qxv
G G 4 A I8 4
P
4 Q64 B9 8 7
D C A 9
EB@8 7 5 564
G F
H64 C 9
3
(
11
0
11 0
2
()
f
S a d #c e
W
S
d c b `
6#V %a @V XY@V U#TR
'
""
&
&
"
%
$$
"
#!
d
U c` a g
r
u stf c ` c g
V U S ` 6a
c` c g
h
pqV c S a i%6e
Machine language
Assembler
d
B
$%$3 2EMHFWV
SAP AG 2002
$%$3 was created with the intention of improving reporting. ABAP developed almost
independently as an in-house programming language, although it was influenced by other
programming languages, for example, COBOL and PASCAL.
$%$3 2EMHFWV is a true extension of ABAP. ABAP Objects unites the most promising aspects of
other object-oriented programming languages, such as Java, C++, and Smalltalk.
SAP AG
BC401
5-4
3URFHGXUDO SURJUDPPLQJ
3URFHGXUDO SURJUDPPLQJ
2EMHFWRULHQWHG SURJUDPPLQJ
2EMHFWRULHQWHG SURJUDPPLQJ
SAP AG 2002
SAP AG
BC401
5-5
f ig f H
he
f ig f H
he
f ig f
hHe
l
l
he
f g f
f hg
ifHe
l n qm ql qk
Tp p o or%qqj j
l n m l k
l o Enqm @l @k @
p p rnqqqj j
l o m l k
l T n %ql lqk
Tp p o o mqqj j
l n m k
l o oEqqqk
p p rnqmqlqj j
l n m l k
l o n nqql qk
Tp p o r%mqqj j
l
m l k
Tp p o o @qj j
l T n %m ql lqk
l n m k
lp p Eqlqj j
T o o n m mqlqk
l n
k
Tp p o E%qlqj j
l T o n m qqk
l n m l k
SAP AG 2002
Information systems were previously defined primarily by their functions: Data and functions were
stored separately and linked using input-output relationships.
SAP AG
BC401
5-6
7\SH GHILQLWLRQV
'$7$
'$7$
'DWD GHFODUDWLRQV
0DLQ SURJUDP
3(5)250 IRUP
3(5)250 IRUP
&$// )81&7,21
)%
&$// )81&7,21
)%
&$// )81&7,21
)%
&$// )81&7,21
)%
)250 I
)250 I
(1')250
(1')250
&DOOLQJ VXEURXWLQHV
&DOOLQJ IXQFWLRQ PRGXOHV
'HILQLWLRQ RI VXEURXWLQHV
SAP AG 2002
A typical ABAP program consists of type definitions and data declarations, which describe the
blueprint of the data the program uses when it is executed.
To make your program more readable and for better program structure, it is recommended that you
work with modularization units (encapsulated units with functions), such as form routines or
function modules. These components can be reused in many different programs.
SAP AG
BC401
5-7
vytE|
)XQFWLRQ JURXS
ti{x!}i2{Bxytus
|
~ w v x | x z t
w v t
@yt{x
x
v
Exx!! &
g
g
2 x
T
g
)XQFWLRQ JURXS
!x!! Y
g txY
g
g
2 x
Interface
Interface
SAP AG 2002
This slide provides an overview of the main program components during program execution in an
internal session (main memory area on application server).
The loaded main program and the two function groups with their encapsulated modules (the function
modules) are both in separate memory areas.
From the main program, you can use function modules to access function group components, for
example their global data. Therefore, a function group is a unit consisting of data and functions that
manage this data.
A user "client" (here, the main program) can only access the function groups and their services using
the interface, that is the function modules. The function group acts as a "server", because it provides
services.
SAP AG
BC401
5-8
@yHu2y
qy
uy
22
6B9(+,&/(
VSHHG
)81&7,21 '(&B63(('
)81&7,21 '(&B63(('
68%75$&7 ,03B63((' IURP VSHHG
68%75$&7 ,03B63((' IURP VSHHG
(1')81&7,21
(1')81&7,21
q
x@
g
!
)81&7,21 *(7B63(('
)81&7,21 *(7B63(('
H[SBVSHHG
VSHHG
H[SBVSHHG
VSHHG
(1')81&7,21
(1')81&7,21
g
SAP AG 2002
The function group s_vehicle provides a user or client with the services inc_speed,
dec_speed, and get_speed.
These services make up the function group interface and access the internally encapsulated
component speed.
SAP AG
BC401
5-9
B
uH
Y@y
y
7<3(6
7<3(6
5(3257 ]YHKLFOHGHPR
5(3257 ]YHKLFOHGHPR
yy@2y
6B9(+,&/(
VSHHG
&$// )81&7,21
,1&B63(('
&$// )81&7,21
,1&B63(('
q
&$// )81&7,21
*(7B63(('
&$// )81&7,21
*(7B63(('
x@
g
!
g
&$// )81&7,21
6723
&$// )81&7,21
6723
SAP AG 2002
SAP AG
BC401
5-10
SAP AG
n
BC401
5-11
If the main program is to work with several vehicles, this is not possible without extra programming
and administration effort.
SAP AG 2002
g
g
T!!
Txq
q
VSHHG
y
@@
yy
}Y
y
y
qyyY
6B9(+,&/(
B@B@uY
@uq@y
qY2
@uqu&u@
q&y!
Yy
Y
!@
VSHHG
VSHHG
VSHHG
VSHHG
SAP AG 2002
The ability to create multiple instances of a "class", such as a vehicle, is one of the central attributes
of object-oriented languages.
SAP AG
BC401
5-12
3URFHGXUDO SURJUDPPLQJ
3URFHGXUDO SURJUDPPLQJ
2EMHFWRULHQWHG SURJUDPPLQJ
2EMHFWRULHQWHG SURJUDPPLQJ
SAP AG 2002
SAP AG
BC401
5-13
SAP AG
n
BC401
5-14
Objects form capsules containing the data itself and the behavior of that data. Objects enable you to
draft a software solution that is a one-to-one reflection of the real-life problem area.
Linked with input-output relations
Stored separately
Created separately
The left part of the slide shows that, with procedural software systems, data and functions are often:
SAP AG 2002
l o n m l k
6Tp r#@@j
%p rqqqj
l o n m l k
Ex
n
%Tp o qqj
l n m l k
l T n %m qlqk
Tp p o o #l@j j
l n m k
Tp p o rm@qj j
l l o n qql qk
n m l k
l o En m ql @k @
p p r#@j j
l o n m l k
l n m l k
%Tp o %qqj
6p En%#@@j
l o m l k
Tp p o o @qj j
l T n %m ql qk
l n m l k
l T o o n m@l qk
p p Emqqj j
l n l k
p p Enq@@j j
l o E%m #l @k @
l o n m l k
l T o orm m#@k @
p p En%#l@@j j
l n l k
l o oEqm@l @k @
p p rnqmqqj j
l n l k
n
E
6lp Eq@@j
o n m l k
%Tp o n%qqj
l
m l k
E
n
l n m l k
%Tp o %qqj
n
Ex
Ex
n
%Tp o qqj
l n m l k
Enq
z t v t x
t2&{
qx
n
nx
2EMHFW PRGHO DV
DEVWUDFWLRQ RI UHDO
ZRUOG
3URJUDPPLQJ 0RGHOV
l
l
z t v t x
t@yuY{
l
l
l
l
SAP AG 2002
In object-oriented programming, data and functions are developed together. Object orientation
focuses on objects that represent either abstract or concrete things in the real world. They are first
viewed in terms of their characteristics, which are displayed using the objects internal structure and
attributes (data).
SAP AG
BC401
5-15
Hqp
Hqp
q@ qq
n o
&qp
@p
TTf &
2Tf &
l ,QKHULWDQFH
6l l o En qm@l @k @
%pp Eqqj j
o n m l k
6l n #l@k @
%Tp or%mqqj
Tf &
SAP AG 2002
(QFDSVXODWLRQ
Encapsulation means that the implementation of an object is hidden from other components in the
system, so that they cannot make assumptions about the internal status of the object and therefore
dependencies on specific implementations do not arise.
3RO\PRUSKLVP
Polymorphism (ability to have multiple forms) in the context of object technology signifies that
objects in different classes react differently to the same messages.
,QKHULWDQFH
Inheritance defines the implementation relationship between classes, in which one class (the
subclass) shares the structure and the behavior defined in one or more other classes (superclasses).
Note: ABAP Objects only allows single inheritance.
SAP AG
BC401
5-16
&OLHQW6HUYHU %HKDYLRU
l
l
0HWKRGV
FDOOBLW
'DWD
0HWKRGV
GRBLW
'DWD
uu
%%
SAP AG 2002
Objects behave like client/server systems: When an object calls a method of another object, it
automatically becomes the client of the other (server) object. This gives rise to two conditions:
The client object must adhere to the SURWRFRO of the server object
The protocol must be clearly described so that a potential client can follow it without problem
Objects normally adopt both roles Every object is a potential server object, and when it is called by a
method of another object, it becomes a client object too.
Establishing logical business and software/technical responsibilities between classes results in a true
client/server software system in which redundancy is avoided.
SAP AG
BC401
5-17
FRXQWHU
FRXQWHU
FRXQWHU
FRXQWHU
029( ZD 72
029( ZD 72
$V VLPSOH DV SRVVLEOH
SAP AG 2002
ABAP Objects is not a new language, but has been developed as an extension of ABAP. It integrates
seamlessly into ABAP syntax and the ABAP programming model. All enhancements are strictly
upward compatible.
In ABAP objects, types have to be assigned more strictly than in ABAP. - for example, when
defining interface parameters for methods, you must type the parameters. The correct pass by value
is then checked by the system when the method is called.
In ABAP Objects, the ABAP language has been cleaned up. As part of this language clean up, the
system sometimes executes stricter syntax checks for previously permitted constructions and
obsolete statements are not allowed. The stricter syntax checks usually result in a syntax that should
also be used outside ABAP Objects, but where the old forms cannot be prohibited for compatibility
reasons.
For further information, refer to the ABAP Objects documentation under 5HSODFHPHQW RI 2EVROHWH
6WDWHPHQWV
SAP AG
BC401
5-18
T @qTqi@
q @ @ @ r T
y@2y
x xxxxE&
T q @ @@ h
@#Tqr
DQG EXVLQHVV
DUH
HQFDSVXODWHG LQ REMHFWV
y@y
Exx! B
%Tp oE@qkj
6l n %m #l@@
En%
x
n
h
iq
%
SAP AG 2002
One feature and design aim of object-oriented languages is that business functions are held
separately.
The client (in this case, the main program) uses the addresses of the objects (pointers or reference
variables) to access their encapsulated functions. Amongst other things, this concept is to improve
the structuring, reusability, and maintainability of the software.
SAP AG
BC401
5-19
5HTXLUHPHQW
LGHD
7HVW
D|
C
Y
yBYt%AYBu
v A
z
v
{}t1@v
C | A
9 z x
@&{@tt
BBy
v
$QDO\VLV DQG
GHVLJQ
,PSOHPHQWDWLRQ
$%$3 2EMHFWV
SAP AG 2002
In object-oriented programming, the analysis and design phase is even more important than it is for
procedural programming. The reason for this is that in object-oriented programming, decisions taken
during the analysis and design phase have even more pronounced effects on implementation than
they do in procedural programming.
SAP AG
BC401
5-20
SAP AG 2002
SAP AG
BC401
5-21
l &ODVV GLDJUDPV
l 6HTXHQFH GLDJUDPV
SAP AG 2002
SAP AG
BC401
6-1
SAP AG 2002
SAP AG
BC401
6-2
SAP AG
BC401
6-3
SAP AG 2002
`
b X aQ
G YG EGR R CVPQ UXT G "S IPQ EHGF
X cE W T S R I G
t E ECEf
x
e vr 'u w
vu s q p i ge
E'h tr "h "h 'fd ( &
53 2
7 2 !8 0 %764 !$ !1$ 0')'%$# !"
D A
A 1 D ECB# " #$ 9@& 7 ! #1
v g qu r y
gh e q Er x qu u y
h u e r tEw qu u y
&ODVVLILFDWLRQ
&ODVVLILFDWLRQ
80/ WKH PRGHOLQJ VWDQGDUG
80/ WKH PRGHOLQJ VWDQGDUG
SAP AG 2002
SAP AG
BC401
6-4
&ODVVLILFDWLRQ RI 2EMHFWV
mlaid
gk j h g f e
OFOBSHRSOH
OFOBYHKLFOH
f}U~Huu{ywutqrpn
o z ~
}| q z x v s s o
SAP AG 2002
In the real world, there are objects, such as various airplanes , cars, and people. Some of these
objects are very similar, that is, they can be described using the same attributes or characteristics and
provide the same functions.
Similar objects are grouped together in classes. Each class is described once, and each object is then
created in accordance with this blueprint.
A class is therefore a description of a quantity of objects characterized by the same structure and the
same behavior. For example, the vehicle "make x, ... serial number xxx" is an object of the class
"vehicle". This object is therefore a concrete instance of the class.
SAP AG
BC401
6-5
y 4
C
:KHHOV
36
OFOBYHKLFOH
)"")H
)
4
Ei
'RRUV
%U'6a
"
7\SH
r a
EyUuu
a
r U4U
t
UU
OFOBYHKLFOH
0DNH
)
fr)
U 4
4
'i
SAP AG 2002
In this context, abstractions are a simplified representations of complex relationships in the real
world. An actually existing object is abstracted to the significant dimensions that are to be mapped.
Insignificant details are left out in order to aid understanding of the overall system.
This example concerns cars. Software for a car enthusiast and software for a scrap merchant contain
different abstractions (classes) for these objects.
While in one software system the class lcl_vehicle describes all transport vehicles (including
bikes), in another system it describes all motorized machines with wheels.
Both classes have the same name but describe different objects.
In our examples, the class lcl_vehicle describes all motorized vehicles with 4 wheels.
SAP AG
BC401
6-6
5HSUHVHQWDWLRQ RI D &ODVV
OFOBYHKLFOH
tu
PDNH
PRGHOO
Optional, does
not have to be
Assoziationsme
specified
fytr)U
SULFH
FRORU
VHWBPDNH
GLVSOD\BDWWULEXWHV
LQFUHDVHBVSHHG
4jaE
U
SAP AG 2002
UML notation:
A class is represented with its name, attributes, and methods. With UML, you also have the option of
omitting either the attribute or the method part.
Attributes describe the data that can be stored in the objects in a class.
They define the state of an object.
Methods describe the functions that can be executed on the data of the object.
They contain the executable source code and therefore also represent an objects "behavior".
SAP AG
BC401
6-7
a@E@a@d
e k j l e e ek e
OFOBYHKLFOH
k
j
2EMHFW LQVWDQFH
l
k j
U H C
&ODVV
SAP AG 2002
A FODVV is a set of objects that have the same structure and the same behavior. A class is therefore
like a blueprint, in accordance with which all objects in that class are created.
Every REMHFW has an identity, a status (set of attribute values), and behavior (set of methods and
events). The structure and behavior of similar objects are defined in their shared class.
Identity is an attribute that distinguishes each object from all other objects. Identity is often confused
with having the same attribute values or with a unique name. 7ZR GLIIHUHQW REMHFWV FDQ KDYH
LGHQWLFDO DWWULEXWH YDOXHV DQG VWLOO QRW EH LGHQWLFDO
Example:
Two coffee cups are the same height and diameter, have the same handle and are both white.
Although they look exactly the same, they are still two separate cups.
In German literature on the subject of object orientation, we often speak of instances. In object
orientation an instance is simply an object. Another synonym of REMHFW is LQVWDQFH.
SAP AG
BC401
6-8
&ODVVLILFDWLRQ
&ODVVLILFDWLRQ
80/ WKH PRGHOLQJ VWDQGDUG
80/ WKH PRGHOLQJ VWDQGDUG
SAP AG 2002
SAP AG
BC401
6-9
l ,W LV D JOREDO VWDQGDUG
w
w
w
w
w
&ODVV GLDJUDP
'LVWULEXWLRQ GLDJUDP
SAP AG 2002
UML (Unified Modeling Language) is a standardized modeling language. It is used for the
specification, construction, visualization, and documentation of models for software systems and
enables uniform communication between various users. UML does not describe the steps in the
object-oriented development process.
UML is an industry standard and has been standardized by the OMG (Object Management Group)
since September 1997 as UML Version 1.1. The members of the OMG are continuously developing
it further. SAP uses UML as the company-wide standard for object-oriented modeling.
You can find the UML specifications on the OMG homepage at:
http://www.omg.org
UML describes a number of different diagram types in order to represent different views of a system.
&ODVV GLDJUDPV show the static view of a model.
%HKDYLRU GLDJUDPV demonstrate the relationships and method calls between objects. They
emphasize the timing sequence of method calls.
SAP AG
BC401
6-10
OFOBUHQWDO
0..*
OFOBERRNLQJ
0..* 1
OFOBFXVWRPHU
1..*
OFOBYHKLFOH
OFOBFDU
OFOBWUXFN
1..*
OFOBZKHHO
OFOBEXV
SAP AG 2002
A class diagram describes the elements contained in the model and their various static relationships.
There are two basic forms of static relationships:
In class diagrams, classes can also be shown with their attributes and methods.
SAP AG
BC401
6-11
$VVRFLDWLRQ
OFOBERRNLQJ 0..*
A booking refers to
a customer
ERRNV
* or 0..*
1
1..*
0..1
OFOBFXVWRPHU
Association name
Many
Exactly one
One or more
Zero or one
SAP AG 2002
An association describes a semantic relationship between classes. The specific relationship between
objects in these classes is known as an object link. Object links are therefore the instances of an
association.
An association is usually a relationship between different classes. However, an association can also
be recursive; in this case, the class would have a relationship with itself. In most cases, recursive
associations are used to link two different objects in one class.
The points below assume that the associations are binary.
Each association has two roles, one for each direction of the association (booking customer,
customer booking). Roles can have names (for example, the association car reservation could
be called "reservation").
Each role has a cardinality that shows how many instances participate in this relationship. The
multiplicity is the number of participating objects in one class that have a relationship to an object
in the other class.
UML notation:
The cardinality of the relationship can be shown at each end of the line
Associations can be given a name for ease of identification (a verb or a short text). This name is
written in italics above the line and may have an arrow to show the read direction. Both are
optional.
SAP AG
BC401
6-12
$JJUHJDWLRQ LV D VSHFLDO
FDVH RI DVVRFLDWLRQ D
ZKROHSDUW UHODWLRQVKLS
Aggregation symbol
OFOBYHKLFOH
&RPSRVLWLRQ LV D VSHFLDO
FDVH RI DJJUHJDWLRQ DQ
H[LVWHQFHGHSHQGHQW
ZKROHSDUW UHODWLRQVKLS
0..*
OFOBZKHHO
Composition symbol
OFOBUHQWDO
1..*
OFOBERRNLQJ
SAP AG 2002
Aggregation is a special kind of association. Aggregation describes one object that contains another
or consists of other objects (whole-part). A vehicle consists of wheels. The relationship can be
described by the words "consists of" or "is a part of".
Composition is a special kind of aggregation. Composition describes the fact that the object
contained cannot exist without the aggregate (for example, a car reservation cannot exist without the
car rental).
Differences to aggregation:
The cardinality on the aggregate side can only be one. Each part is only part of one composite object,
otherwise the existence dependency would be contradictory. The lifetime of the individual parts is
linked to the lifetime of the aggregate: Parts are created either with or immediately after the
aggregate, and they are destroyed either with or immediately before the aggregate.
SAP AG
BC401
6-13
OFOBFDU
OFOBWUXFN
Generalization
Inheritance arrow
Specialization
OFOBYHKLFOH
OFOBFDU
Specialization
OFOBYHKLFOH
OFOBWUXFN
Generalization
RU
SAP AG 2002
UML notation:
Generalization and specialization are denoted by triangular arrows that point from the subordinate
class to the superclass.
Several arrows can be combined into a tree.
SAP AG
BC401
6-14
OFOBGULYHU
Time
3URFHVV
GHVFULSWLRQ
RSWLRQDO
OFOBFDU
0HWKRG SDUDPHWHU
Life line
of object
5HWXUQ YDOXH
Control focus
SAP AG 2002
Sequence diagrams, unlike class diagrams, show the dynamics between objects. They are used to
represent a particular process or a particular situation. Sequence diagrams focus on the time sequence
of the information exchange:
a) Creation and deletion of objects
b) Message exchange between objects
The control focus is shown as a vertical rectangle on the object life line. The control focus shows the
objects "active" period:
Messages are shown as horizontal arrows between the object lines. The message is written above the
arrow in the form PHWKRG SDUDPHWHU. There are various ways of representing the reply; in this
example, the arrow is shown as a returning arrow.
You can also include a description of the process and add comments to the object life line as
required.
SAP AG
BC401
6-15
OFOBGULYHU
OFOBFDU
JHWBIXHOBOHYHO
OFOBWDQN
JHWBIXHOBOHYHO
UHBOHYHO
UHBOHYHO
UHBOHYHO
WDQN!JHWBIXHOBOHYHO
UHBOHYHO
IXHO IXHOBPD[
SAP AG 2002
In delegation, two objects are involved in handling a request: The recipient of the request delegates
the execution of the request to a delegate.
Example:
The driver (lcl_driver) calls the method get_fuel_level for the class car (lcl_car). The
car cannot carry out this task itself. Therefore, the car calls the get_fuel_level method for the
class (lcl_tank), that is the car GHOHJDWHV the execution of the method to the tank.
The main advantage of delegation (as a re-use mechanism) lies in the option of changing the
behavior of the recipient by substituting the delegate (at runtime). For example, delegation enables
the car to be equipped with a new tank, without the call changing for the client or for the car class.
Good encapsulation often forces the use of delegation: If tank in the above example were a private
attribute of the class lcl_car, the user could not address the tank directly, but only through the car.
SAP AG
BC401
6-16
SAP AG 2002
SAP AG
BC401
6-17
([HUFLVHV
8QLW $QDO\VLV DQG 'HVLJQ
1-1
Use a pencil and paper to create a UML class diagram that contains the following
classes:
- Airline:
OFOBFDUULHU
Airplane (general):
OFOBDLUSODQH
Passenger airplane:
OFOBSDVVHQJHUBSODQH
Cargo plane:
OFOBFDUJRBSODQH
1-1-1 Include some appropriate attributes and methods for every class.
1-1-2 Draw lines to represent the relationships between the classes and indicate
possible cardinalities.
SAP AG
BC401
6-18
6ROXWLRQV
8QLW $QDO\VLV DQG 'HVLJQ
SAP AG
BC401
6-19
l $WWULEXWHV
l 0HWKRGV
l 9LVLELOLW\HQFDSVXODWLRQ
l ,QVWDQWLDWLRQ
l &RQVWUXFWRU
l *DUEDJH FROOHFWRU
SAP AG 2002
SAP AG
BC401
7-1
SAP AG 2002
SAP AG
BC401
7-2
SAP AG
BC401
7-3
SAP AG 2002
y
& & $x
g
V eb d f
e c a ` Y WV
dX &b X RX $U
!
% # "
0) '
1(! ! &$
H PQ@
S
6 T6 (A EF9@ DH B(6 ABR89@ 76 5
3 ' 2 %
4
86
4 ! (
H G
I6 C A C
ljhef tv
t
i
g
m k
eW da b h
w s
WX V a b uvg td a d h
i
qrX d V b p&f d a d h
3ULQFLSOHV
SAP AG 2002
SAP AG
BC401
7-4
([DPSOH RI D &ODVV
3XEOLF
components
3ULYDWH
components
h( h
vRsh
OFOBYHKLFOH
{ z x w
tv{I
{ z x
Iyw
{ z x w {
tv
t{Itlw
~ } |
0HWKRGV
Implementation
3ULYDWH DFFHVV
Encapsulation
Normally attributes
3XEOLF DFFHVV
Interface
Normally methods, events
SAP AG 2002
In the graphic, the public component of the class is accessed using the green node or starting point
displayed on the left. A user or client can use this node to access the public components and hence
also indirectly access the private components. However, the private components of the class cannot
be addressed directly . They are not visible to the outside user.
SAP AG
BC401
7-5
'HILQLQJ &ODVVHV
yyf
OFOBYHKLFOH
components
components
1$$
} |
t{
~ R
(1'&/$66
{
x
SAP AG 2002
A FODVV is a set of objects that have the same structure and the same behavior. A class is therefore
like a EOXHSULQW, in accordance with which all objects in that class are created.
The components of the class are defined in the definition part. The components are attributes,
methods, events, constants, types, and implemented interfaces. Only methods are implemented in the
implementation part.
The CLASS statement cannot be nested, that is, you cannot define a class within a class.
SAP AG
BC401
7-6
$WWULEXWHV
l $WWULEXWHV FDQ KDYH DQ\ NLQG RI
GDWD W\SH
OFOBFDU
Public
l 8VHUGHILQHG W\SHV
l 7<3( 5() 72
GHILQHV D UHIHUHQFH WR DQ
REMHFW LQ WKLV FDVH UBFDU
Private
OFOBUHQWDO
(
SAP AG 2002
Attributes describe the data that can be stored in the objects of a class.
Object references
Interface references
make
(car make)
modell
(type , model)
ser_no
(serial number)
color
(color)
max_seats
(number of seats)
r_motor
...
SAP AG
BC401
7-7
7<3(
7<3(
/,.(
7<3(
7<3(
7<3(
7<3(
W\SH!
GGLFBW\SH!
YDULDEOH
W\SH! 9$/8( YDOXH!
W\SH! 5($'21/<
5() 72 FODVVQDPH!
5() 72 LQWHUIDFH!
SAP AG 2002
In classes, you can only use the TYPE addition to refer to data types.
You can only use the LIKE reference for local data objects.
The READ-ONLY addition means that a public attribute declared with DATA can be read from
outside, but can only be changed by methods of the class.
You can currently only use the READ-ONLY addition in the public visibility section (PUBLIC
SECTION) of a class declaration or in an interface definition.
SAP AG
BC401
7-8
l 3XEOLF DWWULEXWHV
'LUHFW DFFHVV
l 3ULYDWH DWWULEXWHV
1R GLUHFW DFFHVV
IURP RXWVLGH WKH FODVV
38%/,& 6(&7,21
'$7$ PDNH 7<3( VWULQJ
35,9$7( 6(&7,21
(1'&/$66
35,9$7( 6(&7,21
'$7$ PDNH
7<3( VWULQJ
(1'&/$66
SAP AG 2002
You can protect attributes against access from outside by characterizing them as private attributes
(defined in the PRIVATE SECTION).
Attributes and their values that may be used directly by an external user are public attributes and are
defined in the PUBLIC SECTION.
In the above example for the class lcl_car, the attribute make is defined as a public attribute.
Public attributes belong to the LQWHUIDFH of the class, that is their implementation is publicized. If you
want to hide the internal implementation from users, you must define internal and external views of
attributes.
SAP AG
BC401
7-9
OFOBYHKLFOH
yj &
3XEOLF
3ULYDWH
PDNH
VHWBPDNH
JHWBPDNH
...
hR
SAP AG 2002
You can access an objects private attributes using public methods, which in turn output this attribute
or change it.
It is not possible to directly access private attributes from outside (for example, main program or
other object). The only exception lies in the so-called "friend" concept, which will be dealt with later
in the course.
SAP AG
BC401
7-10
l ,QVWDQFH DWWULEXWHV
2QH SHU LQVWDQFH
6WDWHPHQW '$7$
l 6WDWLF DWWULEXWHV
35,9$7( 6(&7,21
'$7$ PDNH
...
6WDWHPHQW &/$66'$7$
$OVR NQRZQ DV FODVV
DWWULEXWHV
7<3( VWULQJ
SAP AG 2002
Static attributes
Instance attributes
Instance attributes are attributes that exist separately for each object.
Instance attributes are defined using the ABAP keyword DATA.
Static attributes exist only once for each class and are visible for all (runtime) instances in that class.
Static attributes usually contain information that applies to all instances, such as:
Administrative information about the instances in that class (for example, counters, ...)
You may come across the expression "class attributes" in documentation, however, the official term
in ABAP Objects (as in C++, Java) is "static" attributes.
SAP AG
BC401
7-11
QBRBYHKLFOHV
6WDWLF DWWULEXWHV
RQO\ H[LVW RQFH
v h h v
$ $7 " s $
$ D 7 $ s
v v 1 r
1 v 1 r
UBYHKLFOH
UBYHKLFOH
1 vv 1 t
UBYHKLFOH
,QVWDQFHV
REMHFWV
SAP AG 2002
Therefore, you can say that instances share their common attributes.
SAP AG
BC401
7-12
0HWKRGV 6\QWD[
SAP AG 2002
Methods are internal procedures in classes that determine the behavior of an object. They can access
all attributes in their class and can therefore change the state of an object.
Methods have a parameter interface (called VLJQDWXUH) that enables them to receive values when they
are called and pass values back to the calling program.
In ABAP Objects, methods can have IMPORTING, EXPORTING, CHANGING, and RETURNING
parameters as well as exception parameters. All parameters can be passed by value or reference. (As
of SAP R/3 Basis Release 6.10, you should no longer use the EXCEPTIONS parameter for
exceptions but use the RAISING addition instead; this will be discussed in more detail later.)
You can define a return code for methods using RETURNING. You can only do this for a single
parameter, which additionally must be passed as a value. Also, you cannot then define EXPORTING
and CHANGING parameters. You can define functional methods using the RETURNING parameter
(this will be explained in more detail).
All input parameters (IMPORTING, CHANGING parameters) can be defined as optional parameters
in the declaration using the OPTIONAL or DEFAULT additions. These parameters then do not
necessarily have to be passed when the object is called. If you use the OPTIONAL addition, the
parameter remains initialized according to type, whereas the DEFAULT addition allows you to enter
a start value.
SAP AG
BC401
7-13
OFOBYHKLFOH
yj &
3XEOLF
3ULYDWH
VHWBPDNH
LQLWBPDNH
PDNH
u((
SAP AG 2002
Methods also have to be assigned to a visibility area. This determines whether the methods can be
called from outside or only from within the class.
It is not possible to directly access private methods from outside. However, a private method can be
called by a public method.
Both method types can access the public and private attributes.
SAP AG
BC401
7-14
l 3XEOLF PHWKRGV
l 3ULYDWH PHWKRGV
SAP AG 2002
QR PDNH
(1'0(7+2'
0(7+2' VHWBPDNH
,) LPBPDNH ,6 ,1,7,$/
&DOOLQJ LQLWBPDNH
(/6(
PDNH
LPBPDNH
(1',)
(1'0(7+2'
(1'&/$66
In this example, init_make is a private method that is called by the public method set_make.
SAP AG
BC401
7-15
5XOHV
l ,QVWDQFH PHWKRGV
l 6WDWLF PHWKRGV
SAP AG 2002
Static methods are defined at class level. They are similar to instance methods, but with the
restriction that they can only use static components (such as static attributes) in the implementation
part. This means that static methods do not need instances and can be called from anywhere. They
are defined using the CLASS-METHODS statement, and they are bound by the same syntax and
parameter rules as instance methods.
The term "class method" is common, but the official term in ABAP Objects (as in C++, Java) is
"static method".
SAP AG
BC401
7-16
In the static method get_count, you can only use the static attribute n_o_vehicles. All other
attributes of the class are instance attributes and can only appear in instance methods.
SAP AG
BC401
7-17
OFOBYHKLFOH
h h( RvQh Q
hv Rhh QR"(u uR F
VHWBPDNH
LQLWBPDNH
JHWBFRXQW
Ru h Q
PDNH
PRGHO
QBRBYHKLFOHV
A UML class diagram shows firstly the class name and, underneath that, the class attributes and
methods.
The visibility of components in a class is shown in UML using the characters "+" and "-":
+ indicates public components
indicates private components
Alternatively, public and private can be prefixed to the methods. The third option for providers of
modeling tools in UML is to introduce their own symbols for visibility.
Representation of visibility characteristics is optional and is normally only used for models that are
close to implementation.
The input and output parameters and the parameters to be changed are shown in brackets.
SAP AG
BC401
7-18
3ULQFLSOHV
SAP AG 2002
SAP AG
BC401
7-19
&UHDWLQJ 2EMHFWV
PDNH
PRGHO
VHUBQR
QBRBYHKLFOHV
VHWBPDNH
LQLWBPDNH
JHWBFRXQW
OFOBYHKLFOH
Public
OFOBYHKLFOH
Private
5XOHV
SAP AG 2002
A class contains the generic description of an object. It describes all the characteristics that are
common to all objects in that class. During the program runtime, the class is used to create specific
objects (instances). This process is called instantiation.
Example:
The specific object Car xy with Ser-No. 0815" is created through instantiation from the class
lcl_vehicle; it is created in the main memory at runtime.
The lcl_vehicle class itself does not exist as an independent runtime object in ABAP Objects.
Implementation:
Objects are instantiated using the statement: CREATE OBJECT.
During instantiation, the runtime environment dynamically requests main memory space and assigns
it to the object.
SAP AG
BC401
7-20
5HIHUHQFH 9DULDEOHV
&/$66 OFOBYHKLFOH '(),1,7,21
38%/,& 6(&7,21
35,9$7( 6(&7,21
(1'&/$66
:KDW GR WKHVH
UHIHUHQFH YDULDEOHV
SRLQW WR"
D
@BCBA#@9
E
#BA
"
"
7 6 ' 5 3 1 2 1 ) ( % "
8!#4!0'!&$#!
...
SAP AG 2002
DATA: r_vehicle1 TYPE REF TO lcl_vehicle declares a reference variable that acts as
a pointer to an object.
SAP AG
BC401
7-21
W T $4U T RPH
S Y X W V S S QI
Public
Private
D
GBF
W T $$U 4T QRPH
S Y X W V S S `
E
BCBA#@
Public
Private
SAP AG 2002
The CREATE OBJECT statement creates an object in the main memory. The attribute values of this
object are either initial values or correspond to the VALUE entry.
SAP AG
BC401
7-22
*DUEDJH &ROOHFWRU
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH
UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH
&5($7( 2%-(&7 UBYHKLFOH
&5($7( 2%-(&7 UBYHKLFOH
UBYHKLFOH
W T $4U T RPH
S Y X W V S S QI
UBYHKLFOH
Public
Private
D
@BBA#C@
W T $$U 4T RPH
S Y X W V S S Q`
E
#BCBA#@9
Public
Private
SAP AG 2002
As soon as no more references point to an object, the Garbage Collector removes it from the
memory.
The Garbage Collector is a system routine that automatically deletes objects that can no longer be
addressed from the main memory and releases the memory space they occupied.
SAP AG
BC401
7-23
l
l
$OO LQGHSHQGHQW UHIHUHQFHV LQ WKH JOREDO PDLQ PHPRU\ DUH FKHFNHG 7KH
UHIHUHQFHV SRLQW WR DFWLYH REMHFWV ZKLFK DUH PDUNHG LQWHUQDOO\
5HIHUHQFHV RI FODVV RU LQVWDQFH DWWULEXWHV WR RWKHU REMHFWV DUH IROORZHG
7KHVH REMHFWV DUH DOVR PDUNHG
2EMHFWV WKDW DUH QRW PDUNHG DUH GHOHWHG IURP WKH PDLQ PHPRU\
h T e c S `
!pWgfd#$U S4T QRPH
u
CtG# s
higefCU T GbPH
T W d c S S Q a
h T We d c S Q q
!pgf&$U S4T GrBH
SAP AG 2002
Independent references are references that have not been defined within a class.
SAP AG
BC401
7-24
%XIIHULQJ 5HIHUHQFHV
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH
LWDE 7<3( 7$%/( 2) 5() 72 OFOBYHKLFOH
&5($7( 2%-(&7 UBYHKLFOH
$33(1' UBYHKLFOH 72 LWDE
h We d c S S Q a
iTgfCU T GbPH
wpgfdCU 4T GvPH
h T We c S S Q I
/223 $7 LWDE ,172 UBYHKLFOH
ZRUN ZLWK WKH FXUUHQW LQVWDQFH
(1'/223
...
SAP AG 2002
If you want to keep several objects from the same class in your program, you can define an internal
table, which, for example, only consist of one column containing the object references for this class.
You can process the objects using a LOOP through the internal table.
SAP AG
BC401
7-25
$JJUHJDWLRQ ([DPSOH
OFOBYHKLFOH
S W X S S Qq a
4WppU T GRPH
Private
S W W X S S Q
$ppU T GiH
y
R7
!p
Public
SCwpU T rPH
W W X S S Q a
S W W X S S Q I
Cwp4U T 4PH
SAP AG 2002
If a class defines object references to a second class as attributes (in the above example: References
to objects of the class lcl_wheel), only these object references will be stored in an object
belonging to that class.
The objects in the second class lcl_wheel have their own identity. They are encapsulated in the
first class lcl_vehicle and can only be addressed from this class using reference variables.
SAP AG
BC401
7-26
3ULQFLSOHV
SAP AG 2002
SAP AG
BC401
7-27
&DOOLQJ 0HWKRGV
d
e
OFOBYHKLFOH
Public
Private
PRWRUBRQ
UBYHKLFOH!PRWRUBRQ
0HWKRG
5HIHUHQFH
&RPSRQHQW VHOHFWRU
SAP AG 2002
Every object behaves in a certain way. This behavior is determined by its methods. There are three
types of method:
1. Methods that trigger the behavior and do not return values (see example)
2. Methods that return a value
3. Methods that return or change several values
An object that requires the services of another object sends a message to the object providing the
services. This message names the operation to be executed. The implementation of this operation is
known as a method.
For the sake of simplicity, method will henceforth be used as a synonym for operation and message.
The example shows the new syntax for method calls, in which the CALL-METHOD prefix is omitted.
SAP AG
BC401
7-28
i h g
p u t k
Ch Aonml g jt ABf
y wv t
ot it
u
o
p CxuAo h i g BPGPso g g rtwA q
LQVWDQFH!!LQVWDQFHBPHWKRG!
>DGGLWLRQV@
PDNHBQDPH
Since SAP R/3 Basis Release 6.10, the shortened form is also supported; CALL METHOD is omitted.
For further information, refer to the online documentation.
Special case: When calling an instance method from within another instance method, you can omit
the instance name. The method is automatically executed for the current object.
For the method call set_make, you can omit the EXPORTING addition in the brackets. In this case,
it is sufficient to set the actual parameter of the caller (in the example: make_name). However, if
the method has two or more parameters in its interface, all actual and formal parameters within the
brackets must have the EXPORTING addition.
In the case of methods that return parameters to the caller, the IMPORTING addition must always be
used and all actual and formal parameters must be listed.
SAP AG
BC401
7-29
FRXQW
SAP AG 2002
Static methods (also referred to as class methods) are called using CALL METHOD
<classname>=><class_method>.
Static methods are addressed with their class name, since they do not need instances.
Note:
If you are calling a static method from within the class, you can omit the class name.
SAP AG
BC401
7-30
)XQFWLRQDO 0HWKRGV
k
m#i
E
)XQFWLRQDO PHWKRG
} } {
~s9s9~|z
es9F~|z
} {
w
w
w
SAP AG 2002
Methods that have a RETURNING parameter are described as functional methods. These methods
cannot have EXPORTING or CHANGING parameters, but has many (or as few) IMPORTING
parameters and exceptions as required.
SAP AG
BC401
7-31
SAP AG 2002
Depending on the number of IMPORTING parameters, the syntax for functional methods is as
follows (same for static functional methods):
ref->func_method(
ref->func_method( p1 ) or
ref->func_method( im_1 = p1 )
No IMPORTING parameters:
SAP AG
BC401
7-32
just a demo
just a demo
(1'&/$66
OFOBYHKLFOH
}
s{}
80
UBYHKLFOH!PDNH
PDNHBQDPH
OFOBYHKLFOH !QBRBYHKLFOHV
FRXQW
!0
67$572)6(/(&7,21
* PDLQ SURJUDP WKLQN RI D FOLHQW
&5($7( 2%-(&7 UBYHKLFOH
PDNH
SAP AG 2002
There are different ways of accessing public attributes from outside the class:
n
SAP AG
BC401
7-33
3ULQFLSOHV
SAP AG 2002
SAP AG
BC401
7-34
&RQVWUXFWRU
l
l
l
OFOBYHKLFOH
PDNH
PRGHO
QBRBYHKLFOHV
FRQVWUXFWRU
&5($7( 2%-(&7
Private
The constructor is a special instance method in a class with the name constructor. The following
rules apply:
The constructor is automatically called at runtime within the CREATE OBJECT statement.
When exceptions are raised in the constructor, instances are QRW created, so no main memory space is
occupied.
SAP AG
BC401
7-35
&RQVWUXFWRU ([DPSOH
&/$66 OFOBYHKLFOH '(),1,7,21
38%/,& 6(&7,21
7<3( VWULQJ
0(7+2'6 FRQVWUXFWRU ,03257,1* LPBPDNH
LPBPRGHO 7<3( VWULQJ
35,9$7( 6(&7,21
'$7$ PDNH 7<3( VWULQJ ZHLJKW 7<3( S
&/$66'$7$ QBRBYHKLFOHV 7<3( L
(1'&/$66
BAPArnABPGmP
)HUUDUL
LPBPRGHO
)
SAP AG 2002
You need to initialize attributes that cannot be covered by the VALUE addition to the DATA
statement
SAP AG
BC401
7-36
l
l
l
FRXQWHU
OFOBYHKLFOH !QBRBYHKLFOHV LI DWWULEXWH LV SXEOLF
GHPR
FRXQWHU
OFOBYHKLFOH !JHWBFRXQW LPSRUWLQJ UHBFRXQW
SAP AG 2002
The static constructor is a special static method in a class with the name class_constructor. It
is executed precisely once per program. The static constructor of a class <classname> is called
automatically when the class is first accessed, but before any of the following actions are executed:
Creating an instance in the class using CREATE OBJECT <obj>, where <obj> has the data type
REF TO <classname>
SAP AG
BC401
7-37
3ULQFLSOHV
SAP AG 2002
SAP AG
BC401
7-38
OFOBYHKLFOH
$WWULEXWHV
r$&
components
0HWKRGV
0HWKRG
PDNH
(YHQWV
7\SHV
&RQVWDQWV
i &
components
$WWULEXWH
PDNH
p gvnv! i
$i
$/,$6 QDPHV
SAP AG 2002
Within a class, attribute names, method names, event names, constant names, type names and alias
names all share the same namespace.
There is a local namespace within methods. Definitions of local variables can cover components in
one class.
SAP AG
BC401
7-39
components
components
$WWULEXWH
PDNH
9vnv!0 i
$i
rC$&
OFOBYHKLFOH
9i &
0(!PDNH
0(7+2' '800<
* calling own methods from inside class
* call method display_attributes( )
* with the short syntax variant:
GLVSOD\BDWWULEXWHV
(1'0(7+2'
(1'&/$66
SAP AG 2002
You can address the object itself within instance methods using the implicitly available reference
variable me.
Description of example:
In the constructor, the instance attribute make is covered by the locally defined variable make. In
order to still be able to address the instance attribute, you need to use me.
The dummy method demonstrates how to call a classs own method. You can omit the prefix me->.
SAP AG
BC401
7-40
l &UHDWH FODVVHV
l &UHDWH REMHFWV
l &DOO PHWKRGV
SAP AG 2002
SAP AG
BC401
7-41
([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ
7RSLF
&UHDWLQJ D &ODVV
0RGHO VROXWLRQ
6$3%&B$,56B0$,1B$
6$3%&B$,56B$ include program
1-1
1-2
1-3
Create the package =%&B (where ##: is your group number) and save all the
repository objects you have created during the course in this package.
Create the main program =%&B B0$,1B$,53/$1( and the include
program =%&B B$,53/$1( the main program contains (##: group number).
Create the class OFOBDLUSODQH in the include program.
SAP AG
BC401
7-42
1-3-3 The class has a SXEOLF LQVWDQFH PHWKRG VHWBDWWULEXWHV to set the private
instance attributes name and plane type. Enter two corresponding importing
parameters for the declaration of the method in the definition part. The
definition of these parameters should be analogous to the two attributes.
Implement the method in the implementation part; set both attributes. Each
time the method is called, the static attribute QBRBDLUSODQHV should increase
by one. (This is actually not right but will be corrected later).
1-3-4 The class is to have another SXEOLF LQVWDQFH PHWKRG GLVSOD\BDWWULEXWHV to
display the instance attributes. Declare this method and, in the
implementation part, output the attributes using the WRITE statement (you
can also display the icon icon_ws_plane, in which case you must add
TYPE-POOLS icon to the program.
1-3-5 Declare and implement a SXEOLF VWDWLF PHWKRG GLVSOD\BQBRBDLUSODQHV to
display the static attribute QBRBDLUSODQHV.
(In the remaining exercises in this course, you can always start with the program you
created in the previous exercise or copy the corresponding model solution and continue
working with this program.)
SAP AG
BC401
7-43
([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
,QVWDQWLDWLQJ 2EMHFWV
0RGHO VROXWLRQ
6$3%&B$,56B0$,1B%
6$3%&B$,56B$ include program
SAP AG
BC401
7-44
([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
0HWKRG &DOOV
0RGHO VROXWLRQ
6$3%&B$,56B0$,1B&
6$3%&B$,56B& include program
3-1 Call the static method GLVSOD\BQBRBDLUSODQHV (before instantiating an object in class
OFOBDLUSODQH).
3-2 Use the VHWBDWWULEXWHV method to set the attributes for all objects already created.
Choose an airplane name and airplane type and pass them as text literals. (For the
plane type you can use a type from the table SAPLANE, for example 747-400.)
3-3 Display the object attributes using the GLVSOD\BDWWULEXWHV method.
3-4 Call the static method GLVSOD\BQBRBDLUSODQHV a second time.
3-5 Add a functional static method gHWBQBRBDLUSODQHV to the class OFOBDLUSODQH. The
method must be public and have the UHBFRXQW (type I) return parameter (no input
parameters).
3-6 Test your functional method by calling it from the main program.
SAP AG
BC401
7-45
([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
&RQVWUXFWRU
4-1 Create a FRQVWUXFWRU for the class OFOBDLUSODQH (in your include program
=%&B B$,53/$1() The simplest way of doing this is to copy the method
VHWBDWWULEXWHV, which you now no longer need.
4-1-1
4-1-2
The constructor must have two importing parameters that fill the instance
attributes QDPH and SODQHW\SH.
The static attribute QBRBDLUSODQHV should have an ascending sequence of
one in the constructor.
4-2 In the method VHWBDWWULEXWHV, comment out the line in which the static attribute
QBRBDLUSODQHV is increased by one (if you still want to use this method).
4-3 In the main program =%&B B0$,1B$,53/$1(, extend the creation of the
object with the constructor interface.
Fill the constructors interface parameters with the same values you used when
calling the VHWBDWWULEXWHV method.
4-4 2SWLRQDO
Comment out the method call VHWBDWWULEXWHV or delete the call.
SAP AG
BC401
7-46
([HUFLVHV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG 3URJUDPPLQJ
7RSLF &DOOLQJ D 3ULYDWH 0HWKRG
0RGHO VROXWLRQ
6$3%&B$,56B0$,1B(
6$3%&B$,56B( include program
Create the SULYDWH PHWKRG JHWBWHFKQLFDOBDWWULEXWHV for the class OFOBDLUSODQH (in
your include program =%&B
B$,53/$1().
5-3-3
SAP AG
BC401
7-47
6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
&UHDWLQJ D &ODVV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_AIRS_MAIN_a
*&---------------------------------------------------------------------*
5(3257
VDSEFBDLUVBPDLQBD
7<3(322/6 LFRQ
,1&/8'( 6$3%&BDLUVB$
*&-----------------------------------------------------------------*
*&
Include
SAPBC401_AIRS_A
*&-----------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
&/$66 OFOBDLUSODQH '(),1,7,21
38%/,& 6(&7,21
7<3( VWULQJ
GLVSOD\BDWWULEXWHV
&/$660(7+2'6 GLVSOD\BQBRBDLUSODQHV
35,9$7( 6(&7,21
'$7$ QDPH
7<3( VWULQJ
SAP AG
BC401
7-48
(1'&/$66
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
&/$66 OFOBDLUSODQH ,03/(0(17$7,21
0(7+2' VHWBDWWULEXWHV
QDPH
LPBQDPH
QBRBDLUSODQHV
QBRBDLUSODQHV
SODQHW\SH
(1'0(7+2'
LPBSODQHW\SH
0(7+2' GLVSOD\BDWWULEXWHV
1DPH RI DLUSODQH
$7 SRVB QDPH
$LUSODQH W\SH
$7 SRVB SODQHW\SH
(1'0(7+2'
0(7+2' GLVSOD\BQBRBDLUSODQHV
:5,7(
7RWDO QXPEHU RI SODQHV
FD
(1'0(7+2'
(1'&/$66
SAP AG
BC401
7-49
6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
,QVWDQWLDWLQJ 2EMHFWV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_AIRS_MAIN_b
*&---------------------------------------------------------------------*
*& create and insert planes into internal table
*&---------------------------------------------------------------------*
REPORT
sapbc401_airs_main_b.
TYPE-POOLS icon.
INCLUDE SAPBC401_airs_A.
'$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH
START-OF-SELECTION.
*##############################
SAP AG
BC401
7-50
6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
0HWKRG &DOOV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_AIRS_MAIN_C
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_airs_main_c.
TYPE-POOLS icon.
INCLUDE sapbc401_airs_c.
'$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH
START-OF-SELECTION.
*##############################
OFOBDLUSODQH !GLVSOD\BQBRBDLUSODQHV
&5($7( 2%-(&7 UBSODQH
UBSODQH!VHWBDWWULEXWHV LPBQDPH
/+ %HUOLQ
LPBSODQHW\SH
UBSODQH!VHWBDWWULEXWHV LPBQDPH
$$ 1HZ <RUN
LPBSODQHW\SH
UBSODQH!GLVSOD\BDWWULEXWHV
(1'/223
SAP AG
BC401
7-51
OFOBDLUSODQH !GLVSOD\BQBRBDLUSODQHV
FDOOLQJ WKH IXQFWLRQDO PHWKRG JHWBQBRBDLUSODQHV
ORQJ V\QWD[ IRU IXQFWLRQDO FDOO
FRXQW
OFOBDLUSODQH !JHWBQBRBDLUSODQHV
ZULWH
1XPEHU RI DLUSODQHV
FRXQW
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_AIRS_C
*&
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
&/$66 OFOBDLUSODQH '(),1,7,21
38%/,& 6(&7,21
7<3( VWULQJ
GLVSOD\BDWWULEXWHV
&/$660(7+2'6 GLVSOD\BQBRBDLUSODQHV
35,9$7( 6(&7,21
'$7$ QDPH
7<3( VWULQJ
SAP AG
BC401
7-52
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
&/$66 OFOBDLUSODQH ,03/(0(17$7,21
0(7+2' VHWBDWWULEXWHV
QDPH
LPBQDPH
QBRBDLUSODQHV
QBRBDLUSODQHV
SODQHW\SH
(1'0(7+2'
LPBSODQHW\SH
0(7+2' GLVSOD\BDWWULEXWHV
1DPH RI DLUSODQH
$7 SRVB QDPH
$LUSODQH W\SH
$7 SRVB SODQHW\SH
(1'0(7+2'
0(7+2' GLVSOD\BQBRBDLUSODQHV
:5,7(
7RWDO QXPEHU RI SODQHV
FD
(1'0(7+2'
PHWKRG JHWBQBRBDLUSODQHV
UHBFRXQW
HQGPHWKRG
QBRBDLUSODQHV
(1'&/$66
SAP AG
BC401
7-53
6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
&RQVWUXFWRU
*&---------------------------------------------------------------------*
*& Report
SAPBC401_AIRS_MAIN_d
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_airs_main_d.
TYPE-POOLS icon.
INCLUDE sapbc401_airs_d.
'$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH
START-OF-SELECTION.
*##############################
lcl_airplane=>display_n_o_airplanes( ).
&5($7( 2%-(&7 UBSODQH H[SRUWLQJ LPBQDPH
/+ %HUOLQ
LPBSODQHW\SH
$$ 1HZ <RUN
LPBSODQHW\SH
SAP AG
BC401
7-54
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_AIRS_d
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
"-------------------------------CONSTANTS: pos_1 TYPE i VALUE 30.
0(7+2'6 FRQVWUXFWRU ,03257,1*
LPBQDPH
7<3( VWULQJ
GLVSOD\BDWWULEXWHV
CLASS-METHODS: display_n_o_airplanes.
PRIVATE SECTION.
"---------------------------------DATA: name
TYPE string,
*------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.
0(7+2' FRQVWUXFWRU
QDPH
LPBQDPH
SODQHW\SH
LPBSODQHW\SH
QBRBDLUSODQHV
QBRBDLUSODQHV
(1'0(7+2'
METHOD display_attributes.
WRITE: / icon_ws_plane as icon,
/ Name of airplane(001), AT pos_1 name,
/ Airplane type: (002), AT pos_1 planetype.
ENDMETHOD.
METHOD display_n_o_airplanes.
SAP AG
BC401
7-55
SAP AG
BC401
7-56
6ROXWLRQV
8QLW 3ULQFLSOHV RI 2EMHFW2ULHQWHG
3URJUDPPLQJ
7RSLF
*&---------------------------------------------------------------------*
*& Report
SAPBC401_AIRS_MAIN_e
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_airs_main_e.
TYPE-POOLS icon.
INCLUDE sapbc401_airs_e.
DATA: r_plane TYPE REF TO lcl_airplane,
plane_list TYPE TABLE OF REF TO lcl_airplane.
START-OF-SELECTION.
*##############################
lcl_airplane=>display_n_o_airplanes( ).
CREATE OBJECT r_plane exporting im_name = LH Berlin
im_planetype = 747-400.
APPEND r_plane TO plane_list.
r_plane->display_attributes( ).
CREATE OBJECT r_plane exporting im_name = AA New York
im_planetype = 727-200.
r_plane->display_attributes( ).
lcl_airplane=>display_n_o_airplanes( ).
SAP AG
BC401
7-57
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_AIRS_e
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
"-------------------------------CONSTANTS: pos_1 TYPE i VALUE 30.
METHODS: constructor IMPORTING
im_name
TYPE string
(;3257,1* H[BZHLJKW
H[BWDQNFDS
DATA: name
W\SH VDSODQHSODQHW\SH
7<3( VBSODQBZHL
7<3( VBFDSDFLW\
TYPE string,
SAP AG
"lcl_airplane DEFINITION
BC401
7-58
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name
= im_name.
planetype
= im_planetype.
n_o_airplanes = n_o_airplanes + 1.
ENDMETHOD.
"constructor
METHOD display_attributes.
data: weight type saplane-weight,
cap type saplane-tankcap.
WRITE: / icon_ws_plane AS ICON,
/ Name of airplane(001), AT pos_1 name,
/ Airplane type: (002), AT pos_1 planetype.
get_technical_attributes( exporting im_type = planetype
importing ex_weight = weight
ex_tankcap = cap ).
write: / wheight: (003), weight,
tankcap: (004), 60 cap.
ENDMETHOD.
"display_attributes
METHOD display_n_o_airplanes.
WRITE: /, / Number of airplanes: (ca1),
AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
ENDMETHOD.
"display_n_o_airplanes
0(7+2' JHWBWHFKQLFDOBDWWULEXWHV
,) V\VXEUF !
H[BZHLJKW
H[BWDQNFDS
(1',)
(1'0(7+2'
ENDCLASS.
SAP AG
LPBW\SH
JHWBWHFKQLFDOBDWWULEXWHV
"lcl_airplane IMPLEMENTATION
BC401
7-59
,QKHULWDQFH
&RQWHQWV
l *HQHUDOL]DWLRQ VSHFLDOL]DWLRQ RI FODVVHV
SAP AG 2002
SAP AG
BC401
8-1
SAP AG 2002
SAP AG
BC401
8-2
SAP AG
BC401
8-3
SAP AG 2002
f P#WF
Q
I RA
T S
7 7 DB #@A G FI D7 B C 9@A 87 6
I H
P7 E B E C 9 7
p
V hd g i
hg e a XV
Y fd c Y b` Y WU
&$ "
1) '
20(" " % #!
4
! ' 3 &
5
5 ! " !
g jkiRfRvd R #F
l h g e
n m
o q p e l #
q o
{ z x vv t ss
vv |ss yy iwwPuxrr
{| z xiPtux
hX gc d q
y w
XY V c d xp uvg c g q
sr
tY g V d fi cg g q
LV D
UHODWLRQVKLS
OFOBFDU
FDUBW\SH
PD[BVHDWV
DFFHOHUWLRQ
JHWBW\SH
PDNH
PRGHOO
VHUBQR
FRORU
QBRBYHKLFOHV
FRQVWUXFWRU
GLVSOD\BDWWULEXWHV
JHWBFRXQW
OFOBWUXFN
PD[BFDUJR
QBRBWDQNV
QBRBZKHHOV
JHWBFDUJR
OFOBEXV
PD[BSDVVHQJHUV
ODYDWRU\
WHOHYLVLRQ
JHWBSDVVHQJHUV
SAP AG 2002
Inheritance is a relationship, in which one class (the subclass) inherits all the main characteristics of
another class (the superclass). The subclass can also add new components (attributes, methods, and
so on) and replace inherited methods with its own implementations.
The inheritance relationship is often described as an "is a" relationship: A truck LV D vehicle.
SAP AG
BC401
8-4
OFOBVXSHU
buRuuxx u
~}
OFOBVXE
OFOBVXE
OFOBVXE
Specialization
OFOBVXSHU
Generalization
0XOWLSOH ,QKHULWDQFH"
SAP AG 2002
The situation in which a class, for example lcl_sub2, inherits from two classes (lcl_super1
and lcl_super2) simultaneously, is known as multiple inheritance. However, this is QRW
implemented in ABAP Objects. ABAP Objects only has single inheritance.
You can, however, simulate multiple inheritance in ABAP Objects using interfaces (see the section
on interfaces).
Single inheritance does not mean that the inheritance tree only has one level. On the contrary, the
direct superclass of one class can in turn be the subclass of a further superclass. In other words: The
inheritance tree can have any number of levels, so that a class can inherit from several superclasses,
as long as it only has one direct superclass.
SAP AG
BC401
8-5
OFOBVXSHU
OFOBVXE
1HZ
FRPSRQHQWV
,QKHULWHG
FRPSRQHQWV
SAP AG 2002
Inheritance provides very strong links between the superclass and the subclass. The subclass must
possess detailed knowledge of the implementation of the superclass, particularly for redefinition, but
also in order to use inherited components. Even if the superclass does not technically know its
subclasses, the subclass often makes additional requirements of the superclass, for example, because
a subclass needs certain protected components or because implementation details in the superclass
need to be changed in the subclass in order to redefine methods. The basic reason is that the
developer of a (super)class cannot normally predict all the requirements that subclasses will later
need to make of the superclass.
SAP AG
BC401
8-6
,QKHULWDQFH 6\QWD[
(1'&/$66
Normally the only other entry required for subclasses is what has changed in relation to the direct
superclass. Only DGGLWLRQV are permitted in ABAP Objects, that is, in a subclass you can "never take
something away from a superclass". All components from the superclass are automatically present in
the subclass.
The attributes of the superclass lcl_vehicle exist in the subclass lcl_truck; the method
estimate_fuel is also available in the subclass.
SAP AG
BC401
8-7
5HGHILQLQJ 0HWKRGV
&/$66 OFOBYHKLFOH '(),1,7,21
38%/,& 6(&7,21
0(7+2'6 HVWLPDWHBIXHO
,03257,1* LPBGLVWDQFH
7<3( W\BGLVWDQFH
5(7851,1* 9$/8(UHBIXHO 7<3( W\BIXHO
(1'&/$66
&/$66 OFOBWUXFN '(),1,7,21 ,1+(5,7,1* )520 OFOBYHKLFOH
38%/,& 6(&7,21
0(7+2'6 HVWLPDWHBIXHO 5('(),1,7,21
(1'&/$66
&DQQRW FKDQJH
WKH LQWHUIDFH
1HZ
LPSOHPHQWDWLRQ RI
PHWKRG
SAP AG 2002
The REDEFINITION statement for the inherited method must be in the same SECTION as the
definition of the original method. (It can therefore not be in the PRIVATE SECTION, since a classs
private methods are not visible and therefore cannot be redefined in subclasses).
If you redefine a method, you do not need to enter its interface again in the subclass, but only the
name of the method. The reason for this is that ABAP Objects does not support overloading.
In the case of redefined methods, changing the interface (RYHUORDGLQJ) is not permitted; exception:
Overloading is possible with the constructor.
Within the redefined method, you can access components of the direct superclass using the SUPER
reference.
To implement a redefined method in a subclass, you often need to call the method of the same name
in the LPPHGLDWH superclass. In ABAP Objects you can call the method from the superclass using the
pseudo-reference super:
The pseudo-reference super can only be used in redefined methods.
SAP AG
BC401
8-8
The constructor of the superclass PXVW be called within the constructor of the subclass. The reason
for this is the special function of the constructor: To ensure that objects are initialized correctly. Only
the class itself, however, can initialize its own (private) components correctly; this task cannot be
carried out by the subclass. Therefore it is essential that all (instance) constructors are called in an
inheritance hierarchy (in the correct sequence).
For static constructors, unlike instance constructors, the static constructor in the superclass is called
automatically, that is the runtime system automatically ensures that the static constructors of all its
superclasses have already been executed before the static constructor in a particular class is executed.
SAP AG
BC401
8-9
a1
constructor
(im_a1:i)
FRQVWUXFWRU
OFOB
OFOB
a2
constructor
(im_a1, im_a2)
SAP AG 2002
The model described for instance constructors must also be taken into account for CREATE
OBJECT.
There are two main methods of creating an instance of a class using CREATE OBJECT:
2. The instance constructor for that class has not been defined
In this case, you must search the inheritance hierarchy for the next highest superclass in which
the instance constructor has been defined and implemented. Then, when you are using CREATE
OBJECT, the parameters of that class must be filled (similarly to the first method above).
If there is no superclass with a defined instance constructor, then no parameters may or can be
filled.
If no instance constructor has been defined for a class, then a GHIDXOW FRQVWUXFWRU ZKLFK LV LPSOLFLWO\
DOZD\V SUHVHQW is used. This default constructor calls the constructor from the immediate superclass.
SAP AG
BC401
8-10
3XEOLF FRPSRQHQWV
38%/,& 6(&7,21
9LVLEOH WR DOO
3527(&7(' 6(&7,21
'$7$ WDQN 7<3( 5() 72 OFOBWDQN
'LUHFW DFFHVV
3URWHFWHG FRPSRQHQWV
3ULYDWH FRPSRQHQWV
...
35,9$7( 6(&7,21
'$7$ PDNH 7<3( VWULQJ
(1'&/$66
OFOBYHKLFOH
PDNH
PRGHOO
WDQN
...
FRQVWUXFWRU
GLVSOD\BDWWULEXWHV
JHWBFRXQW
|x
bD ~
xxu
SAP AG 2002
Inheritance provides an extension of the visibility concept: There are protected components. The
visibility of these components lies between that of the public components (visible to all users, all
subclasses, and the class itself), and private (visible only to the class itself). Protected components
are visible to and can be used by all subclasses and the class itself.
Subclasses cannot access the private components (particularly attributes) of the superclass. Private
components are genuinely private. This is particularly important if a (super)class needs to make local
enhancements to handle errors: It can use private components to do this without knowing or
invalidating subclasses.
In ABAP Objects, you must keep to the section sequence PUBLIC, PROTECTED, PRIVATE.
SAP AG
BC401
8-11
OFOBEXV
Public
,QKHULWHG
Private
JHWBPDNH
VHWBPDNH
JHWBFRXQW
GLVSOD\BDWWULEXWHV
HVWLPDWHBIXHO
Protected
,QKHULWHG
WDQN
&DQ RQO\ EH
DGGUHVVHG ZLWKLQ
WKH FODVV
SAP AG 2002
In this example, lcl_bus, a subclass of lcl_vehicle, can directly access the protected attribute
tank. If the attribute was private, the subclasses would only be able to access tank using nonprivate methods.
SAP AG
BC401
8-12
OFOBYHKLFOHV
HVWLPDWHBIXHO
OFOBWUXFN
OFOBEXV
0iFvRF0%
0i8P%W%
OFOBFDU
i8v%%R
DDb|%DdxuxDbDxx ~ u
SAP AG 2002
In ABAP Objects, you can not only add new components, but also provide inherited methods with
new implementations. This is known as UHGHILQLWLRQ. You can only redefine (public and protected)
instance methods, other components (static methods, attributes and so on) cannot be redefined.
Changes to method parameters (signature changes) are not possible.
In UML, the redefinition of a method is represented by listing the method again in the subclass.
Methods (and all other components) that are inherited but not redefined are not listed in the subclass,
as their existence there is clear from the specialization relationship.
You should not confuse redefinition with "overloading". The latter describes the ability of a class to
have methods with the same name but a different signature. This is not available in ABAP Objects.
There is only one static event per roll area. In this way, a class that defines a public or protected
static attribute shares this attribute with all its subclasses. The significant point here is that subclasses
do not each receive their own copy of the static attribute.
SAP AG
BC401
8-13
OFOBWUXFN
PD[BSDVVHQJHUV
...
PD[BFDUJR
...
FRQVWUXFWRU
HVWLPDWHBIXHO
FRQVWUXFWRU
HVWLPDWHBIXHO
0(7+2' HVWLPDWHBIXHO
'$7$ WRWDOBZHLJKW
* just an example!
WRWDOBZHLJKW
PD[BSDVVHQJHUV
DYHUDJHBZHLJKW ZHLJKW
UHBIXHO
WRWDOBZHLJKW
LPBGLVWDQFH
IDFWRU
(1'0(7+2'
0(7+2' HVWLPDWHBIXHO
'$7$ WRWDOBZHLJKW
* just an example!
WRWDOBZHLJKW
PD[BFDUJR
ZHLJKW
UHBIXHO
WRWDOBZHLJKW
LPBGLVWDQFH
IDFWRU
(1'0(7+2'
SAP AG 2002
In the above example, both redefined methods calculate the return code in different ways. The
important point is that the semantics stay the same.
SAP AG
BC401
8-14
SAP AG 2002
SAP AG
BC401
8-15
,QKHULWDQFH ([HUFLVHV
8QLW ,QKHULWDQFH
=%&B B0$,1B$,53/$1(
B$,53/$1( include program
Make both instance attributes of the class OFOBDLUSODQH visible to their subclasses
(PRIVATE SECTION -> PROTECTED SECTION).
1-2
Create the subclass OFOBSDVVHQJHUBSODQH for the class OFOBDLUSODQH. Also, create
this subclass in your include program.
1-2-1 The class is to have a private instance attribute PD[BVHDWV with the same
type as table field VIOLJKWVHDWVPD[.
1-2-2 A public constructor is to be defined and implemented in the class. This
constructor provides DOO instance attributes in the class with values.
1-2-3 Redefine the method GLVSOD\BDWWULEXWHV of the class OFOBDLUSODQH, so that,
using the redefined method, the WRITE statement displays DOO instance
attributes.
1-3
Create the subclass OFOBFDUJRBSODQH for the class OFOBDLUSODQH. Also, create this
subclass in your include program.
1-3-1 The class is to have a private instance attribute PD[BFDUJR with the
same type as the table field VFSODQHFDUJRPD[.
1-3-2 A public constructor is to be defined and implemented in the class. This
constructor provides DOO instance attributes in the class with values.
1-3-3 Redefine the method GLVSOD\BDWWULEXWHV of the class OFOBDLUSODQH, so that,
using the redefined method, the WRITE statement displays DOO instance
attributes.
SAP AG
BC401
8-16
1-4
1-5
Follow the program flow in the Debugger, paying special attention to the call of
GLVSOD\BDWWULEXWHV.
1-6
1-7
Is it necessary for the subclasses to directly access the attributes QDPH and
SODQHW\SH of the superclass to initialize them?
Or, to formulate it differently:
If these attributes remained in the private visibility area of the superclass, how
would the subclasses have to access the attributes?
SAP AG
BC401
8-17
,QKHULWDQFH 6ROXWLRQV
8QLW ,QKHULWDQFH
7RSLF
*&---------------------------------------------------------------------*
*& Report
SAPBC401_INHS_MAIN_a
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_inhs_main_a.
INCLUDE <icon>.
INCLUDE sapbc401_inhs_a.
'$7$ UBSODQH 7<3( 5() 72 OFOBDLUSODQH
/+ %(5/,1
LPBSODQHW\SH
LPBVHDWV
&5($7( 2%-(&7 UBFDUJR (;3257,1*
LPBQDPH
86 +(UFXOHV
LPBSODQHW\SH
LPBFDUJR
UBFDUJR!GLVSOD\BDWWULEXWHV
UBSDVVHQJHU!GLVSOD\BDWWULEXWHV
lcl_airplane=>display_n_o_airplanes( ).
SAP AG
BC401
8-18
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_INHS_a
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
*...
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBFDUJRBSODQH '(),1,7,21 ,1+(5,7,1* )520 OFOBDLUSODQH
38%/,& 6(&7,21
SAP AG
OFOBFDUJRBSODQH '(),1,7,21
BC401
8-19
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBFDUJRBSODQH ,03/(0(17$7,21
0(7+2' FRQVWUXFWRU
(1'0(7+2'
LPBFDUJR
LPBQDPH
LPBSODQHW\SH
FRQVWUXFWRU
0(7+2' GLVSOD\BDWWULEXWHV
VXSHU!GLVSOD\BDWWULEXWHV
:5,7(
0D[ &DUJR
8/,1(
(1'0(7+2'
(1'&/$66
PD[BFDUJR
GLVSOD\BDWWULEXWHV
OFOBFDUJRBSODQH ,03/(0(17$7,21
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBSDVVHQJHUBSODQH '(),1,7,21 ,1+(5,7,1* )520 OFOBDLUSODQH
38%/,& 6(&7,21
(1'&/$66
SAP AG
OFOBSDVVHQJHUBSODQH '(),1,7,21
BC401
8-20
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBSDVVHQJHUBSODQH ,03/(0(17$7,21
0(7+2' FRQVWUXFWRU
(1'0(7+2'
LPBSODQHW\SH
LPBVHDWV
LPBQDPH
LPBSODQHW\SH
FRQVWUXFWRU
0(7+2' GLVSOD\BDWWULEXWHV
VXSHU!GLVSOD\BDWWULEXWHV
:5,7(
0D[ 6HDWV
8/,1(
(1'0(7+2'
ENDCLASS.
SAP AG
PD[BVHDWV
GLVSOD\BDWWULEXWHV
"lcl_passenger_plane IMPLEMENTATION
BC401
8-21
&DVWLQJ
&RQWHQWV
l &DVW
l 3RO\PRUSKLVP
SAP AG 2002
SAP AG
BC401
9-1
SAP AG 2002
SAP AG
BC401
9-2
SAP AG
BC401
9-3
SAP AG 2002
$x
R
P S
U T
8 8 EB "@A H FGP E8 CD 9@A 8 A 8
P
QI8 F B D B 9
7
h
W fc e g
fY &c CY 5CY $V
e d b a ` XW
&$ "
%# !
) '
' 3 % 120(! !
6
6 ! 45
igSSQ C "$
j hf e d
e k &
m o on m l j " C p
nj q
y xw v s
QvGutr
fX eb c i
w u s
XY W b c vh te b e i
qp
r
Y e W c &g be e i
&DVWLQJ
1DUURZLQJ FDVW
1DUURZLQJ FDVW
:LGHQLQJ FDVW
:LGHQLQJ FDVW
,QKHULWDQFH DQG SRO\PRUSKLVP
,QKHULWDQFH DQG SRO\PRUSKLVP
SAP AG 2002
SAP AG
BC401
9-4
OFOBWUXFN
*HHUEW
S~|z
z} {
JHWBPDNH
JHWBFRXQW
{
v5vz
5HGHILQLHUW
GLVSOD\BDWWULEXWHV
VHWBDWWULEXWHV
HVWLPDWHBIXHO
1DUURZLQJ &DVW
UBYHKLFOH
UBWUXFN
JHWBFDUJR
{
v5vz
SAP AG 2002
After the narrowing cast, you can use the r_vehicle reference to access the components of the
truck instance that were inherited from lcl_vehicle - obviously, in some cases with the
limitations entailed by their visibility. You can no longer access the truck-specific part of the
instance (get_cargo in the above example) using the r_vehicle reference.
SAP AG
BC401
9-5
'$7$ UBYHKLFOH
UBWUXFN
OHYHO
One of the significant principles of inheritance is that an instance from a subclass can be used in any
context, in which an instance from the superclass appears. This is possible because the subclass has
inherited DOO components from the superclass and therefore has the same interface as the superclass.
The user can therefore address the subclass instance in the same way as the superclass instance.
Variables that point to a superclass instance can also refer to subclass instances at runtime.
The assignment of a subclass instance to a reference variable of the type "reference to superclass" is
described as a narrowing cast, because you are switching from a more detailed view to a one with
less detail.
What is a narrowing cast used for? A user who is not interested in the finer points of cars, trucks, and
busses (but only, for example, in the fuel consumption and tank gauge) does not need to know about
them. This user only wants and needs to work with (references to) the lcl_vehicle class.
However, in order to allow the user to work with cars, busses, or trucks, you generally need a
narrowing cast.
SAP AG
BC401
9-6
*HQHULF $FFHVV"
OFOBUHQWDO
1..*
OFOBYHKLFOHV
7KH FOLHQW
OFOBWUXFN
OFOBEXV
OFOBUHQWDO
Private
LWDE
2| i
i i i C
Public
i2 i C
SAP AG 2002
A possible client (car rental company for example) wants to access the list of vehicles with
identically-named services. The client wishes to, for example, calculate the required amount of fuel.
The client is not concerned with the details of how the car, bus, or truck do this.
SAP AG
BC401
9-7
OFOBUHQWDO
0tg
DGGBYHKLFOH
Private
i i
Public
v i
7KH FOLHQW
2 i
...
...
SAP AG 2002
Objects from different classes (lcl_bus, lcl_truck, and lcl_car in the above example ) can
be stored in an internal table consisting of references to the superclass (lcl_vehicle in the above
example),and then processed in a uniform manner using the same access technique.
A client, the car rental company in this example, can then generically access the identically-named
service of the different classes. (This will be discussed in more detail.)
SAP AG
BC401
9-8
When objects from different classes react differently to the same method call, this is known as
SRO\PRUSKLVP. To do this, the classes implement the same method in different ways. This can be
done using inheritance, by redefining a method from the superclass in subclasses and implementing
it differently. (,QWHUIDFHV will be discussed later; they too can enable polymorphic behavior.)
When an instance receives a message to execute a particular method, then that method is executed if
it has been implemented by the class the instance belongs to. If the class has not implemented that
method, but only inherited and not redefined it, then a search up through the inheritance hierarchy is
carried out until an implementation of that method is found.
The dynamic type, not the static type of the reference variable is used to search for the
implementation of a method (will be discussed later). r_vehicle->estimate_fuel above
therefore uses the class of the instance that r_vehicle actually refers to to search for the
implementation of estimate_fuel. The static type for r_vehicle, which is always REF TO
lcl_vehicle is not used.
Polymorphism is one of the main strengths of inheritance: The user can work in the same way with
different classes, regardless of their implementation. The search for the right implementation of a
method is carried out by the runtime system, not the user.
SAP AG
BC401
9-9
|v&vi
vv&ii
Ciii
0tg
$ &
Public
|v&v|i
vvii
ECiii
SAP AG 2002
Which coding is actually executed when estimate_fuel is called depends on the dynamic type
of the reference variable r_vehicle, that is it depends on which object from which (sub)class
r_vehicle refers to.
You can use polymorphism to write programs that are generic to a high degree and that do not even
need to be changed if use cases are added. In the simple example above, this means that, should a
further subclass be added, for example for motorbikes, the above coding would not need to be
changed.
A redefined method will be created in the server class lcl_motorbike.
SAP AG
BC401
9-10
'$7$ UBYHKLFOH
7<3( 5() 72 OFOBYHKLFOH
|v&vi
|v&v|i
||2
2&i
iii
i|i|i
2| i
||2
22&i
iii
i|i|i
i v v
vv&v|
SAP AG 2002
In the example, the static type of the r_vehicle variable is always REF TO lcl_vehicle, but
its dynamic type after the cast is REF TO lcl_bus or REF TO lcl_truck.
In the Debugger, the reference me can be used to determine the dynamic type.
SAP AG
BC401
9-11
...
SAP AG 2002
SAP AG
BC401
9-12
&DVWLQJ
1DUURZLQJ FDVW
1DUURZLQJ FDVW
:LGHQLQJ FDVW
:LGHQLQJ FDVW
,QKHULWDQFH DQG SRO\PRUSKLVP
,QKHULWDQFH DQG SRO\PRUSKLVP
SAP AG 2002
SAP AG
BC401
9-13
v| i
0Sg
2 i
0
g Q
0(7+2' JHWBPD[BFDUJR
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH
/223 $7 YHKLFOHBOLVW ,172 UBYHKLFOH
3UREOHP LV WKLV UHDOO\ D WUXFN "
'HWHUPLQH WKH PD[ FDUJR
(1'/223
(1'0(7+2'
i i i
JHWBPD[BFDUJR
Private
0
t
Public
i| v i
OFOBUHQWDO
7KH FOLHQW
7UXFN RU RWKHU
YHKLFOH"
SAP AG 2002
The client, the car rental company in the above example, wants to execute a function for specific
vehicles form the list (vehicle_list). For example, the client wants to ascertain the truck with
the largest cargo capacity.
However, not all vehicles are in the trucks list, it also includes references to cars and busses.
SAP AG
BC401
9-14
v| i
0
t
OFOBUHQWDO
Qi0tE |
iii2|vv
i| i t
CGSi
2 i Q
i 2 Q
0
g Q
0(7+2' JHWBPD[BFDUJR
'$7$ UBYHKLFOH 7<3( 5() 72 OFOBYHKLFOH
UBWUXFN 7<3( 5() 72 OFOBWUXFN
/223 $7 YHKLFOHBOLVW ,172 UBYHKLFOH
75<
UBWUXFN " UBYHKLFOH
SXW PD[ FDUJR LQ YDULDEOH UHBFDUJR
&$7&+ F[BV\BPRYHBFDVWBHUURU
UHDFW RQ WKDW FDVW HUURU
(1'75<
(1'/223
(1'0(7+2'
SAP AG 2002
The type of case described above is known as a widening cast (or "down cast") because it changes
from a less detailed view to one with more detail. The target reference (r_truck in the above
example) must correspond to the object reference (r_vehicle in the above example), that is the
instance must have the details implied by the reference.
The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot
be checked statically, only at runtime. The &DVW 2SHUDWRU ?= (or the equivalent MOVE ... ?TO
) must be used to make this visible.
With this kind of cast, a check is carried out at runtime to ensure that the current content of the
source variable corresponds to the type requirements of the target variables. In this example, it
checks that the dynamic type of the source reference r_vehicle is compatible with the static type
of the target reference r_truck. If it is, the assignment is carried out. Otherwise, an exception that
can be handled is raised, and the original value of the target variable remains the same. This
exception of the error class CX_SY_MOVE_CAST_ERROR can be caught using TRY-ENDTRY and
the CATCH statement. (This will be discussed in more detail later.)
Another way of preventing the runtime error would be to use RTTI (Runtime Type Identification).
This is a class library for ascertaining type attributes at runtime.
SAP AG
BC401
9-15
&DVWLQJ
1DUURZLQJ FDVW
1DUURZLQJ FDVW
:LGHQLQJ FDVW
:LGHQLQJ FDVW
,QKHULWDQFH DQG SRO\PRUSKLVP
,QKHULWDQFH DQG SRO\PRUSKLVP
SAP AG 2002
SAP AG
BC401
9-16
5XOHV
l
l
l
SAP AG 2002
A subclass instance can be used in any context in which a superclass instance also appears.
Moreover: The user does not and is not intended to know whether they are dealing with a subclass or
a superclass instance. The user works only with references to the superclass and must rely on the
inherited components behaving in the subclass instances exactly as they do in the superclass
instances, otherwise the program will not work.
On the other hand, this ensures useful restrictions on the implementation of the subclasses: Inherited
components must keep their inherited semantics. You cannot use inherited attributes or events in any
way other than intended in the superclass, and you cannot change method semantics by redefinition.
You must avoid "code inheritance": It is not correct for one class to inherit from another simply
because SDUW of the functionality required is already implemented there.
SAP AG
BC401
9-17
FDU
FDUBUHG
"
FDUBEOXH
FKDQJHBZLGWK
6XSHUFODVV
SAP AG 2002
6XEFODVV
FKDQJHBKHLJKW
"
C
5XOHV
Because an instance cannot change its class, in circumstances like the following, you should not use
inheritance directly, but use a so-called role design pattern instead:
The class HPSOR\HH has the subclasses IXOOWLPH HPSOR\HH and SDUWWLPH HPSOR\HH. What happens
when a part-time employee becomes a full-time employee? A new full-time employee object would
have to be instantiated and all the information copied from the part-time employee object. However,
users who still have a reference to the part-time employee instance would then be working with a
part-time employee object that logically does not exist anymore.
The use of inheritance does not always correspond to expectations in the real world: For example, if
the class VTXDUH inherits from the class UHFWDQJOH, the latter will have two separate methods for
changing length or width, although the sides of the square actually need to be changed by the same
measurement.
SAP AG
BC401
9-18
l 8VH FDVWV
SAP AG 2002
SAP AG
BC401
9-19
&DVWLQJ ([HUFLVHV
8QLW &DVWLQJ
7RSLF 3RO\PRUSKLVP
At the conclusion of these exercises, you will be able to:
Describe polymorphism and inheritance
Use generic programming for inheritance relationships and implement
polymorphic method calls
0RGHO VROXWLRQ
6$3%&B&$66B0$,1B$
6$3%&B,1+6B$ include program
1-1
1-2
In your main program, define an internal table for buffering airplane objects. The
type of the internal table should be 5() 72 OFOBDLUSODQH.
1-3
Try to insert the planes (passenger and cargo) into this internal table and execute the
GLVSOD\BDWWULEXWHV method for every plane in a LOOP.
Read the internal table in the LOOP using the auxiliary reference variable
UBDLUSODQH (type 5() 72 OFOBDLUSODQH).
1-3-1 Was this successful?
1-3-2 Check the internal table and the execution of the GLVSOD\BDWWULEXWHV method
in the Debugger
1-3-3 Which source code is executed when the GLVSOD\BDWWULEXWHV method is
called, the original method from the superclass or the relevant redefined
methods from the subclasses? What would happen if one of these methods
had not been redefined in the subclass?
0RGHO VROXWLRQ
6$3%&B&$66B0$,1B%
6$3%&B&$66B% include program
7HPSODWH
SAP AG
6$3%&B&$67B%
BC401
9-20
2-1
2-2
B$,53/$1(
2-2-1 The first method is DGGBDLUSODQH, which adds planes to the list of planes
DLUSODQHBOLVW already defined in the class. The transfer parameter is a
reference to the class OFOBDLUSODQH.
Have a close look at the definition of the internal table DLUSODQHBOLVW.
2-2-2 The second method is GLVSOD\BDLUSODQHV, which displays the planes from the
list DLUSODQHBOLVW. The GLVSOD\BDWWULEXWHV method from class OFOBDLUSODQH
should be called at this point.
2-2-3 The third method is GLVSOD\BDWWULEXWHV, which displays the airline attributes.
This includes the airline name and the list of planes (you can use LFRQBIOLJKW
as an icon before the name).
2-3
SAP AG
BC401
9-21
&DVWLQJ 6ROXWLRQV
8QLW &DVWLQJ
7RSLF
3RO\PRUSKLVP
*&---------------------------------------------------------------------*
*& Report
SAPBC401_CASS_MAIN_a
*&
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_cass_main_a.
INCLUDE <icon>.
INCLUDE sapbc401_inhs_a.
START-OF-SELECTION.
*##############################
lcl_airplane=>display_n_o_airplanes( ).
CREATE OBJECT r_passenger EXPORTING
im_name = LH BERLIN
im_planetype = 747-400
im_seats = 345.
$33(1' UBSDVVHQJHU 72 SODQHBOLVW
CREATE OBJECT r_cargo EXPORTING
im_name = US HErcules
im_planetype = 747-500
SAP AG
BC401
9-22
im_cargo = 533.
$33(1' UBFDUJR 72 SODQHBOLVW
/223 $7 SODQHBOLVW ,172 UBSODQH
UBSODQH!GLVSOD\BDWWULEXWHV
(1'/223
lcl_airplane=>display_n_o_airplanes( ).
*&---------------------------------------------------------------------*
*& Report
SAPBC401_CASS_MAIN_b
*&
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_cass_main_b.
INCLUDE <icon>.
INCLUDE sapbc401_cass_b.
START-OF-SELECTION.
*##############################
***** Create Carrier ********************************************
FUHDWH REMHFW UBFDUULHU H[SRUWLQJ LPBQDPH
6PLOH )O\7UDYHO
SAP AG
BC401
9-23
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
* Class definitions of ... lcl_airplane, cargo- and passenger_plane
* ...
* ...
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBFDUULHU '(),1,7,21
38%/,& 6(&7,21
GLVSOD\BDLUSODQHV
GLVSOD\BDWWULEXWHV
35,9$7( 6(&7,21
'$7$ QDPH
(1'&/$66
SAP AG
7<3( VWULQJ
BC401
9-24
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBFDUULHU ,03/(0(17$7,21
0(7+2' DGGBDLUSODQH
(1'0(7+2'
0(7+2' GLVSOD\BDWWULEXWHV
(1'0(7+2'
0(7+2' GLVSOD\BDLUSODQHV
HQGORRS
(1'0(7+2'
0(7+2' FRQVWUXFWRU
QDPH
LPBQDPH
(1'0(7+2'
0(7+2' JHWBQDPH
H[BQDPH
(1'0(7+2'
QDPH
(1'&/$66
SAP AG
BC401
9-25
,QWHUIDFHV
&RQWHQWV
l ,QWHUIDFHV
l &RPSRXQG LQWHUIDFHV
l 3RO\PRUSKLVP
SAP AG 2002
SAP AG
BC401
10-1
SAP AG 2002
SAP AG
BC401
10-2
SAP AG
BC401
10-3
SAP AG 2002
x
c c %yw
G P
Q
7 R7 (A #9@ E CDG (7 A B 89@ 8I7 @ 7
HF7 C A B
G
6
g
T ea d f
eV c `a V YW V %S
d b X UT
&$ "
" % #!
1) '
! ' 3 & 20("
5
5 ! " ! 4
20PdPH #D
h gf e
e i c
k m ml k jc h # P n
l h p o
k h l q
z
vv u
{{ zy w ust
vv ycxx w(ssstrr
eU d` a h
v r
UV T ` a tug sd ` d h
pi
qV d T a cf `d d h
,QWHUIDFHV
,QWHUIDFHV 3ULQFLSOHV
,QWHUIDFHV 3ULQFLSOHV
:RUNLQJ ZLWK LQWHUIDFHV
:RUNLQJ ZLWK LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV
SAP AG 2002
SAP AG
BC401
10-4
,QWHUIDFHV 8VH
6HUYLFH
6HUYLFH
6HUYLFH
"
,QWHUIDFHV
0}~|
OFOBFDUULHU
OFOBUHQWDO
OFOBKRWHO
OFOBYHKLFOHV
OFOBDLUSODQH
SAP AG 2002
In ABAP Objects, interfaces are implemented in addition to and independently of classes. Interfaces
only describe the external point of contact of a class (protocols), they do not contain any
implementation.
Interfaces are usually defined by a user. The user describes in the interface which services (technical
and semantic) it needs in order to carry out a task. The user never actually knows the providers of
these services, but communicates with them through the interface. In this way the user is protected
from actual implementations and can work in the same way with different classes/objects, as long as
they provide the services required. This is known as SRO\PRUSKLVP with interfaces.
SAP AG
BC401
10-5
,QWHUIDFHV 6HUYLFHV
GLVSOD\BSDUWQHU
FKHFNBDYDLODELOLW\
,QWHUIDFH
(s(uu
}
0~|
OFOBFDUULHU
OFOBUHQWDO
OFOBKRWHO
OFOBYHKLFOHV
OFOBDLUSODQH
SAP AG 2002
In the above example, a client wants to use the same services of different server classes
(lcl_carrier, lcl_hotel, and lcl_rental) in the same way. There are different
implementations of the services in the server classes (polymorphism).
The following are examples of shared services of the server classes in the above case:
display_partner, check_availability, booking, or cancel_booking.
SAP AG
BC401
10-6
&OLHQW
,W LV SRVVLEOH WR
,PSOHPHQWDWLRQ
LPSOHPHQW VHYHUDO
PHKUHUHU ,QWHUIDFHV
LQWHUIDFHV
P|JOLFK
,QWHUIDFH!!
,QWHUIDFH!!
OLIBSDUWQHUV
OLIBOLFHQVH
LPSOHPHQWV
OFOBFDUULHU
OFOBUHQWDO
OFOBKRWHO
OFOBYHKLFOHV
OFOBDLUSODQH
SAP AG 2002
In UML, interfaces can represented in the same way as classes. However, they always have the
stereotype interface above their name and can therefore be distinguished from classes.
The use of an interface is represented by a dotted line with a two-sided arrow from the user to the
interface, the stereotype uses is optional. The fact that a class implements an interface is
represented by a dotted line with a three-sided arrow from the class to the interface. The similarity to
the representation of inheritance is intentional, because the interface can be seen as a generalization
of the class implemented or the class can be seen as a specialization of the interface.
In ABAP Objects, the same components can be defined in interfaces and in classes. This allows you
to shift part of the public point of contact of a class into an interface, even though the class is already
in use; users will not notice the difference as long as you use alias names (see appendix) for the
components that are now in the interface.
A class can implement any number of interfaces and an interface can be implemented by any number
of classes. By implementing several interfaces, a class can simulate multiple inheritance. In the
example, lcl_rental implements the interfaces lif_partners and lif_license.
SAP AG
BC401
10-7
l 'HFODULQJ WKH
LQWHUIDFH
,17(5)$&( OLIBSDUWQHUV
0(7+2'6 GLVSOD\BSDUWQHU
(1',17(5)$&(
&/$66 OFOBUHQWDO '(),1,7,21
38%/,& 6(&7,21
,17(5)$&(6 OLIBSDUWQHUV
(1'&/$66
&/$66 OFOBUHQWDO ,03/(0(17$7,21
0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU
MXVW FDOO H[LVWLQJ PHWKRG WKDW ILWV
GLVSOD\BDWWULEXWHV
...
(1'0(7+2'
(1'&/$66
,QWHUIDFH
UHVROXWLRQ
RSHUDWRU
SAP AG 2002
In ABAP Objects, the same components (attributes, methods, constants, types, alias names) can be
defined in an interface in largely the same way as in classes. However, interfaces do not have
component visibility levels.
The interface name is listed in the definition part of the class. Interfaces can only be
implemented publicly and are therefore always in the PUBLIC SECTION. If you do not do this,
you risk multiple implementations if a superclass and a subclass both implement the same
interface privately.
The operations defined in the interface are implemented as methods of a class. A check is carried
out to ensure that all the methods defined in the interfaces are actually present in the
implementation part of the class (for global interfaces, a missing or superfluous implementation
of an interface method results in a warning).
The attributes, events, constants, and types defined in the interface are automatically available to
the class carrying out the implementation.
Interface components are addressed in the class carrying out the implementation by
prefixing the interface name, followed by a tilde (the ,QWHUIDFH 5HVROXWLRQ 2SHUDWRU):
<interfacename>~<componentname>.
SAP AG
BC401
10-8
,QWHUIDFHV )HDWXUHV
6HSDUDWLRQ RI H[WHUQDO SRLQW RI FRQWDFW
LQWHUIDFH DQG LPSOHPHQWDWLRQ FODVV
3RO\PRUSKLVP
$EVWUDFWLRQ
2
yy0
,QWHUIDFH DV D JHQHUDOL]DWLRQ RI
WKH LPSOHPHQWLQJ FODVV
u p
P
H
p D
H
SAP AG 2002
Interfaces are the means of choice for describing external points of contact or protocols, without
linking them to a type of implementation. An extra layer is introduced between the client and the
server to protect the client explicitly from the server, thereby making the client independent.
Interfaces enable you to work uniformly with different classes (providers/servers). In particular, they
always ensure polymorphic behavior as they do not have their own implementation, but instead
allow the providers to carry it out.
The definition of an interface is always an abstraction: The user wants to handle various providers in
the same way and must therefore abstract concrete implementations to a description of the services
required to fulfill the task.
You can also use interfaces to achieve multiple inheritance by defining the functionality to be
inherited by a second class as an interface that the inheriting class then has to implement.
SAP AG
BC401
10-9
,QWHUIDFHV
,QWHUIDFHV 3ULQFLSOHV
,QWHUIDFHV 3ULQFLSOHV
:RUNLQJ ZLWK LQWHUIDFHV
:RUNLQJ ZLWK LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV
SAP AG 2002
SAP AG
BC401
10-10
OLIBSDUWQHUV
GLVSOD\BSDUWQHU
FKHFNBDYDLODELOLW\
LPSOHPHQWV
OFOBUHQWDO
QDPH
YHKLFOHBOLVW
DGGBYHKLFOH
GLVSOD\BDWWULEXWHV
You can access interface components using an object reference, whose class implements the
interface. Syntactically this is done with the interface resolution operator, just as with the method
definitions in the implementation part of the class.
This allows you to differentiate between components defined in the interface and components of the
same name that are defined in the class itself. The reason for this is the shared namespace.
To simplify accessing interface methods you can work with DOLDV names.
Alias names can only appear in the in the declaration part of a class or in the interface definition.
Example for an alias in the interface: ALIASES a1 FOR lif_interface~method1
An alias defined in this way can be directly addressed using r_ref->a1.
SAP AG
BC401
10-11
OFOBUHQWDO
Private
YY
uu
,QWHUIDFH
FRPSRQHQWV
1DUURZLQJ &DVW
UBOLI
UBUHQWDO
GLVSOD\BSDUWQHU
FKHFNBDYDLODELOLW\
ut
SAP AG 2002
Interfaces are addressed using interface references. Interface references always refer to instances in
the classes carrying out the implementation. Interface references always have both static and
dynamic types.
The assignment of an object reference to an interface reference is known as a narrowing cast since,
as with inheritance, only a part of the object interface is visible once you have assigned the reference.
With an interface reference, you can no longer address all components in the class carrying out the
implementation, but only the components defined in the interface. These components are now
addressed using the interface reference exclusively with their own short name.
When an object reference is assigned to an interface reference, you must be able to convert the
dynamic type of the interface reference to the static type of the object reference - that is, the class
that was used to define the object reference must have implemented the interface of the interfacereference.
SAP AG
BC401
10-12
t 2 y
DGGBSDUWQHU
Private
0s
Public
22 2
OFOBWUDYHOBDJHQF\
2t 2
...
...
SAP AG 2002
The travel agent is a user (client) of the created interface. The travel agency keeps references to its
business partners in an internal table. Th type of this internal table is REF TO lif_partners,
that is the reference to the interface lif_partners.
When it is called, the clients public method add_partner is transferred the business partner
references. In the method, the references are inserted into the table partner_list. Therefore, the
interface parameter of the add_partner method has the type REF TO lif_partners.
The aim here is polymorphism: It needs to be possible to generically access the services of the
interface later.
SAP AG
BC401
10-13
...
2 t 2
y0sP
u
0(7+2' aGLVSOD\BSDUWQHU
GLVSOD\BUHQWDOBGDWD ...
(1'0(7+2'
2t 2
0(7+2' aGLVSOD\BSDUWQHU
GLVSOD\BDWWULEXWHV ...
(1'0(7+2'
SAP AG 2002
Polymorphism can also be used for interfaces: you can use interface references to call methods that
can have a GLIIHUHQW LPSOHPHQWDWLRQ depending on the object behind the reference.
The dynamic type, not the static type of the reference variable is used to search for the
implementation of a method. In the above example, r_partner->display_partner( )
therefore uses the class of the instance that r_partner actually refers to to search for the
implementation of display. The static type for r_partner, which is always REF TO
lif_partners, is not used.
SAP AG
BC401
10-14
HHs H
222pt2c
% %
0(7+2' ERRNBIOLJKW
'$7$ UBFDUULHU 7<3( 5() 72 OFOBFDUULHU
UBSDUWQHU 7<3( 5() 72 OFOBSDUWQHU
/223 $7 SDUWQHUBOLVW ,172 UBSDUWQHU
75<
UBFDUULHU " UBSDUWQHU
FDOO PHWKRG RI OFOBFDUULHU WR ERRN IOLJKW
2 2
OFOBUHQWDO
&$7&+ F[BV\BPRYHBFDVWBHUURU
The widening cast is, as with inheritance, the opposite of the narrowing cast: Here it is used to
retrieve a class reference from an interface reference. Obviously it cannot be statically checked, since
an interface can be implemented by more than one class.
An object reference cannot be assigned to an interface reference if it has itself not implemented the
corresponding interface. It cannot be assigned even if a subclass has implemented the interface and
the interface reference points to an object in this class.
Assignments between interface references whose interfaces are not related to each other cannot be
checked statically and must therefore be formulated using the cast operator ?=.
For this type of assignment, a check must be carried out at runtime to see whether the class of the
instance that the source reference points to also supports the interface that the target reference refers
to. If this is the case, the cast is carried out, otherwise the exception that can be handled of the class
CX_SY_MOVE_CAST_ERROR is raised.
SAP AG
BC401
10-15
,QWHUIDFHV
,QWHUIDFHV 3ULQFLSOHV
,QWHUIDFHV 3ULQFLSOHV
:RUNLQJ ZLWK LQWHUIDFHV
:RUNLQJ ZLWK LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV
3UHYLHZ &RPSRXQG LQWHUIDFHV
SAP AG 2002
SAP AG
BC401
10-16
,QWHUIDFH
OLIBSDUWQHUV
OFOBFDUULHU
OFOBKRWHO
6ROXWLRQ
&RPSRXQG
LQWHUIDFHV
,QWHUIDFH
OLIBURRPBERRNLQJ
"
&RPSRXQG ,QWHUIDFHV
OFOBUHQWDO
3UREOHP
([WHQGLQJ
LQWHUIDFHV
OLIBSDUWQHUV
,QWHUIDFH
OFOBKRWHO
OFOBFDUULHU
OFOBUHQWDO
SAP AG 2002
ABAP Objects contains a composition model for interfaces. A compound interface contains other
interfaces as components (component interfaces) and summarizes the extension of these component
interfaces. An elementary interface does not itself contain other interfaces.
UML only deals with the specialization and generalization of interfaces. This relationship is
represented by a dotted line with a three-sided arrow from the specialized to the generalized
interface.
Compound interfaces in ABAP Objects can always be seen as specializations of their component
interfaces and represented as such in UML.
SAP AG
BC401
10-17
'$7$ LBSDUWQHU
LBURRPBERRN
...
LBSDUWQHU
LBURRPBERRN
1DUURZLQJ &DVW
LBURRPBERRN!OLIBSDUWQHUVaGLVSOD\BSDUWQHU
LBSDUWQHU!GLVSOD\BSDUWQHU DOVR SRVVLEOH
LBURRPBERRN "
LBSDUWQHU
:LGHQLQJ &DVW
SAP AG 2002
In a compound interface, the components of the component interface keep their original names, that
is <component-interfacename>~<componentname>; no more prefixes are added. In
other words, all components in a compound interface are on the same level, and components
inherited from component interfaces are marked with the usual interface prefix.
This equality principle for compound interfaces also affects how they are implemented. The
procedure is as follows: First, implement the elementary interfaces, then the additional methods from
the compound interfaces. For multiple compound interfaces, the process is simply repeated. In the
class carrying out the implementation, all components of all interfaces implemented are again on the
same level.
This means that interface components only ever exist once and are known by their original names
<interfacename>~<componentname>. This is true both for compound interfaces and for the
classes that implement them.
SAP AG
BC401
10-18
SAP AG 2002
SAP AG
BC401
10-19
,QWHUIDFHV ([HUFLVHV
8QLW ,QWHUIDFHV
0RGHO VROXWLRQ
6$3%&B,176B0$,1B$
6$3%&B9(+'B) include program
6$3%&B,176B$ include program
1
The classes OFOBUHQWDO OFOBYHKLFOH, and their subclasses are to be added to the existing UML
diagram.
The classes OFOBUHQWDO OFOBFDUULHU, and later OFOBKRWHO will all store separate services and
provide a possible client (to be defined later) using an interface OLIBSDUWQHUV.
1-1
1-2
1-3
SAP AG
BC401
10-20
1-4
1-5
SAP AG
BC401
10-21
([HUFLVHV
8QLW ,QWHUIDFHV
Now the class OFOBWUDYHOBDJHQF\ (travel agency) is to be added to the existing UML
diagram. It will use the interface to access the classes OFOBUHQWDO OFOBFDUULHU OFOBKRWHO (not
yet implemented).
2-1
2-2
Add the class OFOBWUDYHOBDJHQF\ to your UML diagram. What is the role of this
class? Depict this in the UML diagram by adding text and arrows.
Fill in the UML diagram on the next page.
Copy the definition of the class OFOBWUDYHOBDJHQF\ from the template
6$3%&B9(+7B% to your own include program. Then complete this new class.
2-2-1 The travel agency uses the public method DGGBSDUWQHU to add business
partners (that is car rental companies and airlines) to the internal table
SDUWQHUBOLVW.
2-2-2 What are the types of the internal table and the interface parameter of
DGGBSDUWQHU?
2-2-3 The travel agency will use the method GLVSOD\BDJHQF\BSDUWQHUV to display
the data of its business partners.
SAP AG
BC401
10-22
2-3
2-4
SAP AG
BC401
10-23
,QWHUIDFHV 6ROXWLRQV
8QLW ,QWHUIDFHV
7RSLF
'HILQLQJ ,QWHUIDFHV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_INTS_MAIN_A
*&
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_ints_main_a.
START-OF-SELECTION.
*##############################
***** Create Carrier ********************************************
FUHDWH REMHFW UBFDUULHU H[SRUWLQJ LPBQDPH
6PLOH )O\7UDYHO
SAP AG
BC401
10-24
im_name = LH BERLIN
im_planetype = 747-400
im_seats = 345.
***** cargo Plane ************************************************
CREATE OBJECT r_cargo EXPORTING
im_name = US HErcules
im_planetype = 747-500
im_cargo = 533.
***** insert planes into itab if client ***************************
UBFDUULHU!DGGBDLUSODQH UBSDVVHQJHU
UBFDUULHU!DGGBDLUSODQH UBFDUJR
LPBFDUJR
0$1
UBUHQWDO!DGGBYHKLFOH UBWUXFN
0HUFHGHV
LPBSDVVHQJHUV
UBUHQWDO!DGGBYHKLFOH UBEXV
LPBFDUJR
UBUHQWDO!DGGBYHKLFOH UBWUXFN
SAP AG
BC401
92/92
10-25
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_INTS_a
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
...
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
&/$66 OFOBFDUULHU '(),1,7,21
38%/,& 6(&7,21
,17(5)$&(6 OLIBSDUWQHUV
GLVSOD\BDLUSODQHV
GLVSOD\BDWWULEXWHV
35,9$7( 6(&7,21
'$7$ QDPH
(1'&/$66
7<3( VWULQJ
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBFDUULHU ,03/(0(17$7,21
0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU
GLVSOD\BDLUSODQHV
(1'0(7+2'
OLIBSDUWQHUVaGLVSOD\BSDUWQHU
0(7+2' DGGBDLUSODQH
(1'0(7+2'
DGGBDLUSODQH
0(7+2' GLVSOD\BDWWULEXWHV
SAP AG
BC401
10-26
GLVSOD\BDLUSODQHV
(1'0(7+2'
GLVSOD\BDWWULEXWHV
0(7+2' GLVSOD\BDLUSODQHV
(1'/223
(1'0(7+2'
GLVSOD\BDLUSODQHV
0(7+2' FRQVWUXFWRU
QDPH
LPBQDPH
(1'0(7+2'
FRQVWUXFWRU
0(7+2' JHWBQDPH
H[BQDPH
(1'0(7+2'
(1'&/$66
SAP AG
QDPH
JHWBQDPH
OFOBFDUULHU ,03/(0(17$7,21
BC401
10-27
6ROXWLRQV
8QLW ,QWHUIDFHV
7RSLF
8VLQJ ,QWHUIDFHV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_INTS_MAIN_B
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_ints_main_b.
SAP AG
BC401
10-28
LPBFDUJR
0$1
UBUHQWDO!DGGBYHKLFOH UBWUXFN
0HUFHGHV
LPBSDVVHQJHUV
UBUHQWDO!DGGBYHKLFOH UBEXV
LPBFDUJR
UBUHQWDO!DGGBYHKLFOH UBWUXFN
92/92
SAP AG
BC401
10-29
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_VEHD_g
*&---------------------------------------------------------------------*
*---------------------------------------------------------------------*
* define client lcl_travel_agency
* it will use the interface lif_partners
*---------------------------------------------------------------------*
,17(5)$&( OLIBSDUWQHUV
0(7+2'6 GLVSOD\BSDUWQHU
(1',17(5)$&(
OLIBSDUWQHUV
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
"------------------METHODS: get_average_fuel IMPORTING im_distance TYPE s_distance
im_fuel TYPE ty_fuel
RETURNING value(re_avgfuel) TYPE ty_fuel.
METHODS
METHODS
display_attributes.
METHODS
METHODS
TYPE string.
METHODS
init_make.
CLASS-DATA:
n_o_vehicles TYPE i.
ENDCLASS.
SAP AG
"lcl_vehicle DEFINITION
BC401
10-30
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_vehicle IMPLEMENTATION.
METHOD get_average_fuel.
re_avgfuel = im_distance / im_fuel.
ENDMETHOD.
"get_average_fuel
METHOD constructor.
make = im_make.
ADD 1 TO n_o_vehicles.
ENDMETHOD.
"constructor
METHOD set_make.
IF im_make IS INITIAL.
init_make( ).
ELSE.
make = im_make.
ENDIF.
ENDMETHOD.
"set_make
METHOD init_make.
make = default make.
ENDMETHOD.
"init_make
METHOD get_make.
ex_make = make.
ENDMETHOD.
"get_make
METHOD display_attributes.
WRITE: make.
ENDMETHOD.
"display_attributes
METHOD get_count.
re_count = n_o_vehicles.
ENDMETHOD.
ENDCLASS.
SAP AG
"get_count
"lcl_vehicle IMPLEMENTATION
BC401
10-31
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
"------------------METHODS:
METHODS
display_attributes REDEFINITION.
METHODS
PRIVATE SECTION.
"------------------DATA: max_cargo TYPE ty_cargo.
ENDCLASS.
"lcl_vehicle DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_truck IMPLEMENTATION.
METHOD constructor.
super->constructor( im_make ).
max_cargo = im_cargo.
ENDMETHOD.
"constructor
METHOD display_attributes.
WRITE: / icon_ws_truck AS ICON.
super->display_attributes( ).
WRITE: 20 Cargo = , max_cargo.
ULINE.
ENDMETHOD.
"display_attributes
METHOD get_cargo.
re_cargo = max_cargo.
ENDMETHOD.
ENDCLASS.
SAP AG
"get_cargo
"lcl_vehicle DEFINITION
BC401
10-32
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_bus DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
"------------------METHODS:
METHODS
display_attributes REDEFINITION.
PRIVATE SECTION.
"------------------DATA: max_passengers TYPE i.
ENDCLASS.
"lcl_vehicle DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_bus IMPLEMENTATION.
METHOD constructor.
super->constructor( im_make ).
max_passengers = im_passengers.
ENDMETHOD.
"constructor
METHOD display_attributes.
WRITE: / icon_transportation_mode AS ICON.
super->display_attributes( ).
WRITE:
20 Passengers = , max_passengers.
ULINE.
ENDMETHOD.
ENDCLASS.
SAP AG
"display_attributes
"lcl_vehicle DEFINITION
BC401
10-33
*---------------------------------------------------------------------*
*
CLASS lcl_rental DEFINITION
*---------------------------------------------------------------------*
&/$66 OFOBUHQWDO '(),1,7,21
38%/,& 6(&7,21
0(7+2'6
0(7+2'6
0(7+2'6
GLVSOD\BDWWULEXWHV
,17(5)$&(6 OLIBSDUWQHUV
35,9$7( 6(&7,21
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBUHQWDO ,03/(0(17$7,21
0(7+2' OLIBSDUWQHUVaGLVSOD\BSDUWQHU
GLVSOD\BDWWULEXWHV
(1'0(7+2'
0(7+2'
QDPH
FRQVWUXFWRU
LPBQDPH
(1'0(7+2'
0(7+2'
OLIBSDUWQHUVaGLVSOD\BSDUWQHU
FRQVWUXFWRU
DGGBYHKLFOH
(1'0(7+2'
0(7+2'
DGGBYHKLFOH
GLVSOD\BDWWULEXWHV
(1'/223
(1'0(7+2'
(1'&/$66
SAP AG
GLVSOD\BDWWULEXWHV
OFOBUHQWDO ,03/(0(17$7,21
BC401
10-34
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBWUDYHOBDJHQF\ '(),1,7,21
38%/,& 6(&7,21
0(7+2'6
0(7+2'6
0(7+2'6
35,9$7( 6(&7,21
GLVSOD\BDJHQF\BSDUWQHUV
(1'&/$66
OFOBWUDYHOBDJHQF\ '(),1,7,21
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
&/$66 OFOBWUDYHOBDJHQF\ ,03/(0(17$7,21
0(7+2' GLVSOD\BDJHQF\BSDUWQHUV
(1'/223
(1'0(7+2'
0(7+2'
QDPH
FRQVWUXFWRU
LPBQDPH
(1'0(7+2'
0(7+2'
GLVSOD\BDJHQF\BSDUWQHUV
FRQVWUXFWRU
DGGBSDUWQHU
(1'0(7+2'
(1'&/$66
SAP AG
DGGBSDUWQHU
OFOBWUDYHOBDJHQF\ ,03/(0(17$7,21
BC401
10-35
(YHQWV
&RQWHQWV
SAP AG 2002
SAP AG
BC401
11-1
SAP AG 2002
SAP AG
BC401
11-2
SAP AG
BC401
11-3
SAP AG 2002
d dF%Dx
H Q
R
7 S7 A F9@ E CDH 7 A B 89@ 8P7 @ 7
IG7 C A B
H
6
h
U fb e g
fW d ab W `X W %T
e c Y VU
$
&% "#!
1)
! ' 3 & 20'(" "
5 5 ! " ! 4
e 20eQQI FD
h gf d
i d
k m ml k jd h F
lh o n
k h l p
d l d h n
xw u r
Ivtsq
fV ea b i
w s
VW U a b uvh te a e i
qp
rW e U b dg ae e i
(YHQWV 2YHUYLHZ
z
d~}z
| z z
PDLQ SURJUDP
&5($7( 2%-(&7 UBFDU (;3257,1*
...
&DU
|}{y
z ~ z
``Q`Q`0 `
SAP AG 2002
By triggering an event, an object or class announces a change of state, or that a certain state has been
achieved.
In the example, the class FDU triggers the event FUHDWHG. Other classes subscribe to this event
(triggered when a car is instantiated) and process it. The car rental company wants to be informed of
completion; the car is registered at the vehicle registration office.
Note:
The events discussed here are QRW the events of the ABAP runtime system (such as
INITIALIZATION,
START-OF-SELECTION, and so on) and QRW the events of background processing or workflow.
SAP AG
BC401
11-4
)HDWXUHV
l $UHDV RI XVH
*8, LPSOHPHQWDWLRQV
SAP AG 2002
Events link objects or classes more loosely than direct method calls do. Method calls establish
precisely when and in which statement sequence the method is called. However, with events, the
reaction of the object to the event is triggered by the event itself.
Other external object models, such as COM, ActiveX controls, and so on, also provide events.
SAP AG
BC401
11-5
5XOHV
l 7ULJJHULQJ HYHQWV
l +DQGOLQJ HYHQWV
SAP AG 2002
Classes or their instances that receive a message when an event is triggered at runtime and want to
react to this event define event handler methods.
Statement:
[CLASS-]METHODS <handler_method> FOR EVENT <event> OF <classname>.
These classes or their instances are registered to one or more events at runtime.
Statement:
SET HANDLER <handler_method> FOR <reference>. (for instance events)
SET HANDLER <handler_method>. (for static events)
A class or instance can trigger an event at runtime using the RAISE EVENT statement.
SAP AG
BC401
11-6
...
DFWBSDU!
...
(1'&/$66
FDU
...
s(vt0
SAP AG 2002
Events can only have EXPORTING parameters which must be passed by value.
Triggering an event using the statement RAISE EVENT has the following effect:
The event handler methods registered to this event are called and processed
If an event handler method in turn triggers an event, then the program flow is again interrupted and
all event handler methods are executed (nesting).
SAP AG
BC401
11-7
z
d~
|
UHQWDO
z{y
~ | z
d`(Qv
UHJLVWUDWLRQ
RIILFH
t0
FDUULHU
Id
`I``Q`Q`
SAP AG 2002
Events are registered using the SET HANDLER statement. Registration is only active at program
runtime. Events cannot be persistent.
You want to register an object to an event belonging to another object. The SET HANDLER
statement enters the registration in that objects list. All handlers for one event are entered in this list.
When the event is triggered, the list shows which event handler methods need to be called.
SAP AG
BC401
11-8
FDU
UHQWDO
DGGBYHKLFOH
Q`vvv`
SAP AG 2002
Event handler methods are triggered by events (RAISE EVENT), although they can also be called
like normal methods (CALL METHOD).
The interface of the event handler method consists solely of IMPORTING parameters. You can only
use parameters from the definition of the corresponding events (event interface). An event interface,
which only has EXPORTING parameters, is defined using the EVENTS statement in the declaration
of the event. The parameters are typed in the event definition and the typing is passed to the event
handler method, that is, the interface parameters of the event handler method cannot be typed in the
definition of the event handler method.
In addition to the explicitly defined event interface parameters, the implicit parameter sender can
also be listed as an IMPORTING parameter for instance events. This passes on a reference to the
object that triggered the event.
SAP AG
BC401
11-9
35,9$7( 6(&7,21
0(7+2'6 DGGBYHKLFOH )25 (9(17 YHKLFOHBFUHDWHG 2)
(1'&/$66
...
FDU
VHW KDQGOHU
UHQWDO
s0It
SAP AG 2002
When an event is triggered, only those event handler methods are executed that have, by this point,
registered themselves using SET HANDLER.
You can register an event using ACTIVATION X, and deregister it using ACTIVATION
space. If you do not specify ACTIVATION, then the event registers (default behavior).
SAP AG
BC401
11-10
FDU
YHKLFOHBFUHDWHG HYHQW
5HJLVWHUHG
+DQGOHU
REMHFW
PHWKRG
UHJBYHKLFOH
UHJLVWUDWLRQ
RIILFH
` Q
(v
FDU
YHKLFOHBFUHDWHG HYHQW
5HJLVWHUHG
+DQGOHU
REMHFW
PHWKRG
DGGBYHKLFOH
UHJBYHKLFOH
UHQWDO
UHJLVWUDWLRQ
RIILFH
SAP AG 2002
Every object that has defined events has an internal table, the handler table. All objects that have
registered for events are entered in this table together with their event handler methods.
Objects that have registered themselves for an event that is still "active" also remain "active". The
methods of these objects are called when the event is triggered, even if they can no longer be reached
using main memory references.
SAP AG
BC401
11-11
l
l
l
l
:LWK UHJDUG WR WKH *DUEDJH &ROOHFWRU UHJLVWUDWLRQ KDV WKH VDPH HIIHFW
DV D UHIHUHQFH WR WKH UHJLVWHUHG REMHFW
5HJLVWHUHG REMHFWV DUH QHYHU GHOHWHG
(YHQW KDQGOHU PHWKRGV KRZHYHU FDQ RQO\ KDYH WKH VDPH YLVLELOLW\ RU PRUH
UHVWULFWHG YLVLELOLW\ WKDQ WKH HYHQWV WKH\ UHIHU WR
SAP AG 2002
If several objects have registered for an event, then the sequence in which the event handler methods
are called is not defined, that is, there is no guaranteed sequence in which the event handler methods
are called.
If a new event handler is registered in an event handler method for an event that has just been
triggered, then this event handler is added to the end of the sequence and is then also executed when
its turn comes.
If an existing event handler is deregistered in an event handler method, then this handler is deleted
from the event handler method sequence.
Events are also subject to the visibility concept and can therefore be either public, protected, or
private. Visibility specifies who can handle an event:
All users
PROTECTED:
PUBLIC:
PRIVATE:
Event handler methods also have visibility attributes. Event handler methods, however, can only
have the same visibility or more restricted visibility than the events they refer to. The visibility of
event handler methods establishes authorization for SET HANDLER statements; SET HANDLER
statements can be used: Everywhere, in the class and its subclasses, or only within the class.
SAP AG
BC401
11-12
SAP AG 2002
SAP AG
BC401
11-13
(YHQWV ([HUFLVHV
8QLW (YHQWV
0RGHO VROXWLRQ
6$3%&B(9(6B0$,1B$
6$3%&B9(+'B+ include program
6$3%&B(9(6B$ include program
The DGGBDLUSODQH method of the class OFOBFDUULHU will no longer be explicitly called in
the PDLQ SURJUDP, but be triggered automatically from the class OFOBDLUSODQH.
Triggering an event when a plane is created (CREATE OBJECT) will result in the
automatic execution of the airline method DGGBDLUSODQH.
1-1
In the UML diagram decide what steps are needed where for the triggering and
handling of the event DLUSODQHBFUHDWHG.
See next page for UML diagram.
1-2
1-3
Handle the event within the class OFOBFDUULHU using the handler method
DGGBDLUSODQH. This method requires a new interface, its implementation must
also be changed slightly.
1-4
1-5
In the debugger, check whether the event is triggered and handled by the event
handler method when the planes are created.
If this does not happen, check whether one of the four important steps for
implementing events was perhaps left out.
SAP AG
BC401
11-14
2SWLRQDO
Implement the event YHKLFOHBFUHDWHG for the car rental company and its
corresponding vehicles.
2SWLRQDO DGYDQFHG
In the UML, there is the possibility of using events when creating the business partners
of the travel agency.
If airlines, car rental companies, or hotels are created, these business partners should
automatically be made known to the travel agency.
2-1
2-2
SAP AG
You could solve this using the events FDUULHUBFUHDWHG, UHQWDOBFUHDWHG, and
KRWHOBFUHDWHG. Would this be problematic?
What would be the best solution?
Implement your solution for OFOBFDUULHU and OFOBUHQWDO.
BC401
11-15
6ROXWLRQV
8QLW
7RSLF
(YHQWV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_EVES_MAIN_A
*&
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_eves_main_a.
SAP AG
BC401
11-16
UBFDUULHU!DGGBDLUSODQH UBSDVVHQJHU
UBFDUULHU!DGGBDLUSODQH UBFDUJR
UBUHQWDO!DGGBYHKLFOH UBWUXFN
UBUHQWDO!DGGBYHKLFOH UBEXV
UBUHQWDO!DGGBYHKLFOH UBWUXFN
SAP AG
BC401
11-17
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_EVES_A
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
"--------------------------------------------CONSTANTS: pos_1 TYPE i VALUE 30.
METHODS: constructor IMPORTING
im_name
TYPE string
TYPE saplane-planetype
EXPORTING ex_weight
TYPE s_plan_wei
ex_tankcap
DATA: name
TYPE s_capacity.
TYPE string,
SAP AG
"lcl_airplane DEFINITION
BC401
11-18
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name
= im_name.
planetype
= im_planetype.
n_o_airplanes = n_o_airplanes + 1.
UDLVH HYHQW DLUSODQHBFUHDWHG
ENDMETHOD.
"constructor
METHOD display_attributes.
DATA: weight TYPE saplane-weight,
cap TYPE saplane-tankcap.
WRITE: / icon_ws_plane AS ICON,
/ Name of airplane(001), AT pos_1 name,
/ Type of airplane: (002), AT pos_1 planetype.
get_technical_attributes( EXPORTING im_type = planetype
IMPORTING ex_weight = weight
ex_tankcap = cap ).
WRITE: / Weight:(003), weight,
Tankkap:(004), cap.
ENDMETHOD.
"display_attributes
METHOD display_n_o_airplanes.
WRITE: /, / Number of airplanes: (ca1),
AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
ENDMETHOD.
"display_n_o_airplanes
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.
ENDMETHOD.
ENDCLASS.
SAP AG
"get_technical_attributes
"lcl_airplane IMPLEMENTATION
BC401
11-19
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.
PUBLIC SECTION.
"---------------------METHODS: constructor IMPORTING im_name TYPE string
im_planetype TYPE saplane-planetype
im_cargo TYPE scplane-cargomax.
METHODS: display_attributes REDEFINITION.
PRIVATE SECTION.
"---------------------DATA: max_cargo TYPE scplane-cargomax.
ENDCLASS.
"lcl_cargo_plane DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_cargo_plane IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor
EXPORTING
im_name
= im_name
im_planetype = im_planetype.
max_cargo = im_cargo.
ENDMETHOD.
"constructor
METHOD display_attributes.
super->display_attributes( ).
WRITE: / Max Cargo = , max_cargo.
ULINE.
ENDMETHOD.
ENDCLASS.
SAP AG
"display_attributes
"lcl_cargo_plane IMPLEMENTATION
BC401
11-20
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane..
PUBLIC SECTION.
METHODS: constructor IMPORTING im_name TYPE string
im_planetype TYPE saplane-planetype
im_seats TYPE sflight-seatsmax.
METHODS: display_attributes REDEFINITION.
PRIVATE SECTION.
DATA: max_seats TYPE sflight-seatsmax.
ENDCLASS.
"lcl_passenger_plane DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_passenger_plane IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor
EXPORTING
im_name
= im_name
im_planetype = im_planetype.
max_seats = im_seats.
ENDMETHOD.
"constructor
METHOD display_attributes.
super->display_attributes( ).
WRITE: / Max Seats = , max_seats.
ULINE.
ENDMETHOD.
ENDCLASS.
SAP AG
"display_attributes
"lcl_passenger_plane IMPLEMENTATION
BC401
11-21
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_carrier DEFINITION.
PUBLIC SECTION.
"---------------------------------------INTERFACES lif_partners.
METHODS: constructor IMPORTING im_name TYPE string,
get_name RETURNING value(ex_name) TYPE string,
DGGBDLUSODQH IRU HYHQW DLUSODQHBFUHDWHG RI OFOBDLUSODQH
,03257,1* VHQGHU
display_airplanes,
display_attributes.
PRIVATE SECTION.
"----------------------------------DATA: name
TYPE string,
"lcl_carrier DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_carrier IMPLEMENTATION.
METHOD lif_partners~display_partner.
display_airplanes( ).
ENDMETHOD.
"lif_partners~display_partner
METHOD add_airplane.
APPEND sender TO airplane_list.
ENDMETHOD.
"add_airplane
METHOD display_attributes.
WRITE: icon_flight AS ICON, name . ULINE. ULINE.
display_airplanes( ).
ENDMETHOD.
"display_attributes
METHOD display_airplanes.
DATA: r_plane TYPE REF TO lcl_airplane.
LOOP AT airplane_list INTO r_plane.
r_plane->display_attributes( ).
ENDLOOP.
ENDMETHOD.
SAP AG
"display_airplanes
BC401
11-22
METHOD constructor.
name = im_name.
VHW KDQGOHU DGGBDLUSODQH IRU DOO LQVWDQFHV
ENDMETHOD.
"constructor
METHOD get_name.
ex_name = name.
ENDMETHOD.
ENDCLASS.
SAP AG
"get_name
"lcl_carrier IMPLEMENTATION
BC401
11-23
6ROXWLRQV RSWLRQDO
8QLW
7RSLF
(YHQWV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_EVES_MAIN_B
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_eves_main_b.
SAP AG
BC401
11-24
SAP AG
BC401
11-25
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_EVES_B
*&---------------------------------------------------------------------*
*------------------------------------------------------------------*
* events in: lcl_airplane and lcl_carrier !
*
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
"--------------------------------------------CONSTANTS: pos_1 TYPE i VALUE 30.
METHODS: constructor IMPORTING
im_name
TYPE string
TYPE saplane-planetype
EXPORTING ex_weight
TYPE s_plan_wei
ex_tankcap
DATA: name
TYPE s_capacity.
TYPE string,
SAP AG
"lcl_airplane DEFINITION
BC401
11-26
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name
= im_name.
planetype
= im_planetype.
n_o_airplanes = n_o_airplanes + 1.
RAISE EVENT airplane_created.
ENDMETHOD.
"constructor
METHOD display_attributes.
DATA: weight TYPE saplane-weight,
cap TYPE saplane-tankcap.
WRITE: / icon_ws_plane AS ICON,
/ Name of airplane(001), AT pos_1 name,
/ Type of airplane: (002), AT pos_1 planetype.
get_technical_attributes( EXPORTING im_type = planetype
IMPORTING ex_weight = weight
ex_tankcap = cap ).
WRITE: / Weight:(003), weight,
Tankkap:(004), cap.
ENDMETHOD.
"display_attributes
METHOD display_n_o_airplanes.
WRITE: /, / Number of airplanes: (ca1),
AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
ENDMETHOD.
"display_n_o_airplanes
METHOD get_technical_attributes.
SELECT SINGLE weight tankcap FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc <> 0.
ENDMETHOD.
ENDCLASS.
...
SAP AG
BC401
11-27
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_carrier DEFINITION.
PUBLIC SECTION.
"---------------------------------------INTERFACES lif_partners.
METHODS: constructor IMPORTING im_name TYPE string,
get_name RETURNING value(ex_name) TYPE string,
add_airplane FOR EVENT airplane_created OF lcl_airplane
IMPORTING sender,
PRIVATE SECTION.
"----------------------------------DATA: name
TYPE string,
"lcl_carrier DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_carrier IMPLEMENTATION.
METHOD lif_partners~display_partner.
display_attributes( ).
ENDMETHOD.
"lif_partners~display_partner
METHOD add_airplane.
APPEND sender TO airplane_list.
ENDMETHOD.
"add_airplane
METHOD display_attributes.
skip 2.
WRITE: icon_flight AS ICON, name . ULINE. ULINE.
display_airplanes( ).
ENDMETHOD.
"display_attributes
METHOD display_airplanes.
DATA: r_plane TYPE REF TO lcl_airplane.
LOOP AT airplane_list INTO r_plane.
r_plane->display_attributes( ).
ENDLOOP.
ENDMETHOD.
SAP AG
"display_airplanes
BC401
11-28
METHOD constructor.
name = im_name.
SET HANDLER add_airplane FOR ALL INSTANCES.
5$,6( (9(17 OLIBSDUWQHUVaSDUWQHUBFUHDWHG
ENDMETHOD.
"constructor
ENDCLASS.
"lcl_carrier IMPLEMENTATION
*&---------------------------------------------------------------------*
*&
Include
SAPBC401_VEHD_i
*&---------------------------------------------------------------------*
*---------------------------------------------------------------------*
* define client lcl_travel_agency
* it will use the interface lif_partners
*
* implement EVENT in LCL_VEHICLE and LCL_RENTAL
*---------------------------------------------------------------------*
,17(5)$&( OLIBSDUWQHUV
0(7+2'6 GLVSOD\BSDUWQHU
(1',17(5)$&(
OLIBSDUWQHUV
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
"------------------METHODS: get_average_fuel IMPORTING im_distance TYPE s_distance
im_fuel TYPE ty_fuel
RETURNING value(re_avgfuel) TYPE ty_fuel.
METHODS
METHODS
display_attributes.
METHODS
METHODS
TYPE string.
METHODS
init_make.
SAP AG
BC401
11-29
CLASS-DATA:
n_o_vehicles TYPE i.
ENDCLASS.
"lcl_vehicle DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_vehicle IMPLEMENTATION.
METHOD get_average_fuel.
re_avgfuel = im_distance / im_fuel.
ENDMETHOD.
"get_average_fuel
METHOD constructor.
make = im_make.
ADD 1 TO n_o_vehicles.
raise event vehicle_created.
ENDMETHOD.
"constructor
METHOD set_make.
IF im_make IS INITIAL.
init_make( ).
ELSE.
make = im_make.
ENDIF.
ENDMETHOD.
"set_make
METHOD init_make.
make = default make.
ENDMETHOD.
"init_make
METHOD get_make.
ex_make = make.
ENDMETHOD.
"get_make
METHOD display_attributes.
WRITE: make.
ENDMETHOD.
"display_attributes
METHOD get_count.
re_count = n_o_vehicles.
ENDMETHOD.
ENDCLASS.
SAP AG
"get_count
"lcl_vehicle IMPLEMENTATION
BC401
11-30
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_rental DEFINITION.
PUBLIC SECTION.
"------------------METHODS:
0(7+2'6
METHODS
display_attributes.
LPSRUWLQJ VHQGHU
INTERFACES: lif_partners.
PRIVATE SECTION.
"------------------DATA: name TYPE string,
vehicle_list TYPE TABLE OF REF TO lcl_vehicle.
ENDCLASS.
"lcl_rental DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_rental IMPLEMENTATION.
METHOD lif_partners~display_partner.
display_attributes( ).
ENDMETHOD.
METHOD
"lif_partners~display_partner
constructor.
name = im_name.
set handler add_vehicle for all instances.
UDLVH HYHQW OLIBSDUWQHUVaSDUWQHUBFUHDWHG
ENDMETHOD.
METHOD
"constructor
add_vehicle.
SAP AG
"add_vehicle
BC401
11-31
METHOD
display_attributes.
"display_attributes
ENDCLASS.
"lcl_rental IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_travel_agency DEFINITION.
PUBLIC SECTION.
"------------------METHODS:
0(7+2'6
METHODS
display_agency_partners.
PRIVATE SECTION.
"------------------DATA: name TYPE string,
partner_list TYPE TABLE OF REF TO lif_partners.
ENDCLASS.
"lcl_travel_agency DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_travel_agency IMPLEMENTATION.
METHOD display_agency_partners.
DATA: r_partner TYPE REF TO lif_partners.
WRITE: icon_dependents AS ICON, name.
WRITE:
SAP AG
"display_agency_partners
BC401
11-32
METHOD
constructor.
name = im_name.
VHW KDQGOHU DGGBSDUWQHU IRU DOO LQVWDQFHV
ENDMETHOD.
METHOD
"constructor
add_partner.
SAP AG
"add_partner
"lcl_travel_agency IMPLEMENTATION
BC401
11-33
SAP AG 2002
SAP AG
BC401
12-1
SAP AG 2002
SAP AG
BC401
12-2
SAP AG
BC401
12-3
SAP AG 2002
s$7x
y
H PQ@
R
6 S6 A F9@ E DH 6 B 89@ 876 5
H G
I6 C A C B A 6
h
U fc e g
fe d b ` Y U
X c X aX WVT
%# !
$ "
0(
& 2 % 1)&'! !
3
4
4 !
g e d
k ijhfQQu Q s FD
n m f l
n p po sd $ k F
r
o k Q q
n k o s
oe k q
k $t
u
| z
} |Iz{ wwy
} I{y xvv
I
I|| ~ ~~ ~~|| ww ww
~ a||
fW eb c i
w v t
WX U b c 1h ue b e i
qp
r
sX e U c g be e i
&ODVV %XLOGHU
&ODVV %XLOGHU
:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
$SSOLHG H[DPSOH
$SSOLHG H[DPSOH
SAP AG 2002
SAP AG
BC401
12-4
3URJUDPP
3URJUDPP
5(3257 SURJ
5(3257 SURJ
'$7$ UBDLUSODQH
7<3( 5() 72 OFOBDLUSODQH
...
(1'&/$66
...
...
/RFDO FODVVHV DUH RQO\ YLVLEOH LQ WKH SURJUDP WKH\ ZHUH GHILQHG LQ
SAP AG 2002
Local classes and interfaces are only known within the program in which they are defined and
implemented.
Local classes and interfaces are not stored in the Repository (no TADIR entry). There is no "global"
access to these classes or interfaces (for example, from other programs).
SAP AG
BC401
12-5
l
l
a$j
SAP AG 2002
Unlike local program classes and interfaces, global classes and interfaces can be created and
implemented using the ABAP Workbench tool &ODVV %XLOGHU. These classes and interfaces are then
available active throughout the whole system.
SAP AG
BC401
12-6
Utilities
Environment
MIME Repository
Repository Browser
Repository Information System
Tag Library
Transport Organizer
'aQ
a1{ua'1
1s77
s$
Object Name
System
Help
,QVWDQFH FUHDWLRQ
SULYDWH
SURWHFWHG
11{
Edit
$
Workbench
DEVWUDFW
Description
)
s s $a h
1s7D
)LQDO FODVV
2QO\ PRGHOHG
SAP AG 2002
You can also display the Class Builders global classes in the Object Navigator.
Being able to view classes through the navigation window on the left is especially useful when
developing code.
Here, you can also create a new class. Proceed as you would when creating a program: Use the
context menu for a package node or directly for a class node within a package.
Alternatively, you can also create a new class by entering a class name and then choosing 'LVSOD\. In
this case, you must specify a package.
I dialog box asks you to make further specifications for the new class:
Instance creation: Where are instances of this new class to be created?
For example, if you specify SULYDWH the class could only be instantiated within the class itself.
The default setting is SXEOLF.
Exception class (this will be discussed in more detail later, in the context of exception handling)
Persistent class (this will be discussed in more detail later, in the context of special techniques)
Final class (represents the end of an inheritance tree)
Only modeled (class definition only for modeling)
SAP AG
BC401
12-7
SAP AG
n
BC401
12-8
In the Class Builder, choosing the $WWULEXWHV tab displays the class attributes and their properties
(instance attribute/static attribute and the visibility area)
SAP AG 2002
VD )hD
h 7
D )7
) j 1V
' & "
$
%F "
h D7Va
h
h
h
h
j
j
j
7 h D
V "
DVF
DVF
Q7
h
Q h 71V"
D
h7V Q$ 1"'
4
j$5 7V
1h
V$V"
232$ !D1 ( 0
3
V j"
$ $F)$V (
)$2VDh ( VD (
{{{u{
Class
Edit
Goto
Environment
Utilities
System
'HILQH FODVV
DWWULEXWHV
Help
SAP AG
BC401
12-9
This task is simplified by the &RQVWUXFWRU function, choosing which automatically creates the
constructor method. You still have to specify the interface and the source code for the method.
Choose 3DUDPHWHU to define the signature for the method the cursor is currently on.
"
h
hDV j$)" V !'# V " '
"
7 r )h V " '
h th7V DV
( 7 % (
"
h th7V DV
hDjV
a
s7
"VFh
8
D $#
E 1BAB9
D C @
$5 47 $
1j7Q (
AD c IDD
hDjV H1b Ya`%W
E e C Y
2B2A aId
E A g QfI AD
iBh`1HGC F
E YDI A V qI
iA a11#`p
$V )$V (
3 3
2'$ s71 ( 0
{ {{u{
Class
Edit
Goto
Utilities
Environment
System
Help
'HILQH FODVV
PHWKRGV
SAP AG
BC401
12-10
All interface methods are then automatically listed under the 0HWKRGV tab.
All interface methods must then be implemented in this server class. In the example, the interface
zif_fly consists of a single method start.
A global interface that was created in the Class Builder can be announced in a server class by
choosing the ,QWHUIDFHV tab.
SAP AG 2002
$QQRXQFH D JOREDO
LQWHUIDFH LQ D JOREDO FODVV
"Dh wD) $
Q7 hD
VQh j"$7
"
( (
7
) 1V V )
E A E R W E V U S Q CI AD
GGGQ Y 'XGAB22TR2BPBHGC F
0HWKRG LPSOHPHQWDWLRQ RI
D JOREDO LQWHUIDFH
( 7 % (
"
h th7V DV
hDjV
s7
'
"VFh
8
D $#
E 1BAB9
D C @
$5 7 $
4
hDjV
AHGb PPW
D c YIDD
2B2A ad
E e C YI
iBgh`1HDGC F
E A QfI A
E YDI A V qI
iA a11#`p
$V )$V (
23'$ s71 ( 0
3
{ {{u{
Class
Edit
Goto
Utilities
Environment
System
Help
SAP AG
n
BC401
12-11
Selecting a method and choosing 3DUDPHWHUV displays a list of all the methods signature parameters.
SAP AG 2002
6LJQDWXUH SDUDPHWHUV RI
PHWKRG
KHUH &216758&725
(
(
7
D#$y$ u h2
'Q& "
$
% "
)7 7$
h
D) Q#8#
( 7 % (
"
7hDjV
EG219
D C A @
4
V 7 $h
iHGb PHW
E AD c YIDD
2B2A P2d
E e C YI
EhGG`BHGC F
A g QfI AD
3 3
B'Q &V1 ( 0
u V s
x h5
V"
7h7V
FQ V7hh7D
E Y I A V qI
iA aD12p
$V )$V (
Goto
System
Environment
Utilities
E A V U S R Q C AD
GB22T2BPIBHGC F
{{{{uaQ
Edit
E A E
h1h YQ R W
Q1DV (
Class
Help
SAP AG
BC401
12-12
When implementing methods, you can also display the signature of the method to aid your work.
In this example (constructor), the signature consists of the import parameters im_make and
im_planetype.
SAP AG 2002
%
$7 hG '&
) Q7 & ) GV
$ w
1DV$ % x
( "6 (
7
7 % V a' )) #
"
$
6 ) #
"
72 h
hDjV
$ u5
w ) 5
"V"
{j {{ a{Qj
Goto
Utilities
Environment
System
"Q7) "
Edit
Method
Help
Edit
Goto
Utilities
Environment
System
Help
$QQRXQFH VXSHUFODVV
{
)$2VDh ( VD (
3 3
V u $ ( 1 ( 0
4
j$5 7V
Q h 71V"
D
{u{
h7V Q V 1
1h
V$V"
$ $F)$V (
V j"
6XSHUFODVV
6XSHUFODVV
6XSHUFODVV
=&/B$,53/$1(B
Description
Instantiation
Final
SAP AG 2002
In the Class Builder, inheritance relationships are defined by choosing the 3URSHUWLHV tab.
Here, for example, is the 6XSHUFODVV function, which you can use to specify the superclass for the
current class.
SAP AG
BC401
12-13
SAP AG
n
BC401
12-14
If the class is completed and activated, you can use the test tool to create an instance of the class and
test method calls of the class.
SAP AG 2002
i f d d yx
QunD hhi
r
Qiq p
o
h "F d
z
i f d
nD dhh$
d l
m hss i k $j
w v u g s
t
Handler
BiudhD udhgG$f1Q#TV
f
e d
q
srG` p
G d
o
i f d d
nD i)$
d
m FhQ lh k j
} }
)%sx%u!&~ u{
} |
sT
Handler
} } ~ }
5 )!w!!&'|%{
Class
Edit
Goto
Utilities
&ODVV %XLOGHU
&ODVV %XLOGHU
:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
$SSOLHG H[DPSOH
$SSOLHG H[DPSOH
SAP AG 2002
SAP AG
BC401
12-15
Edit
Goto
Utilities
Environment
System
Help
u 'Hi
Pattern
r
uih
FQs7
'aQ
a1{ua'1
Name of airplane
Number of planes
Airplane type
CONSTRUCTOR
Displays airplane attributes
Displays number of airplanes
Description
Class Airplane
ih
uh
##%2'G61
#########
Bu#uBuuB#
i f d d
Q#nD uhhV
k
z s n )
Object Name
m )d
MIME Repository
Repository Browser
Repository Information System
Tag Library
Transport Organizer
G " d
o
nD i)$
i f d d
Bi#funD ud G$BQi$
d g f e d
1Bs# $1d Qi$
i j s s e d
gs j s f g
G#Q
sGq
r
2hTGuudnD
i es i f
BnD iG$f
i f d d g
%p6f
i d
SAP AG 2002
You can also display the Class Builders global classes in the Object Navigator.
Being able to view classes through the navigation window on the left is especially useful when
editing source code. You can drag & drop specific components into the right Editor window and
automatically create and insert source code (as a template).
SAP AG
BC401
12-16
SAP AG
n
n
BC401
12-17
6HOHFWLQJ DQG GUDJJLQJ a class name WR WKH ULJKW automatically creates a CREATE OBJECT
statement with the whole interface of the constructor. You still have to add the calls actual
parameters.
'UDJ
SAP AG 2002
Displays airplane attributes
Displays number of airplanes
CONSTRUCTOR
Name of airplane
Number of planes
Airplane type
Class Airplane
Description
TnnThnnTnnThnnTnnTh
e
i QF Q$2
``P```P```P``HH``HH``HH``HH`H`H`H`
e
i QF Q$2
``P```P```P``HH``HH``HH``HH`H`H`H`
Q
Bi#funD ud G$BQi$
d g f e d
1Bs# $1d Qi$
i j s s e d
gs j s f g
G#Q
sGq
r
2hTGuudnD
i es i f
BnD iG$f
i f d d g
%p6f
i d
G " d
o
nD i)$
i f d d
i f d d
Q#nD uhhV
k
z s n )
&5($7( 2%-(&7
r
IhQu
'aQ
a1{ua'1
MIME Repository
m "hd
Object Name
Transport Organizer
Repository Browser
Pattern
') ai
Workbench
Goto
Edit
Utilities
System
Environment
Help
SAP AG
n
n
BC401
12-18
6HOHFWLQJ DQG GUDJJLQJ a method WR WKH ULJKW automatically creates a CALL METHOD statement with
the whole interface of the method. In this case you must also add the calls actual parameters and set
the calls reference variable.
'UDJ
SAP AG 2002
Displays number of airplanes
Displays airplane attributes
CONSTRUCTOR
Name of airplane
Number of planes
Airplane type
&$// 0(7+2'
[[[!GLVSOD\BDWWULEXWHV
si s" % )
d
Description
iGhhGhGhhGhGhhGhGhhGihGhGhhGhGhh
nhBGBTGnTGu2B2i
hihG1ih1GGhiuhG hBi
Gih2uGGhhG
GhhGhGhhGhGhiGhhGhGhhGhGhhGhGhhGihGhGhhGhGhh
Gih2uGGhhG
GhhGhGhhGhGhiGhhGhGhhGhGhhGhGhhGihGhGhhGhGhh
BfnD d 1Q2#TV
i d g f e d
12s $Gd #TV
i j s s e d
1#s
g s j f g
r q
G
i e s i f d
2iGnD
i f d g
1#nD udhG$f
i p d
6%f
0(7+2'
r
IhQu
'aQ
a1{ua'1
Q
o
G "F hd
i f d d
QnD ihV
i f d d
Q#nD uhhV
k
z s n )
MIME Repository
m "hd
Object Name
Transport Organizer
Repository Browser
Pattern
u 'Hi
Goto
Edit
Workbench
Utilities
System
Environment
Help
&ODVV %XLOGHU
&ODVV %XLOGHU
:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
:RUNLQJ ZLWK JOREDO FODVVHV DQG LQWHUIDFHV
$SSOLHG H[DPSOH
$SSOLHG H[DPSOH
SAP AG 2002
SAP AG
BC401
12-19
1Q 1{)1
1w
'
5x
i1
7RROEDU
$$
$$
$$
$$
/+
/+
/+
/+
/+
86'
86'
86'
86'
'(0
'(0
'(0
'(0
'(0
$
$
$
$
$
$
$
SAP AG 2002
An applied example for the use of ABAP Objects is the standard output tool 6$3 *ULG &RQWURO. It is
implemented in ABAP Objects and provides the user with a wide range of functions (for example
sorting data, reorganizing/hiding columns, totalling).
The exact procedure for using the SAP Grid Control will be discussed in the following.
SAP AG
BC401
12-20
6FUHHQ
$UHD
$
! ( " 7 ( 7 i (
$/9
*ULG
&RQWURO
Public
Private
` #V 7 G (
$
$
&XVWRP
&RQWDLQHU
&RQWURO
Public
Private
SAP AG 2002
An SAP container is capable of housing other controls (such as the SAP Grid Control, tree control,
picture control, splitter control, and so on). It manages these controls in a logical collection and
provides a physical area for visualization. Each control "lives" in a container. Because containers are
themselves controls, containers can be nested within one another. The container becomes the SDUHQW
of its control.
Global classes are available for accessing the custom control and the SAP Grid Control. At runtime,
an object each of the class &/B*8,B&86720B&217$,1(5 and the class &/B*8,B$/9B*5,' is
created. These objects contain all necessary information for accessing the controls. For information
on the object types (classes) and the corresponding methods, refer to the online documentation.
SAP AG
BC401
12-21
...
2 iQ oBQ s
o
Public
&217$,1(5B
Private
...
...
FRQWDLQHUBU
r FVm ss B
o
Public
Private
JULGBU!VHWBWDEOHBIRUBILUVWBGLVSOD\
LBVWUXFWXUHBQDPH
6$3/$1(
LWBRXWWDE
LWDEBVDSODQH
...
alv_grid_control
SAP AG 2002
The control is displayed on a screen, the call of which is not shown in this example.
To create the object that communicates with the container control, it is sufficient to include the name
of the container area
&217$,1(5B
on the screen, on which the container area is defined.
To create the object that communicates with the SAP Grid Control, you must include the reference
variable that refers to the object for the custom container. This is because the object for the SAP Grid
Control contains a reference variable that points to the container object as a private attribute. This
relationship tells the object, which container it should be included in.
To display data in a SAP Grid Control, it must be provided in an internal table. Then a method is
called that receives the content of the internal table and its structure. The name of this method id
VHWBWDEOHBIRUBILUVWBGLVSOD\. If, while the program runs, only the content of the internal
table to be displayed changes, it is sufficient to call the method UHIUHVKBWDEOHBGLVSOD\ with
the container area before the the screen is re-sent.
SAP AG
BC401
12-22
SAP AG 2002
SAP AG
BC401
12-23
([HUFLVHV
8QLW *OREDO &ODVVHV DQG ,QWHUIDFHV
7RSLF &UHDWLQJ *OREDO &ODVVHV
0RGHO VROXWLRQ
&/B+27(/
&/B+286(
,)B3$571(56
1-1
1-2
1-3
1-4
1-5
Activate and test your class using the Class Builder test tool.
SAP AG
BC401
12-24
Define the global interface =,)B B3$571(56 with the interface method
GLVSOD\BSDUWQHU.
Implement the interface in the class =&/B B+27(/.
2SWLRQDO
Include the new class in the program with the airlines and car rental companies. Adjust the
sections that refer to the local interface OLIBSDUWQHUV to suit the new global interface.
OFOBWUDYHOBDJHQF\
XVHV
]LIBSDUWQHUV
LPSOHPHQWV
OFOBFDUULHU
OFOBKRWHO
OFOBYHKLFOHV
OFOBDLUSODQH
OFOBFDUBUHQWDO
4 RSWLRQDO
Define an inheritance relationship in the Class Builder. Your hotel is to inherit from the
superclass =&/B B+286(. Tip: Construct the class =&/B B+286( copying the class
=&/B B+27(/ to =&/B B+286( and then making changes to this copy. The house
should only have an attribute QDPH (protected) and the method GLVSOD\BDWWULEXWHV; delete all
superfluous components.
Finally, define the inheritance relationship between the house and the hotel.
SAP AG
BC401
12-25
6ROXWLRQV
8QLW
7RSLF
*&---------------------------------------------------------------------*
*& Report
SAPBC401_CLSS_MAIN_A
*&---------------------------------------------------------------------*
*&
*&
travel agency
*&---------------------------------------------------------------------*
REPORT
sapbc401_clss_main_a.
SAP AG
BC401
12-26
+2/,'$< ,11
SAP AG
BC401
12-27
6SHFLDO 7HFKQLTXHV
&RQWHQWV
l $EVWUDFW DQG ILQDO FODVVHV
l 9LVLELOLW\ RI FRQVWUXFWRUV
l )ULHQGV
l 2EMHFW 6HUYLFHV
SAP AG 2002
SAP AG
BC401
13-1
SAP AG 2002
SAP AG
BC401
13-2
SAP AG
BC401
13-3
SAP AG 2002
& &"$Gx
R
P S
U T
8 8 EB "@A H FGP E8 CD 9@A 8 A 8
P
QI8 F B D B 9
7
h
W fc e g
fY &c CY 5CY $V
e d b a ` XW
%# !
! &$ "
) '
' 3 % 120(!
6
6 ! 45
igSSQ C "$
j hf e d
e k &
m
m o on l j "
q
n j r C p
m j n s
nd j p
j uGt
f
n s n v
&n d j p l n
} | z
5 ~{xyw
5z }} y~| z
fX eb c i
w u s
XY W b c vh te b e i
qp
r
Y e W c &g be e i
6SHFLDO 7HFKQLTXHV
SAP AG 2002
SAP AG
BC401
13-4
$EVWUDFW &ODVVHV
&ODVV FDQQRW
EH LQVWDQWLDWHG
0HWKRG QRW
LPSOHPHQWHG LQ WKLV
FODVV
SAP AG 2002
It is not possible to instantiate objects of an abstract class However, this does not mean that
references to such a class are not useful. On the contrary, they are very useful, since they can (and
must) refer to instances in subclasses of the abstract class at runtime. The CREATE OBJECT
statement is extended in this context. You can specify the class of the instance to be created
explicitly:
CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>.
Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract)
subclasses, for example to define a uniform interface.
Abstract instance methods are used to specify particular interfaces for subclasses, without having to
immediately provide implementation for them. Abstract methods need to be redefined and thereby
implemented in the subclass (here you also need to include the corresponding redefinition statement
in the DEFINITION part of the subclass).
SAP AG
BC401
13-5
)LQDO &ODVVHV
...
...
...
SAP AG 2002
A final class cannot have subclasses, and can protect itself in this way against (uncontrolled)
specialization.
A final method in a class cannot be redefined in a subclass, and can protect itself in this way against
(uncontrolled) redefinition.
Some features:
A final class implicitly only contains final methods. In this case, you cannot enter FINAL
explicitly for these methods.
Classes, on the other hand, that are both abstract and final can be useful: Only static components
can be used.
SAP AG
BC401
13-6
6SHFLDO 7HFKQLTXHV
SAP AG 2002
SAP AG
BC401
13-7
&5($7( 3527(&7('
2QO\ WKH FODVV LWVHOI DQG DOO LWV VXEFODVVHV FDQ FUHDWH LQVWDQFHV RI WKLV
FODVV
&5($7( 35,9$7(
Public
Private
g
{vv~
l 3RVVLEOH XVH
FRQFHSW
OFOBVLQJOHWRQ
SAP AG 2002
If you want to ensure that not every user (client) can instantiate a class, you can use the following
additions to restrict the visibility area of the constructor and hence limit the use of the class. (The
following additions must be preceded by CLASS ... DEFINITION.)
The optional additions CREATE PROTECTED and CREATE PRIVATE, on the other hand,
have the effect that not every user can create instances of a class. In the case of CREATE
PROTECTED, only VXEFODVVHV of the class or the class itself and, in the case of CREATE
PRIVATE, RQO\ WKH FODVV LWVHOI can create instances of the class.
The CREATE PUBLIC addition is implicitly available for every class definition, provided
neither of the other two CREATE additions is used. It defines the default that HYHU\ user can
create instances of a known class.
Therefore, the additions CREATE PROTECTED and CREATE PRIVATE allow you to control
instance creation and, for example, are a prerequisite for the instance management of persistent
objects, for which the uniqueness of objects must be ensured. (Persistent objects will be
discussed in more detail later.)
If it is to be impossible to instantiate a class more than once (for example, because it serves as a data
administrator or data container), you can use the singleton concept. The class is defined with the
addition CREATE PRIVATE and FINAL and instantiated using its static constructor.
A public static component could then make the reference to the class available to an external user.
SAP AG
BC401
13-8
6SHFLDO 7HFKQLTXHV
SAP AG 2002
SAP AG
BC401
13-9
)ULHQGV
l ,Q UDUH FDVHV FODVVHV KDYH WR ZRUN WRJHWKHU FORVHO\ DQG PDNH DOO
WKHLU FRPSRQHQWV LQFOXGLQJ WKH SURWHFWHG DQG SULYDWH RQHV
DYDLODEOH WR HDFK RWKHU
(IILFLHQW GLUHFW DFFHVV WR WKH GDWD RI D FODVV SURYLGLQJ
IULHQGVKLS
0HWKRGV WKDW DFFHVV WKH VDPH GDWD FDQ WKHUHIRUH EH VSUHDG RYHU
VHYHUDO FODVVHV
3DFNDJH FUHDWLRQ VXSSRUW
yv {
{v
"r
iEgiE r 2
tg{
5y
tg{
5
vEy5v E2 {
igttE r Si
GQ5
SAP AG 2002
In rare cases, classes have to work together so closely that they need access to their protected and
private components. To avoid making these components available to all users, there is the concept of
friendship between classes.
A class can provide friendship to other classes and interfaces (and hence all classes that implement
the interface). To do this you use the )5,(1'6 additions to the CLASS statement, in which all
classes and interfaces that are to be provided friendship are listed. Friends are allowed to access the
protected and private components of the class providing the friendship and can always create
instances of this class, regardless of the CREATE addition to the CLASS statement.
Classes that inherit from friends and interfaces that contain a friend as a component interface also
become friends. Therefore, extreme caution is advised when providing friendship. The higher up a
friend is in the inheritance tree, the more subclasses can access all components of a class providing
friendship. However, providing friendship, unlike the attribute of being a friend, is not inherited. A
friend of a superclass is therefore not automatically a friend of its subclasses.
SAP AG
BC401
13-10
6SHFLDO 7HFKQLTXHV
SAP AG 2002
SAP AG
BC401
13-11
3HUVLVWHQFH 6HUYLFH
$%$3 SURJUDP
{ Ev S
{ v
3HUVLVWHQFH VHUYLFH
'DWDEDVH
v E
2 ~y5&E2
SAP AG 2002
The persistence service helps the programmer to work object-oriented with data in relational
databases.
In principle, ABAP programs work with data and objects that exist(s) in the internal session at
runtime. They are transient when the program is stopped. If this data is to be stored programindependently, that is persistently, it must be stored in the database. (You could also use files on
operating system level.)
SAP AG
BC401
13-12
REMHFWV
3HUVLVWHQW &ODVV
...
SAP AG 2002
To use the persistence service for objects, their classes must be created as so-called SHUVLVWHQW
FODVVHV in the Class Builder.
The term SHUVLVWHQW FODVV indicates that the objects of the class and their state are managed by the
persistence service. In ABAP programs, objects of these classes are, for example, not created using
the normal statement CREATE OBJECT, but instead using a method of the persistence service that
ensures the correct initialization.
When creating a persistent class, the Class Builder automatically creates a corresponding class, the
so called FODVV DFWRU or DJHQW, the methods of which are used to manage the objects of the persistent
class.
As well as their unique identity, persistent classes can also contain so-called key attributes, which the
persistence service uses to ensure the uniqueness of the persistent objects contents.
SAP AG
BC401
13-13
&ODVV $JHQW
5
PDQDJHV
...
FDBFDUULHU !DJHQW
75<
UBFDUULHU
UBDJHQW!JHWBSHUVLVWHQW LBFDUULG
FDUUQDPH
UBFDUULHU!JHWBFDUUQDPH
:5,7(
/+
FDUUQDPH
&$7&+ F[BRVBREMHFWBQRWBIRXQG
(1'75<
7KH DJHQW
D VLQJOHWRQ RI WKH FODVV
&$B&$55,(5 DQG
IULHQG RI WKH SHUVLVWHQW
FODVV &/B&$55,(5
/+
SAP AG 2002
For every persistent class cl_persistent, the Class Builder generates two further classes
ca_persistent and cb_persistent. These classes make up the class-specific part of the
persistence service.
ca_persistent is the so-called FODVV DFWRU (or agent), which is used to manage the managed
object of the class cl_persistent, and in which all actual database accesses take place. The
class actor inherits the relevant methods from the abstract superclass cb_persistent. The
programmer can extend the class actor and redefine the methods (especially the database accesses).
The superclass cb_persistent cannot be changed. The class actor is a friend of the managed
class. It has the attribute CREATE PRIVATE and exactly one instance of the class actor is created
when it is first accessed.
The static attribute AGENT is a reference variable with the type of the class ca_persistent.
When the attribute is first accessed in an ABAP program, the static constructor of the class
ca_persistent creates exactly one instance of this class, which points to the attribute AGENT.
This object is part of the persistence service and its methods are used to manage the object of the
persistent class. For each program there is only one object of the class ca_persistent, because
you cannot create objects from outside using CREATE OBJECT.
The class actor manages one or more objects of the relevant persistent class. These objects must have
different keys.
SAP AG
BC401
13-14
SAP AG 2002
SAP AG
BC401
13-15
([HUFLVHV RSWLRQDO
8QLW 6SHFLDO 7HFKQLTXHV
7RSLF 6LQJOHWRQ &ODVVHV
0RGHO VROXWLRQ
&/B6,1*/(721
6$3%&B63&6B0$,1B$
$GYDQFHG
Create the global class =&/B B6,1*/(721 in the Class Builder.
The following must be specified for the class:
1-1
1-2
1-3
The class has a static reference variable UBVLQJOHWRQ that refers to the instantiated
object.
1-4
You must be able to be instantiate the class only once. (Tip: Instantiation should
take place automatically when the class is first accessed.)
The class has a static reference variable UBVLQJOHWRQ that refers to the instantiated
object.
2-2
Verify your blueprint by calling JHWBVLQJOHWRQ several times. (Debug.) When does
instantiation take place and how often?
SAP AG
BC401
13-16
([HUFLVHV RSWLRQDO
8QLW 6SHFLDO 7HFKQLTXHV
0RGHO VROXWLRQ
&/B$*(1&<
6$3%&B63&6B0$,1B%
$GYDQFHG
Use your singleton and the main program from the last exercise.
3-1
3-2
4-1
4-2
SAP AG
BC401
13-17
4-2-3 In the method, the internal table of the class providing the friendship is to be
accessed in a single record access. (READ TABLE...)
If the requested record does not exist, it is sufficient in this example to
display an appropriate message using the WRITE statement.
5
B$*(1&<.
IULHQG
OFOBDJHQF\
FOBVLQJOHWRQ
JHWB
FRQQHFWLRQ
FRQQHFWLRQB
OLVW
SAP AG
BC401
13-18
6ROXWLRQV RSWLRQDO
8QLW
7RSLF
6SHFLDO 7HFKQLTXHV
6LQJOHWRQ &ODVVHV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_spcS_MAIN_A
*&---------------------------------------------------------------------*
*&
*&
*&
*&
*&
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_spcs_main_a.
SAP AG
FOBVLQJOHWRQ !JHWBVLQJOHWRQ
BC401
13-19
6ROXWLRQV RSWLRQDO
8QLW
7RSLF
6SHFLDO 7HFKQLTXHV
*&---------------------------------------------------------------------*
*& Report
SAPBC401_spcS_MAIN_B
*&---------------------------------------------------------------------*
*&
*&
*&
*&
*&
*&
*&
*&
*&---------------------------------------------------------------------*
REPORT
sapbc401_spcs_main_b.
START-OF-SELECTION.
*########################
UBVLQJOH
FOBVLQJOHWRQ !JHWBVLQJOHWRQ
$JHQF\
/+
LPSRUWLQJ H[BFRQQHFWLRQ
UHF
SAP AG
BC401
13-20
([FHSWLRQ +DQGOLQJ
&RQWHQWV
l 3UHGHILQHG H[FHSWLRQV DQG H[FHSWLRQV \RX GHILQH
\RXUVHOI
l 5DLVLQJ KDQGOLQJ DQG SDVVLQJ DORQJ H[FHSWLRQV
SAP AG 2002
SAP AG
BC401
14-1
SAP AG 2002
SAP AG
BC401
14-2
SAP AG
BC401
14-3
SAP AG 2002
y
d d WUx
Q @ G P9
6 R6 B "89 E CDG B6 3789A 7I6 5
G F
H6 C @ A @ 6
h
T fb We g
fe c a Y VT
WX db X `3X WUS
# !
)' &
0( ! ! %$ "
2 & 1 %
4 4 ! 3
g e d
k ijhfPPH P "U
m f l
n
n p po Wd k "
r
o k P q
n k o s
o e k q
k Ut
u
g
o s o d v
o e k q m o w
x
z
y r o
fV ea b i
w s
VX T a b uvh te a e i
p
rX e T b qdg ae e i
~
H
H D333|}{
([FHSWLRQ FODVV
HLWKHU SUHGHILQHG
RU GHILQHG E\ \RX
3(
5$,6( (;&(37,21 7<3( F[BH[FHSWLRQ
>(;3257,1* DWWU
DWWU
@
([FHSWLRQ KDQGOLQJ
([FHSWLRQ UDLVHG
HLWKHU LQ $%$3
VWDWHPHQW RU E\
UXQWLPH V\VWHP
(
SAP AG 2002
We use the term H[FHSWLRQ to refer to a situation that arises while an ABAP program is being
executed, where there is no point in continuing to run the program in the normal way. Since SAP R/3
Basis Release 6.10, exceptions and exception handling is based on the exception classes concept.
This concept includes the functions of the concepts that preceded it but also enhances (and thus
replaces) them.
Class-based exceptions are raised either using the ABAP statement RAISE EXCEPTION or by the
ABAP runtime environment. For example, if the program tries to divide by zero the runtime
environment raises the exception. You can, however, ascertain that this situation has arisen using a
query in the ABAP program. From there, you can then raise an exception yourself.
You can define exception classes yourself, but there is already a range of predefined exception
classes in the system. In an exception situation, an exception is represented by an exception object that is, an instance of an exception class. The attributes of each exception object can contain
information about the error situation.
The use of class-based exceptions is not limited to object-oriented contexts. Class-based exceptions
can be raised and handled in all ABAP processing blocks. In particular, the catchable runtime errors
used until now can be handled as class-based exceptions.
If a class-based exception occurs, the system interrupts the normal program flow and tries to
navigate to a suitable handler. If it cannot find a handler, a runtime error occurs.
SAP AG
BC401
14-4
"
F[BQRBFKHFN
F[BG\QDPLFBFKHFN
F[BV\BDULWKPHWLFBHUURU
F[BVWDWLFBFKHFN
F[BV\BPRYHBFDVWBHUURU
F[BV\BDULWKPHWLFBRYHUIORZ
SAP AG 2002
All exception classes are derived from the one of the classes CX_NO_CHECK,
CX_DYNAMIC_CHECK, or CX_STATIC_CHECK, themselves derived from the common
superclass CX_ROOT. The way in which exception classes are assigned to one of these three paths
in the hierarchy defines how the associated exceptions are passed along. (This will be discussed in
more detail later in this unit.)
All exception classes begin with the prefix CX_. In general, they are defined globally in the ABAP
Workbench &ODVV %XLOGHU. However you can also define local exception classes.
The root class CX_ROOT contains two predefined methods that are inherited by the other classes.
The GET_SOURCE_POSITION method returns the program name, include name (if relevant), and
line number in the source code where the exception occurred. The GET_TEXT method returns an
exception text of a class in the form of a string. You can assign several texts to each class. You can
then specify which text is to used when an exception is raised by passing an identifier to the
IMPORTING parameter TEXTID of the instance constructor.
All exception classes inherit the KERNEL_ERRID attribute from CX_ROOT. This attribute contains
the name of the appropriate runtime error if the exception was raised by the runtime environment such as COMPUTE_INT_ZERODIVIDE if the program tries to divide by zero. If the exception is
not listed, a runtime error occurs.
SAP AG
BC401
14-5
+DQGOLQJ ([FHSWLRQV
75<
&$7&+ F[B
F[B
>,172 UBH[F@
Code whose
exceptions (if
any) are to be
handled
&$7&+ F[B
&/($183
(1'75<
SAP AG 2002
Like all ABAP control structures, TRY-ENDTRY structures can be nested. Thus the TRY block,
CATCH- blocks, and the CLEANUP block in particular can contain complete TRY-ENDTRY
structures themselves.
The TRY block contains the application code that is to handle the exceptions. If an exception occurs
in the TRY block the system searches first for a CATCH statement (which will handle the exception)
in the same TRY-ENDTRY structure and then step by step outwards in all the enclosing TRYENDTRY structures. If it finds one, it navigates to this handler. If it cannot find a handler but the
TRY-ENDTRY structure is in a procedure, it then tries to pass the exception along to the calling
program. (This will be discussed in more detail later.)
A CATCH block contains the exception handler that is executed if a specified exception has
occurred in the TRY block in the same TRY-ENDTRY structure. After the CATCH statement, you
can specify as many exception classes as you wish. In this way, you define an exception handler for
all these exception classes and their subclasses. After an exception occurs, the system searches
through the listed exception handlers in the order specified. It then executes the first exception
handler whose CATCH statement contains the relevant exception class or one of its superclasses.
In some cases, the system cannot find a handler for an exception within a specific TRY-ENDTRY
structure but the exception is handled in a surrounding TRY-ENDTRY structure or passed along to a
calling program. If this occurs, a CLEANUP block is executed before leaving the TRY-ENDTRY
structure.
SAP AG
BC401
14-6
7<3( L
7<3( L
UHVXOW
LQW
LQW
:5,7( UHVXOW
&$7&+ F[BV\BDULWKPHWLFBRYHUIORZ ,172 UBH[F
WH[W
UBH[F!JHWBWH[W
0(66$*( WH[W 7<3(
,
(1'75<
Information
"
SAP AG 2002
In the above calculation, if the value range for data type i is exceeded, the runtime system raises
the exception CX_SY_ARITHMETIC_OVERFLOW. This exception is handled in the
implemented CATCH block.
The object reference to the exception object is stored in the reference variable r_exc. Using
r_exc and the functional method get_text, the handler accesses the exception text for this
exception object and stores in the string variable text.
To display exception texts as messages, the MESSAGE statement has been extended so that you can
use any string:
MESSAGE <string> TYPE <type>.
As well as the message <string> that will be displayed, you must display the message type
<type>, either as a literal or in a field.
If the value range for data type i is not exceeded, no exception is raised and the TRY block is
processed completely. The program then continues executing after the keyword ENDTRY.
The keyword documentation for each keyword lists the exception classes whose exceptions may
occur when the appropriate ABAP statement is executed.
SAP AG
BC401
14-7
SAP AG
BC401
14-8
The above program source code shows the method get_technical_attributes of the class
lcl_airplane, which was implemented in an earlier exercise in this training course. It receives
an airplane type as an import parameter and returns its weight and tank capacity as export
parameters.
The relevant information is read from the database table VDSODQH. If the airplane type passed is not
available in this table (that is, if sy-subrc <> 0), the values 100.000 and 10.000 respectively
are assigned to the export parameters ex_weight and ex_tankcap. We will now change this
behavior: If an airplane type is not entered in the table, an exception that we have defined should be
raised and handled appropriately.
SAP AG 2002
thHt
t3BHt
}HH
3tH3(t
3tHttHt
H
tt(Ht
Ht(HH(H}tjH3
HttH}Bt3ht(
3tUBB3($HH
(BBht(Dt(H
tU
BhU
3HhBtt(
thHt
( (Ht3hH
(
BhHBtH(
(
BHhBtH(D
(thHBHt3 HH
t(H
(B(tt
Goto
Utilities
Environment
MIME Repository
Object Navigator
Repository Infosystem
Tag Library
Transport Organizer
System
SULYDW
=&;B:521*B3/$1(7<3(
SURWHFWHG
&;B67$7,&6B&+(&.
,QKHULWV )URP
DEVWUDFW
&ODVV 7\SH
8VXDO $%$3 &ODVV
([FHSWLRQ &ODVV
3HUVLVWHQW &ODVV
&ODVV
PP TR PH G
e%18Qr6EqD
5pWhgW
!YiSeAf
C)&
( #
Object Name
Help
B @A88564
97
Workbench
Description
XYQeXeW dG
5X U
c SW Sb AYYW UVG
@ @ a` XX
P T PH E
QP!RSQIG6FD
)LQDO &ODVV
2QO\ PRGHOHG
SAP AG 2002
Exceptions are represented by objects that are instances of exception classes. Defining an exception
is thus synonymous with creating an exception class.
Exception classes are generally defined globally. For special exceptions that will only occur within a
single ABAP program however, you can also define local exception classes.
Global exception classes are defined and managed in the Class Builder. When you create a new
class, if you use the correct naming convention (prefix ZCX_) and choose the class type ([FHSWLRQ
&ODVV, the system automatically displays the ([FHSWLRQ %XLOGHU instead of the Class Builder.
The ([FHSWLRQ %XLOGHU offers all the functions you need to create exception classes and generates
specified components that cannot be changed. When you create an exception class, you must also
specify which category of exception it will be - that is, whether it is derived from
CX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK.
SAP AG
BC401
14-9
Edit
Goto
Utilities
Environment
System
Help
x s { z x o v t s q o m k
uiywuhr2pnljigedywtuyu3!ti)ywus
h f s x x v s v s x x vt
}
A}Fr6rrqq
} }
6 A'| IIq6
I!V
}
rr3% rV
|
} }}
AFr ~|
r } V
FV3)q1d% |
| |
IVr
3 qI
IVr
IVr
IVr
IVr
}
FdIIA 6VFF| 6 Vrr
'
AIAAq
}
([FHSWLRQ WH[W
XVHG WR GHVFULEH
H[FHSWLRQ VLWXDWLRQ
LQ PRUH GHWDLO
V
V
V
V
V
rIA
}
rIA
}
rIA
}
}
rIA
}AF
IVr
r
3 ' 3
y 36
~
d
F w~|
} }}
Exception ID
Text
CX_ROOT
An exception occurred
The methods are all inherited from CX_ROOT. You can also add your own methods. The instance
constructor is generated automatically.
You can also define your own attributes, whose contents specify the exception in more detail. The
([FHSWLRQ %XLOGHU ensures that the instance constructor has identically-named IMPORTING
parameters for these attributes.
The exception texts of global classes are defined on the 7H[WV tab of the Exception Builder. They can
contain parameters. To do this, use the elementary attributes of the exception class by enclosing their
name in ampersands (&) in the exception text.
The exception texts of global exception classes are stored in their different translations in the 2SHQ
7H[W 5HSRVLWRU\ OTR). Note that several texts can be assigned to a single class. You assign a text to
an exception using the TEXTID attribute, which contains the globally unique ID of the text object
within an exception object at runtime. The method GET_TEXT then exports this text, replaces any
text parameters with the contents of the relevant attributes as necessary, and returns the text as a
character string.
For each global class, the Exception Builder generates a default text whose name matches the class
name. (The name of this default text cannot be changed.) You need to create names for other texts.
For each text, the Exception Builder generates a static constant that contains the associated ID in the
OTR. You can then specify which text is to used when an exception is raised by passing an identifier
to the IMPORTING parameter TEXTID of the instance constructor. If you do not specify a text, the
default text is used.
SAP AG
BC401
14-10
([DPSOH
LPBW\SH $
t (
3BH( th
tttHHt
HHhHHhHtjH3h
HH h3tH( B3Ht
Hh
BHH
HgP H}tB
H33(B3H
3H H3htHH(3hHHBhth
3Hh 3HhH(
3HhBt3( (B3 hH
Information
}Ht
HHt
"
SAP AG 2002
If the airplane type passed to the method has not been stored in the table VDSODQH, the exception we
defined previously, ]F[BZURQJBSODQHW\SH, is raised. In addition, a TRY-ENTRY control structure is
implemented that is only processed if sy-subrc <> 0.
The TRY block contains the application code that is to handle the exceptions. When the exception is
raised, the IMPORTING parameter SOBW\SH of the instance constructor is filled. (This parameter is
automatically generated by the Exception Builder.) Using this parameter, the program then assigns
the value of the airplane type to the identically-named attribute.
The exception that has been raised is handled in the CATCH block. The reference to the exception
object is stored in the reference variable UBH[F, which was created as a local data object in the
method (TYPE REF TO cx_root).
Since the IMPORTING parameter TEXTID of the instance constructor was not filled when the
exception was raised, the default text generated when the exception class was created is addressed
using the functional method JHWBWH[W. The method GET_TEXT then exports this text, replaces the
text parameter with the contents of the attribute SOBW\SH, and returns the text as a character string.
The returned text is stored in the local data object WH[W, which has the type VWULQJ The text is then
displayed as an information (type ,) message.
SAP AG
BC401
14-11
3
thHBHt
HHHt
H}th (BBthH
Ht
t3B
tH
H
t "V
Y j B Hh eV
YhtQ tuwj
Hhh tthtt
tH(t3e B H(
h(}B H(B
HtBH
HBhHtBh
HBhHtB
hHBH
t
3hHt
3BB
3BBhB }t33
Ht
HdtH3H3(B
tBtt3th
Ht
Ht
)) h
BB
BB (3(t))
H
Ht
Ht3th
BHt
SAP AG 2002
Exceptions that occur in procedures (methods, function modules, or subroutines) do not necessarily
need to be handled there; they can be passed along to the calling program. The calling program can
then handle the exception itself or also pass it along to its own caller, and so on.
The highest levels to which an exception can be passed are processing blocks without local data
areas - that is, event blocks or dialog modules. The exceptions passed along by the called procedures
must be dealt with there, as must any exceptions raised within this processing block itself. Otherwise
a runtime error occurs.
To pass along an exception from a procedure, you generally use the RAISING addition when
defining the procedure interface.
In methods of local classes and subroutines, specify the RAISING addition directly when defining
the procedure (METHODS meth ... RAISING cx_... cx_..., FORM form ... RAISING cx_... cx_...).
After RAISING, list the exception classes whose objects are to passed along.
In methods of global classes, the exception classes whose objects are to be propagated are entered in
the exception table of the method in the Class Builder. Check the ([FHSWLRQ &ODVV field in this
exception table. Similarly, exceptions raised by function modules are passed along by being entered
in the Function Builder.
SAP AG
BC401
14-12
SAP AG
n
BC401
14-13
If the exception is raised in the method JHWBWHFKQLFDOBDWWULEXWHV, the program continues by handling
this exception. That is, the method JHWBWHFKQLFDOBDWWULEXWHV is terminated and the appropriate
CATCH block is processed within the caller. Note in particular that the program no longer executes
the WRITE statements entered in the TRY block DIWHU JHWBWHFKQLFDOBDWWULEXWHV is called.
As in the previous example, the exception we have defined (]F[BZURQJBSODQHW\SH) is raised if the
airplane type passed to the method JHWBWHFKQLFDOBDWWULEXWHV is not stored in the table VDSODQH. Here,
however, the exception is only raised in the method JHWBWHFKQLFDOBDWWULEXWHV, not handled there.
To pass the exception along to the caller of the method, we enter it after the RAISING keyword.
Now, the caller - that is, the method GLVSOD\BDWWULEXWHV - handles the exception. For this purpose, we
have implemented a TRY-ENDTRY control structure in this method. The method
JHWBWHFKQLFDOBDWWULEXWHV is now called in the TRY block of this control structure.
SAP AG 2002
tBBHH
t
tttD 3B3
3HhBt3(
B3ht
tBHH}h
tttHtHt
(Ht(HBH(ttjH
t((H
Ht3BH(t HBHB3h
Bt3}UBB33(DHHH
33HH
t
( Bt(H3
Bt(Dt Bth
BHH tHH3HBhth
H 333HH'B(3td
3B BtBBHHddH(
tHt
B H(ttB3
tHH(tB3
3tUBB3(D
tH
t(3H
Bt(
B}(H3t 3H u
33}(BH
thH
HBhHtBh
3tH
3tBH}D h
3tHt
HHBD h
t(tt3hB3
tHH(HH h
t3h3B
hthBttBHH
ht(tHHBtHH
(Bh3Bt tBt
F[BG\QDPLFBFKHFN
F[BVWDWLFBFKHFN
3V 3t
1
B" Bt
Q 3(
B HYh
B (B dthH (e
BH
F[BQRBFKHFN
B Bt
e t H t3h B t
Y(B (B W(HhHh
(e Bt(( B
h(V
3
Q
B Bt
"( W "t3hY(h
tV B "Hd (3(t
B"Hu(huQ
t B d (B dthh
i HHdB
h(BV
3H
SAP AG 2002
SAP AG
BC401
14-14
SAP AG 2002
SAP AG
BC401
14-15
([HUFLVH
8QLW ([FHSWLRQ +DQGOLQJ
7RSLF
'HILQLQJ UDLVLQJ SURSDJDWLQJ DQG
FDWFKLQJ FODVVEDVHG H[FHSWLRQV
7HPSODWH
=%&B
B5$,6(B75<
6$3%&B(9(6B0$,1B%
0RGHO VROXWLRQ
6$3%&B(;&6B5$,6(B75<
1-1
1-2
Copy your solution to the last exercise from the unit (YHQWV, or the corresponding
model solution 6$3%&B(9(6B0$,1B%, with all their includes. Give them the
new names =%&B B5$,6(B75<, =%&B B5$,6(B75<B&/, and
=%&B B5$,6(B75<B&/.
Define a JOREDO exception class. (We suggest the name
=&;B B,19$/,'B3/$1(7<3(.)
Choose the appropriate superclass so that the system performs a syntax check to
ensure that, after the relevant exception is raised, it is either handled or passed
explicitly along using a RAISING addition.
Add an attribute for the airplane type (suggested name: 3/$1(7<3() and assign
the type 6B3/$1(7<3( to it.
Create a default error message that can be enhanced dynamically to include the airplane
type.
1-3
SAP AG
14-16
1-4
SAP AG
BC401
14-17
6ROXWLRQ
8QLW ([FHSWLRQ +DQGOLQJ
Include
BC401_EXCS_RAISE_TRY_CL2
*&---------------------------------------------------------------------*
...
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
"--------------------------------------------CONSTANTS: pos_1 TYPE i VALUE 30.
METHODS: constructor IMPORTING
im_name
TYPE string
TYPE saplane-planetype
EXPORTING ex_weight
TYPE s_plan_wei
ex_tankcap
RAISING
DATA: name
TYPE s_capacity
cx_bc401_invalid_planetype.
TYPE string,
SAP AG
BC401
14-18
"lcl_airplane DEFINITION
*------------------------------------------------------------------*
*
*------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
...
ENDMETHOD.
"constructor
METHOD display_attributes.
DATA: weight TYPE saplane-weight,
cap TYPE saplane-tankcap,
r_exception TYPE REF TO cx_root,
exc_text TYPE string.
WRITE: / icon_ws_plane AS ICON,
/ Name des Flugzeugs(001), AT pos_1 name,
/ Type of airplane: (002), AT pos_1 planetype.
* handle exception in case of invalid planetype:
75<
get_technical_attributes( EXPORTING im_type = planetype
IMPORTING ex_weight = weight
ex_tankcap = cap ).
WRITE: / Gewicht:(003), weight,
Tankkap:(004), cap.
&$7&+ F[BEFBLQYDOLGBSODQHW\SH ,172 UBH[FHSWLRQ
H[FBWH[W
UBH[FHSWLRQ!JHWBWH[W
(1'75<
ENDMETHOD.
"display_attributes
METHOD display_n_o_airplanes.
...
ENDMETHOD.
"display_n_o_airplanes
METHOD get_technical_attributes.
SELECT SINGLE weight tankcap FROM saplane
INTO (ex_weight, ex_tankcap)
SAP AG
BC401
14-19
LPBW\SH
ENDIF.
ENDMETHOD.
ENDCLASS.
SAP AG
"get_technical_attributes
"lcl_airplane IMPLEMENTATION
BC401
14-20
'\QDPLF 3URJUDPPLQJ
&RQWHQWV
l '\QDPLF DWWULEXWHV
SAP AG 2002
SAP AG
BC401
15-1
SAP AG 2002
SAP AG
BC401
15-2
SAP AG
BC401
15-3
SAP AG 2002
y
d d Vx
R @ G P9
6 S6 BQ "89 E CDG B6 3789A 7I6 5
G F
H6 C @ A @ 6
h
U fb e g
fe c a Y WU
X db X `3X VT
%# !
! $ "
)' &
& 1 % 0(!
4 4 ! 23
h g f d
j iPePt D
m f l k
n
n p po Qd k j "
r
o j s P q
n j o t
oe j q
j Vu
v
g
o t o d w
d o e j q m o
y
{
z r o x
fW ea b i
w s
WX U a b uvh te a e i
p
rQX e U b qdg e a e i
|
n m o r d j u
~
H}}
H ~
'\QDPLF 3URJUDPPLQJ
,QWHJUDWLRQ LQ &RXUVH &RQWHQW
'\QDPLF 3URJUDPPLQJ
SAP AG 2002
SAP AG
BC401
15-4
)LHOG 6\PEROV
),(/'6<0%2/6 IV! 7<3(_/,.( _7<3( $1<`
$66,*1 GDWDREMHFW 72 IV!
81$66,*1 IV!
IV! ,6 $66,*1('
"B0
"B0
"B0
81$66,*1 IVBLQW!
,) IVBLQW! ,6 $66,*1('
:5,7( LQW IVBLQW!
(/6(
:5,7(
ILHOGV\PERO QRW DVVLJQHG
IQD
(1',)
"B0
IVBLQW!
:5,7( LQW IVBLQW!
*HQHULF RU
FRPSOHWH W\SH
VSHFLILFDWLRQ
SAP AG 2002
You declare field symbols using the ),(/'6<0%2/6 IV! statement. Note that the parentheses
(<>) DUH part of the syntax.
Field symbols allow you to access an assigned data object - that is, all the accesses that you make to
the field symbol are made to the data object assigned to it. Field symbols are similar to dereferenced
pointers. Thus, you can only access the content of the data object to which the field symbol points.
(That is, field symbols use value semantics).
You use the ASSIGN statement to assign a data object to the field symbol IV!. If the field
symbol is generically typed (TYPE ANY), it adopts the type of the data object.
By specifying a type for the field symbol, you can ensure that only compatible objects are assigned
to it.
Example:
DATA: date TYPE d VALUE 19991231, time TYPE t.
FIELD-SYMBOLS: <fs_date> TYPE d, <fs_time> TYPE t.
ASSIGN: date TO <fs_date>, time TO <fs_time>.
<fs_time> = <fs_date>. returns a syntax error.
Conversely, using the following construction would deliver a runtime error:FIELD-SYMBOLS:
<fs_date> TYPE ANY, <fs_time> TYPE ANY.
Use the expression <fs> IS ASSIGNED to find out whether the field symbol <fs> is assigned to
a field.
The statement UNASSIGN <fs>. sets the field symbol <fs> so that it points to nothing. The
logical expression <fs> IS ASSIGNED is then false.
SAP AG
BC401
15-5
"s
V$PVD
"s
RSWLRQ H[SOLFLW
),(/'6<0%2/6 IV! 7<3( $1<
$66,*1 V\GDWXP 72 IV! &$67,1* 7<3( VWBGDWH
"s
RSWLRQ LPSOLFLW
),(/'6<0%2/6 IV! 7<3( VWBGDWH
$66,*1 V\GDWXP 72 IV! &$67,1*
SAP AG 2002
If you use the CASTING addition when you assign a data object to a field symbol that has a different
type, you can remove the restrictions of having to use the data objects original type. The access is
then interpreted as though the data object had the data type of the field symbol.
If you use the CASTING TYPE <type> addition when you assign a data object to a field symbol
that has a different type, you can access the data object using the field symbol as if the object had the
type <type>.
In the above example, note that the system field sy-datum is an elementary character-type
component of length .
You can also use type casting dynamically when you assign a data object to a field symbol.
For example:
PARAMETERS tabname TYPE dd02l-tabname.
DATA:
dummy TYPE i,
line(65535) TYPE c.
FIELD-SYMBOLS <fs_wa> TYPE ANY.
ASSIGN line TO <fs_wa> CASTING TYPE (tabname).
You can now access line through <fs_wa> as if this elementary data object had the same type as
the line type of the transparent table passed using tabname. (You define the dummy data object
only to ensure that line has the correct alignment).
SAP AG
BC401
15-6
'\QDPLF 3URJUDPPLQJ
SAP AG 2002
SAP AG
BC401
15-7
&$// 75$16$&7,21
7B&2'(
G\QDPLF
GDWDBREMHFW
7B&2'(
&$// 75$16$&7,21 GDWDBREMHFW
VSDFHV
68%0,7 UHSRUWBQDPH
G\QDPLF
GDWDBREMHFW
5(3257B1$0(
68%0,7 GDWDBREMHFW
l
6(7 3)67$786
67$786B1$0(
(;&/8',1*
)&
)&
G\QDPLF
ILOO LQWHUQDOBWDEOH ZLWK IXQFWLRQ FRGHV
6(7 3)67$786
67$786B1$0(
(;&/8',1* LQWHUQDOBWDEOH
SAP AG 2002
In many ABAP statements, you can pass attributes dynamically. The syntax for doing this differs,
depending on the statement you use:
If you pass the attribute as a literal in the static form of the statement, you can replace the literal with
a variable. If you pass the attribute as an identifier in the static form of the statement, you can replace
the identifier with a variable in parentheses. Make sure there are no spaces between either
parenthesis and the variable. If the attribute is a list, you can replace it with an internal table.
Note: you PXVW use upper case when filling data objects with literals.
For more information on which of these three options you can use with a specific ABAP statement if any - refer to the ABAP documentation for that statement.
In Open SQL statements, you can also specify the logical conditions of the WHERE clause
dynamically at runtime, by entering the ABAP source code in a variable. In the FROM clause of the
SELECT statement, you can specify either individual table names or a dynamic JOIN expression
using a variable. Similarly, you can program the GROUP-BY and HAVING clauses in SELECT
statements. For more information, refer to the keyword documentation.
SAP AG
BC401
15-8
s( ( 3
(s
(" P
ss
7<3(322/ DEDS
(
0 (vD
s(
st
0(7+2'B1$0(
st
7<3(322/6 DEDS
SAP AG 2002
You can call instance and static methods dynamically using parentheses in the syntax, as is normal in
ABAP. Use the PARAMETER-TABLE and EXCEPTION-TABLE additions of the CALL METHOD
statement to pass the actual parameters dynamically.
The parameter table must have the attributes of the ABAP_PARMBIND_TAB table type. The table
has three columns: NAME (for the name of the formal parameter); KIND for the way the parameter
is passed (exporting, importing, changing, or receiving); VALUE with the type REF TO data (for
the value of the actual parameter).
The way the parameter is passed is specified for each formal parameter in the declaration of the
called method. Thus, the content of the KIND column can be initial.
For the value of the actual parameter, the VALUE reference of the line in the table must point to the
data object containing that value. You can use the GET REFERENCE statement to achieve this.
The exception table must have the attributes of the ABAP_EXCPBIND_TAB table type. This table
has two columns: NAME for the name of the exception; VALUE for the value to be assigned to sysubrc. VALUE must be of type i.
SAP AG
BC401
15-9
'\QDPLF 3URJUDPPLQJ
SAP AG 2002
SAP AG
BC401
15-10
'$7$
*HQHULF W\SH
DVVLJQPHQW
'$7$
UHI
$Q\ FRPSOHWHO\
VSHFLILHG W\SH
SAP AG 2002
Data reference variables contain data references - that is, pointers to data objects. You use the
TYPES reftype TYPE REF TO type_name statement to define a reference type to a data
object, where type_name is any completely specified type. You can also use the generic variant
TYPE REF TO data here. You define the data reference variable itself using the DATA statement.
This reference variable is a data object that can contain any data object (TYPE REF TO data) or
a data object of the specified type.
You work with data references using references. That is, when you access a data reference variable
the data reference itself is accessed, so that changes are made to the addresses.
Data references are handled in ABAP like any data object with an elementary data type. This means
that a reference variable can be defined not only as a single field, but also as the smallest indivisible
unit in a complex data object, such as a structure or an internal table.
After it has been defined, the data reference variable is initial - that is, it contains an empty pointer.
For a data reference variable to contain a reference that points to a data object, you must use this
variable to get a reference to a data object that has already been declared (GET REFERENCE OF
dataobject INTO ref). You can also assign an existing data reference from another data
reference variable or generate a data object dynamically using it. (This will be discussed in more
detail later.)
SAP AG
BC401
15-11
SAP AG
BC401
15-12
In this example, the get_make method of the lcl_vehicle class is called dynamically. It has
only one export parameter and no exceptions.
First we define a parameter table ptab with the global table type abap_parmbind_tab from the
type group abap. The associated work area is called wa_ptab
We fill the work area wa_ptab with the associated values. We assign the name of the export
parameter EX_MAKE to the component name. The component value contains the reference to the
corresponding actual parameter gd_make. We also define the data reference variable ref (TYPE
REF TO data) and fill the corresponding reference using GET REFERENCE. The kind
component of the work area need not be filled.
We then insert this work area in the parameter table ptab using the INSERT statement. Finally we
assign the name of the calling method to the data object meth. We then call the method using the
syntax already shown.
After the method call, the actual parameter gd_make contains the passed value.
SAP AG 2002
S R U S
TVTR
D PH D
I G F E
A@
tt'(3B(D
%
8 5 3 0
9(B7&642 t13B%)(
" % "
H(tB #!3
B B (B 3
'
&(#!
" % $ "
!tB
tt 3t3tt
fytx4uvVtush2(4fTdb`PW
p g w eY a q r q p i g a eY c aYX
tCt
3
0
t3B%1
H 3
B
BV H0t%
3
3
3134
Ht
t (BtB3
3 BH(Bt
t (Bt
(B
BHBt
H(t(tB(B(Bt
(
(Btt(HBH
tH4Ht
%
3HB%1
(Bt3
B
tH
$
B3
HB H0%
tH
3 3
(1B4H
(
'$7$
3$5$0(7(56 SDBWDE
7<3( GGWDEQDPH
'$7$
UHIBLWDE 7<3( 5() 72 GDWD
H B
UHI
^ 7<3( 5() 72 W\SHBQDPH _ /,.( 5() 72 GRBQDPH `
&5($7( '$7$ UHI
<RX FDQ DOVR
DVVLJQ D W\SH
G\QDPLFDOO\
'$7$ UHI
7<3( 5() 72 GDWD
All data objects declared in the declaration part of a program using the appropriate statement (such
as DATA) are generated statically and can be addressed from when the first event block is executed.
However, you can also use data reference variables to generate any data object you want dynamically
while the program is executing. You can use either of the above variants to do this.
Both of these variants generate a data object in the internal session of the current ABAP program.
The data reference in the data reference variable ref points to this object after the statement has
been executed. This dynamically generated data object does not have its own name; it can only be
addressed using the data reference variable. If you want to access the content of the data object, you
need to dereference the data reference first.
In the second variant, you specify the data type of the data object you want to generate after the
TYPE addition of the CREATE-DATA statement. In this case, you can specify the data type
dynamically: CREATE DATA ref TYPE (dataobject). You cannot do this in other ABAP
statements. dataobject is the name of a field containing the name of the relevant data type.
When you generate internal tables using the second variant, you must specify the table kind
statically. You can, however, specify the line type either statically or dynamically. You can also
specify the key components either statically or dynamically (as the contents of a table containing the
component names). You can also specify the initial number of table lines statically or dynamically
(as the contents of a variable).
SAP AG
BC401
15-13
7<3( GGWDEQDPH
7<3( 5() 72 GDWD
t 0
3$5$0(7(56 SDBWDE
'$7$
UHIBLWDE
H B
SAP AG 2002
To access the contents of the data object, you need to dereference the data reference first. To access
data objects generated with data reference variables with generic types (TYPE REF TO data),
you can use field symbols: ASSIGN ref->* TO <fs>. This statement assigns the relevant data
object (the one to which the data reference in the reference variable ref points) to the field symbol
<fs>. If the data object i s assigned successfully, sy-subrc is set to 0. If the field symbol is fully
generically typed, it adopts the type of the data object. If the field symbol is partially or completely
typed, the system checks the compatibility of the data types. You can also cast to the assigned data
object.
If the data reference in ref is initial or invalid, it cannot be dereferenced. In that case the field
symbol remains unchanged and sy-subrc is set to 4.
If the data reference variable ref is completely typed (that is, not generically), you can use the
prefix ref! to access the contents of the data object to which ref is pointing. You can write this
expression in any operand position. If the data reference is typed, you can also address the
components of the referenced data object directly and use them in any operand position.
DATA ref TYPE REF TO sflight.
CREATE DATA ref.
ref->fldate = ref->fldate + 5.
WRITE: / ref->seatsmax.
SAP AG
BC401
15-14
This example displays the content of a transparent table. You can make the FROM clause of the
SELECT statement dynamic. For the INTO clause, you will need a data object that has a line type
compatible with that of the table being displayed. Since the name - and thus the line type of the table
is not known until runtime, you should not create the data object until then.
Unlike conventional data objects, you can specify the type of a data object FUHDWHG DW UXQWLPH
dynamically. The TYPE addition of the CREATE DATA statement contains the name of the table, so
that the system creates the appropriate structure.
The statement ASSIGN d_ref->* TO <fs_wa> assigns the data object to the field symbol.
The data type of the table is inherited by the field symbol, so type casting is no longer necessary.
You can now write each data record from the SELECT statement into the compatibly-typed data
object using the field symbol <fs_wa>.
If you knew the component names, you could display the fields directly using WRITE <fs_wa>... .
However, you will not normally know the names of the components, nor how many of them there
are. For this reason, you must display the components using the ASSIGN-COMPONENT variant: The
components of the structure IVBZD are assigned one-by-one to the field symbol IVBFRPS! and
then displayed. When the loop runs out of components, the program reads the next data record.
SAP AG
BC401
15-15
'\QDPLF 3URJUDPPLQJ
SAP AG 2002
SAP AG
BC401
15-16
1XPEHU RI OLQHV LQ
WKH LQWHUQDO WDEOH
1XPEHU RI OLQHV
UHVHUYHG ZKHQ WDEOH
ZDV ILUVW FUHDWHG
7DEOH NLQG
RWKHU YDULDQW
OLQHV LWDE
QRBRIBOLQHV
WDEOHBNLQG
&RQVWDQWV XVHG
WR HYDOXDWH WKH
UHWXUQ YDOXH
7<3(322/ V\GHV
V\GHVBNLQGXQGHILQHG
V\GHVBNLQGVWDQGDUG
V\GHVBNLQGVRUWHG
V\GHVBNLQGKDVKHG
SAP AG 2002
The DESCRIBE TABLE statement allows you to obtain the following information about an internal
table:
l
The number of table lines initially reserved:After OCCURS, you must enter a type L variable
(here: initial_lines)
Since SAP R/3 Basis Release 6.10, there is also a built-in function, lines.
1RWH
You can use the information about the number of lines in an internal table to query at runtime
whether an internal table has any entries in it at all. Alternatively, use the IS INITIAL query. The
generically-typed interface parameters of subroutines, function modules, and methods can use the
information on the table kind.
SAP AG
BC401
15-17
5XOHV
D C VG PG
CD D CP1 U U
D G G
G G CP1 U U
D G G G G
D G
G
t
o kl
y n
uq q dtT wBl
l
x
l
4j m k kqoojn m
z s r
i
v
p n l
j
j
k4&g
j i h
D
F F D D U U D D V4 U U
G C PG 1
D
G G
P D D
H e e D D V4 U U
C G C PG 1
G G
H E G C PG 1
P DD D U U F F S S V4 U U
D E
G G
D Af C VG 4 VG 1
P A9 PC1 U U
D f
G G
AD D U U PC1 U U
D
H D C PG VG 4 VG G1
G G
hj
l u
q
P A G
P 4DT D D A S S GCP1 U U
D
G G
D C
P A G G U U V4 U U
D
G G
G C PG 1
P D A A G C PG 1
H T eC9dd 4V4 U U
D e G G
SAP AG 2002
Since the introduction of ABAP Objects, there is now a system called the RTTI concept (Run Time
Type Information) that you can use to find out type attributes at runtime. It is based on system
classes. The concept includes all ABAP types, and so covers all of the functions of the statements
DESCRIBE FIELD and DESCRIBE TABLE.
There is a description class for each type with special attributes for special type attributes. The class
hierarchy of the description classes corresponds to the hierarchy of the types in the ABAP type
system. In addition, the description classes for complex types, references, classes, and interfaces
have special methods used to specify references to sub-types. Using these methods, you can navigate
through a compound type to all its sub-types.
To obtain a reference to a description object of a type, you must use the static methods of the class
CL_ABAP_TYPEDESCR or the navigation methods of the special description class. The description
objects are then created from one of the subclasses. At runtime, exactly one description object exists
for each type. The attributes of the description object contain information on the attributes of the
type.
SAP AG
BC401
15-18
SAP AG 2002
We can now enhance the example of dynamic type declarations so that the system also displays the
column names of the transparent table in the list.
Since we need the attributes of a structure, we first define a reference to the appropriate description
class. Instances of this class possess a COMPONENTS attribute, which you use to describe the
individual components of the relevant structure. This attribute is an internal table. Therefore you also
need to define a work area with a compatible line type.
The (functional) method call returns the reference to the description instance of the structure. (The
system creates the structure dynamically, which is why it is accessed through a field symbol).
Only the abstract class CL_ABAP_TYPEDESCR contains the method DESCRIBE_BY_DATA. Its
RETURNING parameter is typed as a reference to this superclass. However, since the actual
parameter descr_ref has the type of the subclass CL_ABAP_STRUCTDESCR, we need to assign
the object using a (widening) cast.
You can then access the attributes of the description instance in any form. In this example, the
component names are displayed as the column headers. (We have omitted the formatting options for
the sake of clarity.)
For more information and syntax examples, refer to the online documentation, either under the
keyword 577, or the class CL_ABAP_TYPEDESCR.
SAP AG
BC401
15-19
SAP AG 2002
SAP AG
BC401
15-20
2SWLRQDO
8QLW '\QDPLF 3URJUDPPLQJ
7RSLF &DVWLQJ 7\SHV
3URJUDP
7HPSODWH
1-2
1-3
SAP AG
B&$67,1*
6$3%&B7$%6B352&(66B'$7$
0RGHO VROXWLRQ
1-1
=%&B
6$3%&B'<16B&$67,1*
Copy your solution to the last exercise from the unit 8VLQJ ,QWHUQDO 7DEOHV
=%&B B352&(66B'$7$ or the corresponding model solution
6$3%&B7$%6B352&(66B'$7$ and give it the new name
=%&B B&$67,1*.
Define a structure type (we suggest the name: VWBGDWH) with three components:
Year, month, and day. Assign an appropriate type to each component.
Define a field symbol based on this type (suggested name: IVBGDWH!).
In the /2$'2)352*5$0 event block:
Copy todays date to the input parameter SDBGDWH.
Assign the input parameter to the field symbol.
Now fill the input parameter with the first day of the next month by accessing the
pseudo-components for the year, month, and day, using the field symbol.
BC401
15-21
6FUHHQ ([HUFLVH
8QLW '\QDPLF 3URJUDPPLQJ
7RSLF
'\QDPLF 2SHQ 64/ 6WDWHPHQWV DQG
*HQHUDWLQJ 'DWD 2EMHFWV DW 5XQWLPH
At the conclusion of these exercises, you will be able to:
Generate data objects dynamically
Program dynamic SQL statements
Develop an ABAP program that can be used as an DG KRF data browser.
You should be able to access only the table name on the selection screen.
Delegate filtering by field content and similar functions to an SAP Grid
Control instance, which you should also use to display the data.
The purpose of this program is to make you more familiar with dynamic
programming techniques. (There is already a 'DWD %URZVHU, installed as a
standard tool in the ABAP Workbench.)
=%&B
3URJUDP
7HPSODWH
B&5($7(B'$7$B64/
SAPBC401_DYN7_CREATE_DATA_SQL
0RGHO VROXWLRQ
SAPBC401_DYN6_CREATE_DATA_SQL
2-1
2-2
Get to know the source code and runtime behavior of this program.
Then implement the following concept: At runtime, the program should generate an
internal table whose line type is compatible with the selected transparent table. The
former should then be filled with the complete contents of the latter using a
dynamic SQL statement. The internal table filled in this way should then be passed
to a SAP Grid Control instance.
2-3
Define a reference for the internal table that will be generated (we suggest the name
UHIBLWDE).
2-4
At program runtime, generate the internal table. At this time, it is known which line
type must be used.
The table must be a standard table with a non-unique standard key.
SAP AG
BC401
15-22
2-5
2-6
Before calling the screen, insert an Open SQL statement that copies the complete
contents of the selected transparent table into the internal table using an array fetch.
If an error occurs, make sure the program terminates.
2-7
Generate a SAP Grid Control instance within the PBO module that you have
created, ,1,7B&21752/6B. Again, if an error occurs, make sure the
program terminates.
The relevant class is called &/B*8,B$/9B*5,'.
2-8
Pass the internal table filled in this way to this SAP Grid Control instance.
If an error occurs, make sure the program terminates.
The relevant method is called
6(7B7$%/(B)25B),567B',63/$<.
You need only pass values to two parameters: The line type to
,B6758&785(B1$0( and the name of the internal table to
,7B2877$%.
SAP AG
BC401
15-23
6ROXWLRQ 2SWLRQDO
8QLW '\QDPLF 3URJUDPPLQJ
7RSLF
&DVWLQJ 7\SHV
5(3257 VDSEFBG\QVBFDVWLQJ
TYPE-POOLS col.
TYPES:
BEGIN OF st_flight_c,
...
END OF st_flight_c,
BEGIN OF st_flight,
...
END OF st_flight,
%(*,1 2) VWBGDWH
\HDU
7<3( Q
GD\
7<3( Q
PRQWK 7<3( Q
(1' 2) VWBGDWH
TYPE string,
set_string
TYPE string,
SAP AG
TYPE st_flight.
BC401
15-24
DATA:
it_sets TYPE STANDARD TABLE OF string
WITH NON-UNIQUE DEFAULT KEY
INITIAL SIZE c_number,
it_flights TYPE SORTED TABLE OF st_flight
WITH UNIQUE KEY fldate carrid connid
INITIAL SIZE c_number,
it_doubles TYPE SORTED TABLE OF st_flight
WITH NON-UNIQUE KEY fldate carrid connid
INITIAL SIZE c_number,
it_col_flights TYPE bc401_t_flights_color,
it_col_doubles LIKE it_col_flights,
wa_col_flight LIKE LINE OF it_col_flights.
),(/'6<0%2/6 IVBGDWH! 7<3( VWBGDWH
PARAMETERS:
pa_date LIKE sy-datum,
pa_alv
AS CHECKBOX DEFAULT X.
LOAD-OF-PROGRAM.
SDBGDWH
V\GDWXP
,) IVBGDWH!PRQWK
IVBGDWH!PRQWK
IVBGDWH!PRQWK
IVBGDWH!PRQWK
(/6(
IVBGDWH!\HDU
(1',)
IVBGDWH!\HDU
AT SELECTION-SCREEN.
IF pa_date < sy-datum.
MESSAGE e085(bc401).
ENDIF.
START-OF-SELECTION.
...
SAP AG
BC401
15-25
6ROXWLRQ
8QLW
'\QDPLF 3URJUDPPLQJ
2-1
The copy template displays a selection screen where you can enter the name of a
transparent table. It then shows a screen that displays the contents of this table.
From this screen, you can either navigate back to the selection screen or end the
program.
This runtime behavior is implemented using standard ABAP programming
techniques. A docking container control instance that fills the whole screen has
already been generated and attached to the screen.
REPORT
sapbc401_dyns_create_data_sql.
DATA:
ok_code LIKE sy-ucomm,
popans.
DATA:
ref_docking TYPE REF TO cl_gui_docking_container,
UHIBDOY
7<3( 5() 72 FOBJXLBDOYBJULG
'$7$ UHIBLWDE 7<3( 5() 72 GDWD
),(/'6<0%2/6 IVBLWDE! 7<3( $1< 7$%/(
PARAMETERS pa_tab TYPE dd02l-tabname DEFAULT SPFLI.
SAP AG
BC401
15-26
START-OF-SELECTION.
&5($7( '$7$ UHIBLWDE 7<3( 67$1'$5' 7$%/( 2) SDBWDE
:,7+ 12181,48( '()$8/7 .(<
$66,*1 UHIBLWDE!
72 IVBLWDE!
6(/(&7
)520 SDBWDE
,172 7$%/( IVBLWDE!
,) V\VXEUF !
0(66$*( DUIZ :,7+ WH[WQGW
(1',)
CALL SCREEN 100.
*&--------------------------------------------------------*
*&
Module clear_ok_code OUTPUT
*
*&--------------------------------------------------------*
MODULE clear_ok_code OUTPUT.
CLEAR ok_code.
ENDMODULE.
" clear_ok_code OUTPUT
*&--------------------------------------------------------*
*&
Module STATUS_0100 OUTPUT
*
*&--------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS ST100.
SET TITLEBAR T100.
ENDMODULE.
" STATUS_0100 OUTPUT
*&--------------------------------------------------------*
*&
Module init_controls_0100 OUTPUT
*
*&--------------------------------------------------------*
MODULE init_controls_0100 OUTPUT.
IF ref_docking IS INITIAL.
SAP AG
= DOCK_AT_LEFT
= 2000
= 6
UHIBGRFNLQJ
BC401
15-27
,) V\VXEUF !
0(66$*( DUIZ :,7+ WH[WDHU
(1',)
&$// 0(7+2' UHIBDOY!VHWBWDEOHBIRUBILUVWBGLVSOD\
(;3257,1*
LBVWUXFWXUHBQDPH
SDBWDE
&+$1*,1*
LWBRXWWDE
IVBLWDE!
(;&(37,216
LQYDOLGBSDUDPHWHUBFRPELQDWLRQ
SURJUDPBHUURU
WRRBPDQ\BOLQHV
27+(56
,) V\VXEUF !
0(66$*( DUIZ
(1',)
ENDIF.
ENDMODULE.
" init_controls_0100
OUTPUT
*&--------------------------------------------------------*
*&
Module leave_programm INPUT
*
*&--------------------------------------------------------*
MODULE leave_programm INPUT.
CLEAR popans.
CALL FUNCTION POPUP_TO_CONFIRM_STEP
EXPORTING
textline1
= text-dml
textline2
= text-rcn
titel
= text-cnc
cancel_display =
IMPORTING
answer
= popans.
CASE popans.
WHEN J.
LEAVE PROGRAM.
WHEN N.
CLEAR ok_code.
ENDCASE.
ENDMODULE.
" leave_programm INPUT
SAP AG
BC401
15-28
*&--------------------------------------------------------*
*&
Module user_command_0100
*
*&--------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN BACK.
CLEAR popans.
CALL FUNCTION POPUP_TO_CONFIRM_STEP
EXPORTING
textline1
= text-dml
textline2
= text-rbk
titel
= text-bak
cancel_display =
IMPORTING
answer
= popans.
CASE popans.
WHEN J.
LEAVE TO SCREEN 0.
WHEN N.
ENDCASE.
WHEN OTHERS.
ENDCASE.
SAP AG
BC401
15-29