439
SAP AG 2002 %&$%$32EMHFWV %& %& $%$32EMHFWV $%$32EMHFWV n SAP R/3 n System requirements: SAP R/3, Basis Release 6.10 or later n 2002/Q3 n Material number 5005 4667

003 - Bc401 - Abap Objects

Embed Size (px)

Citation preview

Page 1: 003 - Bc401 - Abap Objects

SAP AG 2002

%&����$%$3�2EMHFWV

%&���%&���$%$3�2EMHFWV$%$3�2EMHFWV

n SAP R/3

n System requirements: SAP R/3, Basis Release 6.10 or later

n 2002/Q3

n Material number 5005 4667

Page 2: 003 - Bc401 - Abap Objects

SAP AG 2002

&RS\ULJKW������6$3�$*��$OO��ULJKWV�UHVHUYHG�1R�SDUW�RI�WKLV�SXEOLFDWLRQ�PD\�EH�UHSURGXFHG�RU�WUDQVPLWWHG�LQDQ\�IRUP�RU�IRU�DQ\�SXUSRVH�ZLWKRXW�WKH�H[SUHVV�SHUPLVVLRQ�RI6$3�$*��7KH�LQIRUPDWLRQ�FRQWDLQHG�KHUHLQ�PD\�EH�FKDQJHGZLWKRXW�SULRU�QRWLFH�

&RS\ULJKW

1RWHV�RQ�7UDGHPDUNV�n Some software products marketed by SAP AG and its distributors contain proprietary software

components of other software vendors.

n Microsoft®, WINDOWS®, NT®, EXCEL®, Word®, PowerPoint®, and SQL Server® are registeredtrademarks of Microsoft Corporation.

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

n ORACLE® is a registered trademark of ORACLE Corporation.

n INFORMIX®-OnLine for SAP and INFORMIX® Dynamic ServerTM are registered trademarksof Informix Software Incorporated.

n UNIX®, X/Open®, OSF/1®, and Motif® are registered trademarks of the Open Group.

n HTML, DHTML, XML, and XHTML are trademarks or registered trademarks of W3C®, WorldWide Web Consortium, Massachusetts Institute of Technology.

n JAVA® is a registered trademark of Sun Microsystems, Inc.

n JAVASCRIPT® is a registered trademark of Sun Microsystems, Inc., used under license fortechnology developed and implemented by Netscape.

n SAP, SAP Logo, R/2, RIVA, R/3, ABAP, SAP ArchiveLink, SAP Business Workflow,WebFlow, SAP EarlyWatch, BAPI, SAPPHIRE, Management Cockpit, mySAP.com Logo, andmySAP.com are trademarks or registered trademarks of SAP AG in Germany and several othercountries all over the world. All other products mentioned are trademarks or registered trademarks oftheir respective companies.

Page 3: 003 - Bc401 - Abap Objects

SAP AG 2002

&RXUVH�3UHUHTXLVLWHV

l 3URJUDPPLQJ�H[SHULHQFHl 6$37(&

�P\6$3�7HFKQRORJ\�6ROXWLRQ�2YHUYLHZ�l %&���

�$%$3�:RUNEHQFK��)RXQGDWLRQV�DQG�&RQFHSWV�l 3URJUDPPLQJ�H[SHULHQFH�LQ�$%$3

Page 4: 003 - Bc401 - Abap Objects

SAP AG 2002

l 3DUWLFLSDQWV��'HYHORSHUV��FRQVXOWDQWVl 'XUDWLRQ����GD\V

7DUJHW�*URXS

1RWHV�WR�WKH�XVHUn The training materials are QRW�DSSURSULDWH�IRU�VHOI�VWXG\. 7KH\�FRPSOHPHQW�WKH�FRXUVH

LQVWUXFWRUV�H[SODQDWLRQV. There is space for you to write down additional information on thesheets.

n There may not be enough time during the course to complete all the exercises. The exercises areintended as additional examples of the topics discussed during the course. Participants can also usethem as an aid to enhancing their knowledge after the course has finished.

Page 5: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-1

SAP AG 2002

l &RXUVH�JRDOl &RXUVH�REMHFWLYHVl &RXUVH�FRQWHQWl &RXUVH�RYHUYLHZ�GLDJUDPl 0DLQ�EXVLQHVV�VFHQDULR

&RQWHQWV�

&RXUVH�2YHUYLHZ

Page 6: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-2

SAP AG 2002

7KLV�FRXUVH�ZLOO�HQDEOH�\RX�WR�l /HDUQ�WKH�SULQFLSOHV�RI�REMHFW�RULHQWHG

SURJUDPPLQJl /HDUQ�WKH�VWUXFWXUH�DQG�DSSOLFDWLRQ�RI

$%$3�2EMHFWV

&RXUVH�*RDO

Page 7: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-3

SAP AG 2002

$W�WKH�FRQFOXVLRQ�RI�WKLV�FRXUVH��\RX�ZLOO�EH�DEOH�WR�

l 'HVFULEH�DQG�XVH�WKH�PRVW�LPSRUWDQW�SULQFLSOHV�n ,QWHUQDO�WDEOHVn &ODVVHV�LQKHULWDQFH�LQWHUIDFHVn 3RO\PRUSKLVP��LQKHULWDQFH��DQG�LQWHUIDFHVn (YHQWVn ([FHSWLRQ�KDQGOLQJn '\QDPLF�SURJUDPPLQJ

l 'HYHORS�SURJUDPV�LQ�$%$3�2EMHFWV

&RXUVH�2EMHFWLYHV

Page 8: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-4

SAP AG 2002

Unit 1 &RXUVH�2YHUYLHZUnit 2 'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLOUnit 3 8VLQJ�,QWHUQDO�7DEOHVUnit 4 &DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWDUnit 5 ,QWURGXFWLRQ�WR�2EMHFW�2ULHQWHG�3URJUDPPLQJUnit 6 $QDO\VLV�DQG�'HVLJQUnit 7 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJUnit 8 ,QKHULWDQFHUnit 9 &DVWLQJUnit 10 ,QWHUIDFHV�

3UHIDFH

&RXUVH�&RQWHQW����

Page 9: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-5

SAP AG 2002

Unit 11 (YHQWVUnit 12 *OREDO�&ODVVHV�DQG�,QWHUIDFHVUnit 13 6SHFLDO�7HFKQLTXHVUnit 14 ([FHSWLRQ�+DQGOLQJUnit 15 '\QDPLF�3URJUDPPLQJ

$SSHQGL[

&RXUVH�&RQWHQW����

Page 10: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-6

SAP AG 2002

&RXUVH�2YHUYLHZ��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������������ � � ��� � ��� � � � � �

Page 11: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-7

SAP AG 2002

l $�WUDYHO�DJHQW�PDLQWDLQV�LWV�FRQQHFWLRQV�WREXVLQHVV�SDUWQHUV���VXFK�DV�DLUOLQHV��FDU�KLUHFRPSDQLHV��DQG�KRWHOV

0DLQ�%XVLQHVV�6FHQDULR

Page 12: 003 - Bc401 - Abap Objects

© SAP AG BC401 1-8

SAP AG 2002

7HFKQLFDO�1DPHV

l 3DFNDJH��%&���l 1DPLQJ�FRQYHQWLRQV�IRU�SURJUDP�REMHFWV�

n 'HPRQVWUDWLRQV� 6$3%&���B[[['B���n &RS\�WHPSODWHV� 6$3%&���B[[[7B���n 0RGHO�VROXWLRQV� 6$3%&���B[[[6B���[[[�LV�WKH�LQGLYLGXDO�XQLW�FRGH

n Unit codes:Unit 2: DTOUnit 3: TABUnit 4: CALUnit 7: AIRUnit 8: INHUnit 9: CASUnit 10: INTUnit 11: EVEUnit 12: CLSUnit 13: SPCUnit 14: EXCUnit 15: DYN

Page 13: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-1

SAP AG 2002

l 'DWD�REMHFWV�LQ�SURJUDPVl (OHPHQWDU\�GDWD�REMHFWVl 6WUXFWXUHVl &KDUDFWHU�VWULQJ�SURFHVVLQJ�LQ�8QLFRGH

&RQWHQWV�

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO

Page 14: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-2

SAP AG 2002

l 8VH�HOHPHQWDU\�GDWD�REMHFWV�DQG�VWUXFWXUHVDSSURSULDWHO\

l 8VH�QXPHULF�GDWD�W\SHV�DSSURSULDWHO\l 8VH�DXWRPDWLF�W\SH�FRQYHUVLRQV�DSSURSULDWHO\l 7DNH�WKH�VSHFLDO�IHDWXUHV�RI��FKDUDFWHU�VWULQJ

SURFHVVLQJ�LQ�8QLFRGH�LQWR�DFFRXQW

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO��8QLW�2EMHFWLYHV

Page 15: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-3

SAP AG 2002

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO�,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

����� ������� � �������� � ���������� � ��� �� � � � ���� � � � ��� � � ������ � ��� � � � � ��� �� � � � � �����! #"!$�%'& $ &!( $*)

Page 16: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-4

SAP AG 2002

6SHFLDO�)HDWXUHV�LQ�8QLFRGH6SHFLDO�)HDWXUHV�LQ�8QLFRGH

(OHPHQWDU\�'DWD�2EMHFWV(OHPHQWDU\�'DWD�2EMHFWV

6WUXFWXUHV6WUXFWXUHV

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO����

,QWURGXFWLRQ,QWURGXFWLRQ

Page 17: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-5

SAP AG 2002

5HYLVLRQ��'HILQLQJ�'DWD�2EMHFWV

3UHGHILQHG�$%$3�W\SHV

[

/RFDO�W\SHV

LIVWULQJ[VWULQJ

WG

*OREDO�W\SHV

FQS

7<3(6�W\SHBQDPH�7<3(����

'$7$�GRBQDPH�7<3(�W\SHBQDPH�'$7$'$7$

'$7$�GRBQDPHBQHZ�/,.(�GRBQDPH�'$7$'$7$

n Data objects are usually defined with the '$7$�statement as follows. After the name of the dataobject, a a fully-specified type is assigned to it using the 7<3( addition. The type is linked to thedata object statically and cannot be changed at runtime.

n There are other syntax variants available (for historical reasons). Note however, that some of thesehistorical variants are no longer supported in ABAP Objects. For further information, refer to thekeyword documentation for the '$7$ statement.

n All types are based on predefined ABAP types, which will be discussed in greater detail in thefollowing slide. Some of these ABAP types are fully specified and can be used to type data objectsdirectly. Other types need to be modified to include the length, and in some cases the number ofdecimal places so that they are fully specified. These are the simplest examples of user-definedtypes.

n You can create complex structured types of any level of complexity, based on elementary types.

n You can also define types centrally in the $%$3�'LFWLRQDU\� You can then use these JOREDO�W\SHV inall appropriate Repository objects in the system.

Page 18: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-6

SAP AG 2002

VWULQJ

3UHGHILQHG�$%$3�7\SHV

S

LI

QFGW

[

[VWULQJ

'DWDW\SH1XPHULF

���������

,QWHJHU)ORDW��SRLQW�QXPEHU3DFNHG�QXPEHU

&KDUDFWHU�VWULQJ�W\SH���KH[DGHFLPDO

)L[HG

�OHQJ

WK'HVFULSWLRQ /HQJWK�LQ�E\WHV

1XPEHU�VHTXHQFH&KDUDFWHU�VHTXHQFH'DWH7LPH

+H[DGHFLPDO�FRGH

&KDUDFWHU�VHTXHQFH+H[DGHFLPDO�FRGH

������������������������

�����������

'LIIHU�LQ�•�5XOHV�IRU�VWRUDJH•�9DOXH�UDQJH•�$ULWKPHWLF�XVHG

%LW�RSHUDWLRQV

&KDUDFWHU�VWULQJ�W\SH

+H[DGHFLPDO

5XQWLPH�V\VWHP�DGMXVWV�OHQJWK�G\QDPLFDOO\

&KDUDFWHU�VWULQJ�RSHUDWLRQV��DOORZHG�IRU�DOO�W\SHV���GDWH�FDOFXODWLRQV��WLPH�FDOFXODWLRQV

$WWULEXWHV

9DULD

EOHOHQ

JWK

n 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 theirlength is adjusted dynamically at runtime. Thus, for these two types, length is not a component of thetype.

n The following ABAP types are not fully specified, so you must include additional information beforeyou 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 lengthof �.

é SYou 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 thedefault length of � bytes (that is, 15 digits) and � decimal places.

n The type also contains information on how the data is stored, what range of values is allowed, whatoperations can be performed on the data, and what sort of arithmetic is used (if the data object issuitable for use in calculations). We will deal with this topic in greater detail later in this unit.

Page 19: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-7

SAP AG 2002

7\SH�*URXSV�LQ�WKH�$%$3�'LFWLRQDU\

7\SH�JURXS�]P\WS7<3(�322/�]P\WS�&2167$176�]P\WSBFRQVWBQDPH����>�7<3(6�]P\WSBW\SHBQDPH������@>�7<3(6�]P\WSBW\SHBQDPH������@���

8VLQJ�D�FRQVWDQW�

7<3(�322/6��]P\WS�>�'$7$�YDU�7<3(�]P\WSBW\SH���@,)�YDU� �]P\WSBFRQVWBQDPH������(1',)�

]P\WS]P\WS]P\WS]P\WS

]P\WS]P\WS]P\WS]P\WS

]P\WS]P\WS

]P\WS]P\WS

1DPH�RI�W\SH�JURXS��8S�WR���FKDUDFWHUV�HDFK�W\SH�QDPH�DQG

FRQVWDQW�QDPHVWDUWV�ZLWK�WKH�QDPH�RI

WKH�W\SH�JURXS��

7<3(�322/67<3(�322/6

(DFK�W\SH�JURXS��ZKRVHW\SHV�DQG�FRQVWDQWV�\RX

ZDQW�WR�XVH��PXVW�EH�VSHFLILHG�LQ�WKH�$%$3�SURJUDP

$%$3�SURJUDP

]P\WS]P\WS

n You must use a type group to define global constants. The name of the type group can contain up to5 characters.

n You define constants in the type group using the &2167$176 statement, just as in an ABAPprogram. 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.

n To be able to use the types of a type group in a program, you must refer to the type group using the7<3(�322/6 statement.After this line in the program, you can then use all the constants in the type group.

n You can also define global data types in a type group. Before SAP R/3 Basis Release 4.5, this wasthe only way to define complex global data types.

Page 20: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-8

SAP AG 2002

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO����

6SHFLDO�)HDWXUHV�LQ�8QLFRGH6SHFLDO�)HDWXUHV�LQ�8QLFRGH

(OHPHQWDU\�'DWD�2EMHFWV(OHPHQWDU\�'DWD�2EMHFWV

6WUXFWXUHV6WUXFWXUHV

,QWURGXFWLRQ,QWURGXFWLRQ

Page 21: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-9

SAP AG 2002

,QWHJHU�DX[LOLDU\�YDULDEOH

LQW�� ���������������LQW�� ��������������������LQW�� ���������

�� � � �

�� �

LQW�

,QWHJHUV�DQG�,QWHJHU�$ULWKPHWLF

�� �',902'

$GGLWLRQ6XEWUDFWLRQ0XOWLSOLFDWLRQ'LYLVLRQ,QWHJHU�GLYLVLRQ5HPDLQGHU�LQWHJHU�GLY�3RZHU

$ULWKPHWLF�RSHUDWLRQV�IRU�LQWHJHUV�

LQW�� �LQW����LQW����LQW��

'$7$��FRXQWHU�7<3(�L�9$/8(��� l /HQJWK����E\WHVl 9DOXH�UDQJH�>�����������������������@

l ,QWHJHU�$ULWKPHWLF� $OO�LQWHUQDO�DX[LOLDU\�ILHOGV�DUH�LQWHJHUV� ,QWHULP�UHVXOWV�DUH�URXQGHG

n 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

n Calculations performed using integer arithmetic are faster than calculations using fixed point orfloating point arithmetic.

n For further information on calculations performed on integers, refer to the keyword documentationfor the &20387( statement.

Page 22: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-10

SAP AG 2002

3DFNHG�1XPEHUV�DQG�)L[HG�3RLQW�$ULWKPHWLF

� � � � � �+-,/. 01.325476589�:<;>=3?�@BAC236

D . 0FE

'$7$��SHUFHQWDJH����7<3(�S�'(&,0$/6���9$/8(������������� '(&,0$/6��'(&,0$/6��

/HQJWK�LQ�E\WHV��Q�E\WHV�FRUUHVSRQG�WR

�Q�����GLJLWV

1XPEHU�RI�GHFLPDO�SODFHV

�����1XPEHU

)L[HG�SRLQW�DULWKPHWLF���OLNH�XVLQJ�SDSHU�DQG�SHQFLO�

l���3HUPLWWHG�OHQJWK����WR����E\WHV�� ���WR����GLJLWV�l���1XPEHU�RI�GHFLPDO�SODFHV���QXPEHU�RI�GLJLWV��PD[����l���([DPSOH��/HQJWK���E\WHV����GHFLPDO�SODFHV������������������ë�9DOXH�UDQJH��>���������������@

,QWHUQDO�UHSUHVHQWDWLRQ

������������������������������������

n The length of packed numbers is given in bytes. The connection between value range and length isderived from the internal representation: Each decimal digit is represented by a half-byte. The lastbyte is reserved for the plus or minus sign.

n The number of decimal places is defined using the '(&,0$/6 addition. The maximum number ofdecimal places is either 15 or the length of the number minus 1 (that is, there must be at least onedigit before the comma).

n In user dialogs, decimal numbers are formatted according to the settings in the user master record.

n Decimal point-aligned fixed point arithmetic is used as default for calculations. Packed numbers arethus well-suited to business calculations, where the correct rounding for the specified number ofdecimal places is very important. The algorithm for this arithmetic is similar to using "pencil andpaper".

n The system always uses packed numbers of maximum length for interim results.

n You can switch off fixed point arithmetic in the program attributes. If you do, the '(&,0$/6addition when defining a packed number only affects the output of the number. Internally, allnumbers are interpreted as integers, regardless of the position of the decimal point. The fixed pointarithmetic option is always selected by default. You should DOZD\V accept this value and use packednumbers for business calculations.

Page 23: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-11

SAP AG 2002

ΛΛ

Λ

Λ

Λ

Λ

Λ

1499023437�0

1494140625,0

1484375,0

140625,0

125,0

20481

10241

1281

641

81

212 121212115,0

21

21215,1

1110763

10

1

=

+=

+=

+=

+=

+++++=

+×+×+×+×+×=

+=

×+×=

-- ---

-

)ORDWLQJ�3RLQW�$ULWKPHWLF

_ _

_ _ _ _ _� � � � �����

2QO\����ELWV�DYDLODEOH

2QO\�IRUDSSUR[LPDWLRQV

n 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 inbinary form. This representation complies with the IEEE norm for double precision floating pointnumbers. The floating point operations of the relevant processors are used for calculations.Since algorithms are converted to binary, inaccuracies can occur. Therefore, floating point numbersare QRW suitable for business calculations.

([DPSOH�You want to calculate 7.72% of 73050 and display the result accurate to two decimal places. Theanswer 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 <RX�VKRXOG�WKHUHIRUH�RQO\�XVH�IORDWLQJ�SRLQW�QXPEHUV�IRU�DSSUR[LPDWLRQV��:KHQ�\RX�FRPSDUHQXPEHUV��DOZD\V�XVH�LQWHUYDOV��DQG�DOZD\V�URXQG�DW�WKH�HQG�RI�\RXU�FDOFXODWLRQV�

n 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 andnegative numbers as well as zero.�In addition, you must use floating point numbers for specialaggregation functions of the 6(/(&7 statement.

Page 24: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-12

SAP AG 2002

6XPPDU\��5HFRPPHQGDWLRQV�IRU�8VLQJ�1XPHULF'DWD�7\SHV

5HTXLUHG�5HTXLUHG� 5HFRPPHQGHG�SUHGHILQHG�$%$3�GDWD�W\SH�5HFRPPHQGHG�SUHGHILQHG�$%$3�GDWD�W\SH�

,QWHJHUV�RQO\ 7\SH�L�VLQFH�FDOFXODWLRQV�XVLQJ�LQWHJHU�DULWKPHWLF�DUH

IDVWHVW

'HFLPDO�QXPEHUV�IRU�EXVLQHVV�FDOFXODWLRQV 7\SH�S

'HFLPDO�QXPEHUV�IRU�URXJK�FDOFXODWLRQV�SHUIRUPHG�RQ�YHU\�VPDOO�RU�YHU\�ODUJH�QXPEHUV

7\SH�)

n 1RWH�

The results of the following functions have the data type I:é Trigonometric functions:�FRV, DFRV,�VLQ, DVLQ, WDQ, DWDQé Hyperbolic functions: WDQK, VLQK, FRVKé Exponential functions (base e): H[Sé Natural logarithms (base e): ORJé Logarithms (base 10): ORJ��é Square root: VTUW

Page 25: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-13

SAP AG 2002

$ULWKPHWLF�DQG�0L[HG�([SUHVVLRQV

FD EU � �S�'(&,0$/6�� L L I

���������(������� ���� �

→��)ORDW��SRLQW�DULWKPHWLF�

↓&RQYHUVLRQ�DIWHU�I�DQG�FDOFXODWLRQ

ó

���������(���������������(����,QWHULP�UHVXOW�

� �����������(����↓

&RQYHUVLRQ�DIWHU��S�'(&,0$/6��

����

l���)L[HG�SRLQW�DULWKPHWLF�LV�WKH�GHIDXOWl���,QWHJHU�DULWKPHWLF�LV�RQO\�XVHG�LI�DOO�WKH�FRPSRQHQWV�DUH�LQWHJHUVl���)ORDWLQJ�SRLQW�DULWKPHWLF�LV�XVHG�LI�DW�OHDVW�RQH�FRPSRQHQW�LV�D�IORDWLQJ�SRLQW�QXPEHUl���$OO�FRPSRQHQWV�DUH�FRQYHUWHG�WR�WKH�UHOHYDQW�GDWD�W\SHl���$IWHU�WKH�FDOFXODWLRQ��DOO�DUH�FRQYHUWHG�WR�WKH�UHVXOW�W\SH

([DPSOH�

n An arithmetic expression may contain any data types that are convertible into each other and into thetype of the result field.

n The system converts all the values into one of the three numeric data types (L, S, or I), depending onthe data types of the operands. The ABAP runtime system contains an arithmetic for each of thethree data types. The system then performs the calculation and converts it into the data type of theresult field.

n This may mean that the same arithmetic expression leads to different results when performed ondifferent combinations of data types.

n It is also possible for an arithmetic expression to have RQO\�FKDUDFWHU�VWULQJ�W\SH data objects, aslong as their contents are appropriate. The values are converted to numeric type objects. Bear inmind that conversions affect performance. Wherever possible, choose a data type that does QRWrequire runtime conversion.

n If an arithmetic expression contains numeric literals, the choice of arithmetic depends on the size ofthe number: If the number is within the value range for the data type L, the numeric literal isinterpreted as an integer. If the value of the literal is greater than 2147483647, it is interpreted as apacked number.([DPSOH�'$7$�LQW�7<3(�L��LQW� �������������������������� ����"result: 9�����������������LQW� �������������������������� ����"result: 10

Page 26: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-14

SAP AG 2002

3UHGHILQHG�$%$3�7\SHV�IRU�&KDUDFWHU�6WULQJV

'HVFULSWLRQ

/HQJWK

9DOXH�UDQJH

&DOFXODWLRQV

)RUPDWWLQJ�RSWLRQV

����������������������FKDUDFWHUVFKDUDFWHUV

�����������FKDUDFWHUV

��GLJLWV<<<<00''

��GLJLWV++0066

'HSHQGV�RQ'HSHQGV�RQ�FRGHSDJH�FRGHSDJH%\�*UHJRULDQFDOHQGDU%\�FORFN

7\SH�7\SH�FF7\SH�Q7\SH�G7\SH�W

7LPH 'DWH 6HTXHQFH�RI�GLJLWV

)L[HG�OHQJWK�)L[HG�OHQJWK�FKDU��VWULQJFKDU��VWULQJ

'DWH�DULWKPHWLF

7LPH�DULWKPHWLF

%DVHG�RQ�XVHU�GHIDXOW�YDOXHV

++�00�66

'LJLWV

9DULDEOH9DULDEOH

7\SH�7\SH�VWULQJVWULQJ&KDU��VWULQJ�&KDU��VWULQJ�RI�YDULDEOH�RI�YDULDEOH�OHQJWKOHQJWK

&RQYHUVLRQ &RQYHUVLRQ &RQYHUVLRQ&RQYHUVLRQ

n The value range of each string depends on the code page, which contains all the supported charactersin form of a table. Internally, each character is represented by a code number. When the systemoutputs the character, it uses the code page to convert this number. To find the code page valid in agiven system, choose 7RROV →�&&06�→�6SRRO�$GPLQLVWUDWLRQ�→�)XOO�$GPLQLVWUDWLRQ�→�&KDUDFWHU6HWV.

n The initial value of each character string with fixed length is a space character.

n Numeric strings are represented internally as character strings. Note, however, that only digits arepermissible as characters. When character strings are assigned to numeric strings, the system ignoresletters and only copies the digits (right-aligned). Missing characters are filled with zeros.

n The initial value of each character in a numeric string is a zero.

n Only sequences of digits are valid for values of type G. These digits form a meaningful date, whichcomplies with the Gregorian calendar. The first four digits represent the year, the next two the monthand the last two the date. For performance reasons however, the object is only checked if it is aninput field on a screen or selection screen.

n The initial value of a date is ������. The formatting options are determined by the user settings.

n For values of type W, a sequence of digits is only valid if it can be interpreted as a time on the 24-hourclock. The rules for interpreting the value are analogous to those used for dates.

n The initial value of a time is ������.

Page 27: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-15

SAP AG 2002

2YHUYLHZ��&KDUDFWHU�6WULQJ�3URFHVVLQJ

5(3/$&(5(3/$&(75$16/$7(75$16/$7(6+,)76+,)7

&21'(16(&21'(16(

),1'),1'

63/,763/,7

&21&$7(1$7(&21&$7(1$7( �

29(5/$<29(5/$<

'HVFULSWLRQ�DQG�QRWHV6HDUFK�LQ�D�FKDUDFWHU�VWULQJ

5HSODFH

0RYH5HPRYH��VSDFH�FKDUDFWHUV2YHUZULWH����6SDFHV�DUH�RYHUZULWWHQ�E\�FKDUDFWHUV�IURP�WKH�VHFRQG�FKDUDFWHU�VWULQJ&RQFDWHQDWH��VHYHUDO�FKDUDFWHU�VWULQJV6SOLW��D�FKDUDFWHU�VWULQJ

$�%�$�3$�%�$�3 D��E��D�S$�%�$�3 %�$�3��

%�%�$�3

$�3�� $�3��

$�% $�%�$�3$�3��

$�%�����3 $�%�$�3

$�% $�3��

$�$�$�$

6HDUFK�LQ�D�FKDUDFWHU�VWULQJ6HDUFK�LQ�D�FKDUDFWHU�VWULQJ

5HSODFH�ILUVW�RFFXUUHQFH5HSODFH�ILUVW�RFFXUUHQFH

0RYH0RYH5HPRYH5HPRYH2YHUZULWH�2YHUZULWH�

&RQFDWHQDWH&RQFDWHQDWH

6SOLW6SOLW

5HSODFH�DOO�RFFXUUHQFHV5HSODFH�DOO�RFFXUUHQFHV

$�%�&�$�3��

$�%�$�3% "GIHKJ�LBM�N'O<P�QRO�JCS�TVUXWZY

3RVLWLRQ�RI�VHDUFK�VWULQJXVLQJ�0$7&+�2))6(7�RII�DGGLWLRQ

n Note for ),1'�statement (search in a character string):There are special comparison operators for strings, which you can use in logical expressions in aquery (,)) to search more flexibly for character sequences in a character string. For moreinformation, see the keyword documentation for ,).

n For every statement, the operands are treated like type F fields, regardless of their actual field type.No internal type conversions take place.

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

n All of the statements apart from 6($5&+ are case-sensitive.

n To find out the occupied length of a string, use the standard function 675/(1���n 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 intowhich the string should be split.

Page 28: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-16

SAP AG 2002

$FFHVVLQJ�3DUWV�RI�)LHOGV

�VWDWHPHQW!��ILHOG!��RII!��OHQ!�����

5(3257����3$5$0(7(56���SDBVWU�����/2:(5�&$6(���SDBSRV�7<3(�L���SDBOHQ�7<3(�L�:5,7(�SDBVWU�SDBSRV�SDBOHQ�� [�\I\<\�[5]<^�[�_�_3`a]I^Ib<\<b/\

Y/Y<Y<Y<Y/Y

3RVVLEOH�ZLWK�DQ\FKDUDFWHU�W\SH�ILHOG

��SDSDBBSRVSRV��SDSDBBOHQOHQ��

n In DQ\ statement that operates on a FKDUDFWHU�W\SH field, you can address part of the field orstructure by specifying a starting position and a number of characters. If the field lengths aredifferent, the system either truncates the target or fills it with initial values. The source and targetfields must have the type [, F, Q, G, W, or 675,1*��You can also use structures.([DPSOH029(�<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>.

Page 29: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-17

SAP AG 2002

6SHFLDO�)HDWXUHV�LQ�8QLFRGH6SHFLDO�)HDWXUHV�LQ�8QLFRGH

(OHPHQWDU\�'DWD�2EMHFWV(OHPHQWDU\�'DWD�2EMHFWV

6WUXFWXUHV6WUXFWXUHV

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO����

,QWURGXFWLRQ,QWURGXFWLRQ

Page 30: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-18

SAP AG 2002

'HILQLQJ�6WUXFWXUHV�ZLWK�/RFDO�7\SHV

7<3(6��%(*,1�2)�VBQDPHBW\SH���SUHQDPH�����7<3(�F���VXUQDPH�����7<3(�F���WLWOH�������7<3(�F��(1'�2)�VBQDPHBW\SH�

'$7$���VBQDPH�7<3(�VBQDPHBW\SH��

�'$7$���%(*,1�2)�VBQDPH����SUHQDPH�����7<3(�F����VXUQDPH�����7<3(�F����WLWOH�������7<3(�F���(1'�2)�VBQDPH�

'$7$�'$7$�

7<3(6�7<3(6�

%(*,1�2)%(*,1�2)

(1'�2)(1'�2)

%(*,1�2)%(*,1�2)

(1'�2)(1'�2)

'$7$�'$7$�

67$57�2)�6(/(&7,21���VBQDPH�SUHQDPH� �6PLWK���VBQDPH�VXUQDPH� �-RKQ�

VBQDPH

VBQDPH

VBQDPH

VBQDPH

$OWHUQDWLYHV

VBQDPHBW\SH

VBQDPHBW\SH

VBQDPH��

�'HILQLWLRQ�RI�D�VWUXFWXUH�W\SH

'HILQLWLRQ�RI�D�VWUXFWXUH�ZLWKUHIHUHQFH�WR�D�XVHU�GHILQHG�W\SH

'HILQLWLRQ�RI�D�VWUXFWXUH�ZLWKLPSOLFLW�W\SH�FRQVWUXFWLRQ

n As with elementary data objects, you can define structures in two ways:

é First, define a structure type explicitly using the 7<3(6 statement.To do this, enter the name of the structure after %(*,1�2) and then list the definitions of all thecomponents. End the definition of the structure type using (1'�2).You then define the structured data object with the '$7$ statement, using your own user-definedstructure type.

é Define the data object directly using the '$7$ statement. The syntax is similar to the definition ofa structure type. If you use this option, the corresponding structure type is defined implicitly at thesame time.

n In both cases, the type is defined locally. Bear in mind that you can also use globally defined typesinstead.

n You address components of structures using:structure_name�comp_name.For this reason, you should avoid using hyphens in variable names.

Page 31: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-19

SAP AG 2002

'HILQLQJ�1HVWHG�6WUXFWXUHV�ZLWK�/RFDO�7\SHV

1HVWHG�VWUXFWXUH1HVWHG�VWUXFWXUH

6WUXFWXUH�W\SH6WUXFWXUH�W\SH

QDPH

VWUHHWFLW\

SUHQDPHVXUQDPHWLWOH

VBDGGUHVV

VBQDPHBW\SHSUHQDPHVXUQDPHWLWOH

7<3(6��%(*,1�2)�VBQDPHBW\SH�����SUHQDPH�����7<3(�F�����VXUQDPH�����7<3(�F�����WLWOH�������7<3(�F��(1'�2)�VBQDPHBW\SH�'$7$��%(*,1�2)�VBDGGUHVV������QDPH�������7<3(�VBQDPHBW\SH������VWUHHW�����7<3(�F������FLW\�������7<3(�F��(1'�2)�VBDGGUHVV�

VBDGGUHVV�QDPH�VXUQDPH� �6PLWK�VBDGGUHVV�FLW\� �/RQGRQ�

VBQDPHBW\SH

�� ��

��

VBQDPHBW\SH

$SSOLFDWLRQ��/RJLFDO�VXEJURXSLQJ�

RI�GDWD

n You can define nested structures by assigning a structure type to a component ZLWKLQ a structure type.

n You can address this substructure as a whole using the component name:structure_name�substructure_name.

n You can also address individual components in the VXEstructure:structure_name-substructure_name-comp_name.

n Structures can be nested to any level you wish.

n You can also integrate components of a dynamic type in a structure. This can either be an elementarydata object of variable length (VWULQJ or [VWULQJ),an internal table, or a reference. Thesestructures are known as GHHS structures.

n There are constraints on how such deep structures can be used. For instance, a deep structure cannotbe used DV�D�ZKROH in the�,172 clause of the 6(/(&7 statement. (Instead, each component must belisted separately). Offset operations are also not appropriate. For more information, refer to SAPNote 176336.

Page 32: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-20

SAP AG 2002

8VLQJ�1DPHG�,QFOXGHV'$7$��%(*,1�2)�WBOLQHW\SH�����SUHQDPH�����7<3(�F�����VXUQDPH�����7<3(�F�����WLWOH�������7<3(�F��(1'�2)�VBQDPHBW\SH�'$7$��%(*,1�2)�VBDGGUHVV�,1&/8'(�6758&785(�VBQDPHBW\SH���������������$6�QDPH�'$7$�����VWUHHW������7<3(�F�����FLW\��������7<3(�F���(1'�2)�VBDGGUHVV�VBDGGUHVV�QDPH�VXUQDPH� �6PLWK� �RU��VBDGGUHVV�VXUQDPH� �6PLWK�

VBQDPHBW\SH

VBQDPHBW\SH,1&/8'(�6758&785(,1&/8'(�6758&785($6$6

6WUXFWXUH�ZLWK�QDPHG�LQFOXGH6WUXFWXUH�ZLWK�QDPHG�LQFOXGH

6WUXFWXUH�W\SH6WUXFWXUH�W\SH

VWUHHWFLW\

SUHQDPHVXUQDPHWLWOH

VBDGGUHVV

VBQDPHBW\SHSUHQDPHVXUQDPHWLWOH

QDPH

�� ��

�� 8VH�/RJLFDO�VXEJURXS�GHVLUHG��EXW�QHVWHG�VWUXFWXUHV�QRW�WHFKQLFDOO\�SRVVLEOH

n In some cases, you cannot use nested structures, for example:

é Because you can only define database tables with flat line types

é In situations where user dialogs with a table-type display can only contain simple structures (suchas in the SAP List Viewer or Table Control)

n 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 inthe substructure using structure_name-name-comp_name or directly usingstructure_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 - youcan 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.

Page 33: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-21

SAP AG 2002

6SHFLDO�)HDWXUHV�LQ�8QLFRGH6SHFLDO�)HDWXUHV�LQ�8QLFRGH

(OHPHQWDU\�'DWD�2EMHFWV(OHPHQWDU\�'DWD�2EMHFWV

6WUXFWXUHV6WUXFWXUHV

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO����

,QWURGXFWLRQ,QWURGXFWLRQ

Page 34: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-22

SAP AG 2002

8QLFRGH��2YHUYLHZ

6ROXWLRQ���8QLFRGHl 21(�FRGHSDJH�WKDW

FRPSULVHV�DOO�NQRZQFKDUDFWHUV

l (YHU\�FKDUDFWHU�LV�JHQHUDOO\GLVSOD\HG�E\�WZR�E\WHV

D

""

"6

D8QLFRGH8QLFRGH

3UHYLRXV�SUREOHPVl 0L[WXUH�RI�GLIIHUHQW�LQFRPSDWLEOH�FKDUDFWHU�VHWV�LQ�RQH�FHQWUDO

V\VWHPl 'DWD�H[FKDQJH�EHWZHHQ�V\VWHPV�ZLWK�LQFRPSDWLEOH�FKDUDFWHU�VHWV

n To be able to work with Unicode, you must have a Unicode-compatible SAP System installed thatitself has a corresponding operating system and database. The ABAP programs must also beUnicode-compatible.

n In Unicode programs, other syntax rules apply than in non-Unicode programs. This is due to thedifference 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 ismade about the internal length of a character. To execute the relevant syntax checks, you must check8QLFRGH�&KHFNV�$FWLYH�under program attributes.

n In a Unicode system, you can only execute programs that have the Unicode flag set. If the Unicodeflag is set for a program, the syntax check and program are executed in accordance with the rulesdescribed in the Unicode online help (irrespective of whether it is a Unicode or a non-Unicodesystem).

n If the Unicode flag is not set, the program can only be executed in a non-Unicode system. For suchprograms, the Unicode-specific changes of syntax and semantics do not apply. However, you canuse all language enhancements introduced in connection with the conversion to Unicode.

Page 35: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-23

SAP AG 2002

&KDUDFWHU�7\SH�DQG�%\WH�7\SH�7\SHV

&KDUDFWHU�W\SH�W\SHV�LQ�8QLFRGH�SURJUDPV

G

W VWULQJ

[VWULQJ

[F Q

&KDUDFWHU�W\SHVWUXFWXUH�W\SHV

'LIIHUHQFH�EHWZHHQ�E\WH�SURFHVVLQJ�DQG�FKDUDFWHU�SURFHVVLQJ

'$7$��FBILHOG����7<3(�F�9$/8(�+8*2�������[BILHOG����7<3(�[�9$/8(�(���%�$��

6+,)7�FBILHOG�%<���3/$&(6�>,1�&+$5$&7(5�02'(@�6+,)7�[BILHOG�%<���3/$&(6�,1�%<7(�02'(�

ced�f�g h�i j k�d�flg h5i jmonqpsr

t<u�v�w�x/v y1z

x�vey{z�|�|�|�|p}r d~d

2QO\�FRPSRQHQWV�ZLWK�WKH�W\SHV�c��n��d��DQG�t

E\WH�W\SHE\WH�W\SH

n In Unicode programs, the following data types are interpreted as character-type:F, Q, G, W, VWULQJ as well as structure types, that directly or in substructures only containcomponents with types F, Q , G and W. In non-Unicode systems, a character of this type is one byteand in Unicode systems, it is as long as corresponds with the length of a character on the relevantplatform. Variables of the types [�and [VWULQJ are described as byte-type.

n The character string processing (for which, previously, all arguments were implicitly interpreted astype F�fields) is separated into string processing for character-type and byte-type arguments.

n 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 aretransferred, this triggers a syntax or runtime error.

n 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 optionaladdition. If the ,1�%<7(�02'(�addition is specified, only ;�fields and ;�strings are permitted asarguments. If arguments of another type are transferred, this triggers a syntax or runtime error.

Page 36: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-24

SAP AG 2002

)XQFWLRQV�DQG�&RPSDULVRQ�2SHUDWRUV

)XQFWLRQV

&KDUDFWHU�W\SH�W\SH

;675/(1

%\WH�W\SH�W\SH

&RPSDULVRQRSHUDWRUV

)XQFWLRQV 675/(1

&2��&$��&6��&3�&1��1$��16��13

%<7(�&2��%<7(�&$�%<7(�&6��%<7(�&1�%<7(�1$��%<7(�16

675/(1

�/���<�����5�{�F�<�����e�C�����K�<�����B���I�C�����B�C�C���B��5�{�F�<�F�X�e�C�����K�<�����B���I�C���*� �/¡

¢/£¤���¥�C�I�K�§¦��¨���¥�C�I�C��¡¡�¡�¡

��©��¥¢I£�¡

�/�1�����«ª<���F¬<­1�<���*�F�®�/�����¤ª�B�{�I�F���¯��°F±B�'²¥± �7�³�B�

�/­�´¤���K�<�µ¬a¡�I­B´·¶¹¸�ºI�B»C�B�B©¼�½ª'�7�C¬I­K�I�¾�¿¡

/HQJWK�LQE\WHV

([DPSOHV�

/HQJWK�LQFKDUDFWHUV

n The operators &2, &1, &$, 1$, &6, 16, &3, 13 are available for comparing the contents of characterstring type data objects (for syntax and semantics, see keyword documentation). As with thestatements for string processing, these operators require single fields of type F��Q, G, W, or VWULQJas arguments. Again, character-type structures are also permitted.

n The ;�variants of the string comparison operators are distinguished from the character string variantsby the %<7(- prefix. For this operation, only ; fields and fields of the type [VWULQJ� are allowedas arguments.

n 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, trailingblanks are not counted.

n The ;675/(1 function is available for the length of byte sequences. For ; strings, ;675/(1returns the current length and for ;�fields, the defined length in bytes, where null bytes at the end offields are counted.

Page 37: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-25

SAP AG 2002

&RPSDWLELOLW\�DQG�&RQYHUVLRQ

l 1R�FRQYHUVLRQ�WDNHV�SODFH�LI�W\SH�FRPSDWLEOH�GDWD�REMHFWV�DUH�DVVLJQHGl ,I�QRQ�W\SH�FRPSDWLEOH�GDWD�REMHFWV�DUH�DVVLJQHG��FRQYHUVLRQ�GRHV�WDNH

SODFH�LI�D�FRQYHUVLRQ�UXOH�LV�GHILQHG

:KHQ�DUH�WZR�W\SHV�FRPSDWLEOH"

7ZR�HOHPHQWDU\�W\SHV�DUH�FRPSDWLEOH�ZKHQ�WKH\KDYH�H[DFWO\�WKH�VDPH�W\SH�DQG�OHQJWK��DQG��LQ�WKHFDVH�RI�SDFNHG�QXPEHUV��WKH�VDPH�QXPEHU�RIGHFLPDO�SODFHV���7ZR�VWUXFWXUH�W\SHV�DUH�FRPSDWLEOH�ZKHQ�WKH\�KDYH�WKHVDPH�VWUXFWXUH�DQG�WKHLU�FRPSRQHQWV�DUH����������������������

7ZR�WDEOH�W\SHV�DUH�FRPSDWLEOH�LI�WKHLU�URZ�W\SHV�DUHFRPSDWLEOH�DQG�WKHLU��NH\�GHILQLWLRQV�DQG�WDEOH�W\SHVDUH�WKH�VDPH�

W\SHW\SH OHQJWKOHQJWK

GHFLPDO�SODFHVGHFLPDO�SODFHV

VDPH�VWUXFWXUHVDPH�VWUXFWXUH FRPSRQHQWV�DUH�FRPSDWLEOHFRPSRQHQWV�DUH�FRPSDWLEOH

URZ�W\SHVURZ�W\SHVNH\�GHILQLWLRQVNH\�GHILQLWLRQV WDEOH�W\SHVWDEOH�W\SHV

DUH�WKH�VDPH�DUH�WKH�VDPH�

À����1�ÂÁ·�B�ÄÃÅ¡Ã�¶¤Á ¡

FRPSDWLEOHFRPSDWLEOHFRPSDWLEOHFRPSDWLEOH

n If two data types are not compatible but there is a conversion rule, the system converts the sourceobject into the type of the target object when you assign values, perform calculations, or comparevalues.

n For a full list of all conversion rules, refer to the ABAP syntax documentation for the 029(statement.

n If there is no conversion rule defined for a particular assignment, the way in which the system reactsdepends on the program context.

n 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!�

n In the case of G\QDPLF�typing a UXQWLPH�HUURU occurs, because the field symbols are not assignedtypes 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)

Page 38: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-26

SAP AG 2002

&RQYHUVLRQ�8QGHU�8QLFRGH����

&RQYHUVLRQ�EHWZHHQ�IODW�VWUXFWXUHV

O _ T�J/U ]

À{�'���ÇÆBÈBÉIÊK�C�§���ËÆBÈBÉIÊK����¡

'HFLVLYH�FULWHULRQ�)UDJPHQW�YLHZ

ÌÍCÎÏu�ÐÑ ÎRu ÐÑ Î Ò�Ð Ñ Î>u Ð Ñ Î Ò�Ð

O _ T�J/U [Ñ ÎÏÓ�Ð Ô�Î Ò�ÐÕÍ�Î>u�Ð Ô�Î#Ò�Ð Ì Ö<ÎÏ×�Ð

O _ T�J/U [

O _ T�J/U ]

Ö<ÎÏ×�ÐÌÑ Î Ò�ÐÍCÎÏu�ÐÑ Î3wØ|�Ð

&RQYHUVLRQ�EHWZHHQ�IODW�VWUXFWXUHV�LV�SRVVLEOH�LI�WKH�IUDJPHQWV�RIWKH�VRXUFH�DQG�WDUJHW�VWUXFWXUHV�PDWFK�ERWK�LQ�W\SH�DQG�OHQJWK�LQWKH�OHQJWK�RI�WKH�VKRUWHU�VWUXFWXUH�

8QGHU�ZKDW�FRQGLWLRQLV�WKLV�DVVLJQPHQWSRVVLEOH"

$VVLJQPHQW�LV�SRVVLEOH�⇐

n For some data types there are, for technical reasons, specific alignment requirements that depend onthe individual platforms. (In the memory, fields of this type must begin and end on specificaddresses - for example, a memory address divisible by four.)

n Within a structure, the runtime system, if necessary, inserts bytes before or after these componentswith alignment requirements to achieve the alignment needed. These bytes are known as$OLJQPHQW.

n To check whether the conversion is even permitted, the system first creates the Unicode fragmentview of the structures by grouping adjacent components and alignment gaps (one group each forcharacter-type types [F, Q, G, W], byte-type types, and types L, I, and S).

n Adjacent character-type components of a structure are therefore only grouped together if there areno alignment gaps between these components. Adjacent byte-type components are grouped in thesame way.

n If the fragments of the source and initial structures match the type and length in the length of theshorter structure, the conversion is allowed.

n If the target structure is longer than the source structure, the character-type components of theremainder are filled with space characters. All other components in the remainder are filled with thetype-specific initial value, alignment gaps are filled with null bytes.

Page 39: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-27

SAP AG 2002

&RQYHUVLRQ�8QGHU�8QLFRGH����

&RQYHUVLRQ�EHWZHHQ�VWUXFWXUHV�DQG�VLQJOH�ILHOGV

O _ T�J/U

À{�'���Â���¥�C¬I­F�B�·�B�ËÆ�È/É/ÊF��¡

ÍCÎÏu�ÐÔBÎÏÒ�ÐÑ ÎÏÓ�Ð Ñ Î Ò�Ð

U�ÙBÚ*Û ÜCÝ MÑ Î>v Ð

U�ÙBÚ*Û Ü�Ý M

O _ T*J/U

8QGHU�ZKDW�FRQGLWLRQLV�WKLV�DVVLJQPHQWSRVVLEOH"

$VVLJQPHQW�LV�SRVVLEOH�⇐

'HFLVLYH�FULWHULRQ�)UDJPHQW�YLHZ

&RQYHUVLRQ�EHWZHHQ�VWUXFWXUHV�DQG�VLQJOH�ILHOGV�LV�SRVVLEOH�LIWKH�VWUXFWXUH�EHJLQV�ZLWK�D�FKDUDFWHU�W\SH�IUDJPHQW�DQG�WKLVIUDJPHQW�LV�DW�OHDVW�DV�ORQJ�DV�WKH�VLQJOH�ILHOG�

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 isn’t completely character-type, the conversion is onlyallowed if the structure begins with a character-type group and this group is at least as long as thesingle field. The conversion then takes place between the first character-type group of the structureand the single field. If the structure is the target field, the character-type parts of the remainder arefilled 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 nottype F.

Internal tables can be converted if their row type can be converted.

Page 40: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-28

SAP AG 2002

([DPSOH�RI�8VLQJ�WKH�&RQYHUVLRQ�5XOHV

3$5$0(7(56�SBGDWH�/,.(�V\�GDWXP�'$7$��%(*,1�2)�VBGDWH���������\HDU�����7<3(�Q���������PRQWK����7<3(�Q���������GD\������7<3(�Q�������(1'�2)�VBGDWH�029(�SBGDWH�72�VBGDWH�VBGDWH�GD\� ����029(�VBGDWH�72�SBGDWH�

Þ Ù�MCß _ Ü O5ÙCM�ß _ Ü

&KDUDFWHU�W\SHVLQJOH�ILHOG

&KDUDFWHU�W\SHVWUXFWXUH

l 6LQFH�WKH�VWUXFWXUH�VBGDWH�LV�SXUHO\�FKDUDFWHU�W\SH��LW�LV�WUHDWHG�OLNHD�W\SH�F�GDWD�REMHFW�GXULQJ�FRQYHUVLRQ�

l 7KLV�PHDQV�LW�LV�WUDQVIHUUHG�OHIW�DOLJQHG��FKDUDFWHU�E\�FKDUDFWHU�

n VBGDWH�is structured in such a way that the first four characters of a variable specify the year, thenext two specify the month, and the last two the day. If a date is assigned to this structure using the029( 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.

Page 41: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-29

SAP AG 2002

2IIVHW�DQG�/HQJWK�$FFHVVHV�DQG�(OHPHQWDU\�'DWD2EMHFWV

$FFHVV�WR�VLQJOH�ILHOGV�VSHFLI\LQJ�RIIVHW�DQG�OHQJWK

�VWDWHPHQW!��ILHOG!��RII!��OHQ!�����

l )RU�FKDUDFWHU�W\SH�ILHOGV��RIIVHW�DQG�OHQJWK�DUH�LQWHUSUHWHG�FKDUDFWHUE\�FKDUDFWHU�

l ,Q�WKH�FDVH�RI�E\WH�W\SH�ILHOGV��WKH�YDOXHV�IRU�RIIVHW�DQG�OHQJWK�DUHWDNHQ�LQ�E\WHV�

�B�������à�5���F¬<­��<���eá����/�1���Â�������/���â��ã ��²<�7��äå�I�ª����F¬<­����Z�eá����/�1���¤ª§�����/���â���1°1±/��²�±��{�KæB°B£Ká³�/¡

ç{»�¢I�/�X�à���¥�C¬/­K�I��è1���!���7�ª'�¥�C¬/­F�/��èK°-���K�é¡

ê ÙBÚ*Û Ü�Ý M�{°C±��'²¥±����Fæ�°I£�á

U5ÙBÚ*Û Ü�Ý M ���$���%���$���3���

n Offset/length accesses are permitted on character-type single fields, single fields with the typeVWULQJ, and single fields of the types [� and [VWULQJ.

n For character-type fields and type VWULQJ�fields, offset and length are interpreted character bycharacter. Only with types [ and�[WULQJ�are the values for offset and length taken in bytes.

Page 42: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-30

SAP AG 2002

2IIVHW�DQG�/HQJWK�$FFHVVHV�DQG�6WUXFWXUHV

$FFHVVLQJ�VWUXFWXUHV�E\�VSHFLI\LQJ�RIIVHW�DQG�OHQJWK

�VWDWHPHQW!��VWUXFWXUH!��RII!��OHQ!�����

l 6WUXFWXUH�PXVW�VWDUW�ZLWK�D�FKDUDFWHU�W\SH�IUDJPHQW�l $FFHVV�LV�RQO\�DOORZHG�ZLWKLQ�WKH�FKDUDFWHU�W\SH�LQLWLDO�SDUW�l 2IIVHW�DQG�OHQJWK�VSHFLILFDWLRQV�DUH�LQWHUSUHWHG�DV�FKDUDFWHUV�

�B�������ë²��C�1¢5©��K£�ìK­Iì�ÃF­CÉå�È�¬CÈ��I­í��îF� �/�����Â�³�ï É�­�´CÁ�ì1­í�!�/îK�®�/�����Â�³�Æ'Ê1É/´CÁ�ì1­í�!�/îK�®�/�����Â�³�ÁBðC­ �/�����ˬ���C¬CÈ'ñ¼�¯�KæC� �/�����Â�³�

��©����K£�ìK­IìCÃF­�É�¡�I��»K£/��»�ÀµÊ{Æ�­<�1´FÁ�ì1­¹�1ºC¢5©1�®ìK­IìCÃF­CÉ�èCî �*°FæC�é¡

ò Ü ò SIÜ�TÑ Î>óØÐ Ñ Îôweó�Ð Ñ Î3wØó�Ð}Ì Ñ ÎÏz�|�Ð

õ5ö fÏf Î i h'÷ Ð

n Offset and length accesses to structures are only permitted in Unicode programs if the structure isflat and the offset and length specifications only contain character-type fields from the beginning ofthe structure. This means, the access will cause an error if the offset and length area contains bothcharacter-type and non-character-type components.

n If an offset/length access is allowed in a Unicode program, both offset and length specifications areinterpreted as characters.

n 5HFRPPHQGDWLRQ�Only use offset and length accesses if it is necessary or useful. Bear in mind that processingcomponent by component and using character string operations is generally safer and morereadable.

Page 43: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-31

SAP AG 2002

l 8VH�HOHPHQWDU\�GDWD�REMHFWV�DQG�VWUXFWXUHVDSSURSULDWHO\

l 8VH�QXPHULF�GDWD�W\SHV�DSSURSULDWHO\l 8VH�DXWRPDWLF�W\SH�FRQYHUVLRQV�DSSURSULDWHO\l 7DNH�WKH�VSHFLDO�IHDWXUHV�RI��FKDUDFWHU�VWULQJ

SURFHVVLQJ�LQ�8QLFRGH�LQWR�DFFRXQW

<RX�DUH�QRZ�DEOH�WR�

'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO��6XPPDU\

Page 44: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-32

�([HUFLVH��8QLW� 'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO7RSLF� 'HILQLQJ�'DWD�7\SHV�DQG�'DWD�2EMHFWV

%DVLF�6WDWHPHQWV3URFHVVLQJ�&KDUDFWHU�6WULQJV

At the conclusion of these exercises, you will be able to:

• Define structure types locally in the program

• Define elementary and complex data objects

• Split strings

• Use conversion rules

• Display the contents of data objects in lists

In this exercise, you will use a template to create a program that receives asingle data record from the database table SFLIGHT in form of acharacter string. The program will split the this character string into itscomponents and display it formatted in a list.

Because the focus of this exercise is not on the transfer of data as acharacter string, we will use a function module that will provide us withthe required database. This will simulate actual cases, such as data transferfrom an external system.

3URJUDP� =%&���B��B63/,7B675,1*7HPSODWH� 6$3%&���B'727B63/,7B675,1*0RGHO�VROXWLRQ� 6$3%&���B'726B63/,7B675,1*���LV�\RXU�WZR�GLJLW�JURXS�QXPEHU

1-1 Copy the program, 6$3%&���B'727B63/,7B675,1* and give it the new name=%&���B��B63/,7B675,1*.

1-2 Familiarize yourself with the main body of the program. Pay special attention to thecontent of the data object datastring after the function module call. Use theDebugger to do this, and/or display the character string in a list. (The functionmodule itself is here seen as a black box. For this exercise, it is not necessary tounderstand its construction.)

1-3 To be able to split the character string into its components you must first removethe ## characters. Remove the two leading separators from the character string first.Then copy the initial part up to the closing separators to the auxiliary variableset_string. For this, set_string has to be defined appropriately.

Page 45: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-33

1-4 Now use the separators to split the contents of the auxiliary variable set_stringinto the structure wa_flight_c. The latter is typed with the local programstructure type st_flight_c. You still have to comment out the components ofthis structure type and assign them an appropriate type.

1-5 As a test, display the fields of the structure, wa_flight_c in a list.

1-6 In the list displayed in exercise 1-5, you should have observed that some of thefields were displayed without formatting – for example, the PRICE field. Your nextstep is to change this.To do this, convert the data you have extracted by copying it to data objects withsuitable 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 withthe structure type st_flight. You must comment out the components ofst_flight and find appropriate types for these components for the formatting.Then copy the identically-named components of the character-type structurewa_flight_c to the fields of the structure wa_flight.Display the contents of the structure wa_flight in a list. Use the appropriateformatting options for the WRITE statement for the fldate and pricecomponents.

Page 46: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-34

�6ROXWLRQ��8QLW� 'DWD�7\SHV�DQG�'DWD�2EMHFWV�LQ�'HWDLO7RSLF� 'HILQLQJ�'DWD�7\SHV�DQG�'DWD�2EMHFWV

%DVLF�6WDWHPHQWV3URFHVVLQJ�&KDUDFWHU�6WULQJV

REPORT sapbc401_dtos_split_string.

TYPES:

BEGIN OF st_flight_c,

mandt(3) TYPE c,

carrid(3) TYPE F, connid(4) TYPE Q, fldate(8) TYPE Q, price(20) TYPE F, currency(5) TYPE F, planetype(10) TYPE F, seatsmax(10) TYPE Q, seatsocc(10) TYPE Q, 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) TYPE S�'(&,0$/6��, currency(5) TYPE F, planetype(10) TYPE F, seatsmax TYPE L, seatsocc TYPE L, END OF st_flight.

Page 47: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-35

DATA:

datastring TYPE string,

VHWBVWULQJ��7<3(�VWULQJ�

wa_flight_c TYPE st_flight_c,

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

��6+,)7�GDWDVWULQJ�%<���3/$&(6���),1'����,1�GDWDVWULQJ���,)�V\�VXEUF��!�������0(66$*(�D����EF�������(1',)���63/,7�GDWDVWULQJ�$7����,172�VHWBVWULQJ�GDWDVWULQJ�

��63/,7�VHWBVWULQJ�$7���,172��������ZDBIOLJKWBF�PDQGW��������ZDBIOLJKWBF�FDUULG��������ZDBIOLJKWBF�FRQQLG��������ZDBIOLJKWBF�IOGDWH��������ZDBIOLJKWBF�SULFH��������ZDBIOLJKWBF�FXUUHQF\��������ZDBIOLJKWBF�SODQHW\SH

Page 48: 003 - Bc401 - Abap Objects

© SAP AG BC401 2-36

��������ZDBIOLJKWBF�VHDWVPD[��������ZDBIOLJKWBF�VHDWVRFF��������ZDBIOLJKWBF�SD\PHQWVXP��������ZDBIOLJKWBF�VHDWVPD[BE��������ZDBIOLJKWBF�VHDWVRFFBE��������ZDBIOLJKWBF�VHDWVPD[BI��������ZDBIOLJKWBF�VHDWVRFFBI�

��029(�&255(6321',1*�ZDBIOLJKWBF�72�ZDBIOLJKW�

WRITE: /

wa_flight-carrid,

wa_flight-connid,

����ZDBIOLJKW�IOGDWH�''�00�<<<<�����ZDBIOLJKW�SULFH�&855(1&<�ZDBIOLJKW�FXUUHQF\�����ZDBIOLJKW�FXUUHQF\�����ZDBIOLJKW�SODQHW\SH�����ZDBIOLJKW�VHDWVPD[�����ZDBIOLJKW�VHDWVRFF�

Page 49: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-1

SAP AG 2002

l ,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHVl 'HILQLQJ�LQWHUQDO�WDEOHVl ,QWHUQDO�WDEOH�RSHUDWLRQVl 1RWHV�RQ�SHUIRUPDQFHl 6SHFLDO�LQWHUQDO�WDEOHV

&RQWHQWV�

8VLQJ�,QWHUQDO�7DEOHV

Page 50: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-2

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV��8QLW�2EMHFWLYHV

l 'HILQH�LQWHUQDO�WDEOHVl 3HUIRUP�RSHUDWLRQV�RQ�LQWHUQDO�WDEOHVl ,GHQWLI\�WDEOH�NLQGV�DQG�XVH�WKHP�DSSURSULDWHO\�LQ

GLIIHUHQW�VLWXDWLRQV

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

Page 51: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-3

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV��,QWHJUDWLRQ�LQ�&RXUVH&RQWHQW

����� ���� ��� � ������ ��� � �� � � ���� � � ������ � � � ������ ��������� � ��!#"

��� � �%$'&)(*��+ � ��, !� � � � , - .�/�021�3246587 4 1 729 4�:

Page 52: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-4

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV����

,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV

'HILQLWLRQ'HILQLWLRQ

2SHUDWLRQV2SHUDWLRQV

6SHFLDO�VLWXDWLRQV6SHFLDO�VLWXDWLRQV

1RWHV�RQ�SHUIRUPDQFH1RWHV�RQ�SHUIRUPDQFH

Page 53: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-5

SAP AG 2002

7KH�,QWHUQDO�7DEOH���D�'DWD�2EMHFW�RI�9DULDEOH�/HQJWK

$%$3SURJUDP(OHPHQWDU\�GDWD�REMHFWV

6WUXFWXUHV

,QWHUQDO�WDEOH$W�UXQWLPH��WKH�UXQWLPH�V\VWHP�DOORFDWHV�PHPRU\�IRU�WKH�OLQHV�LQ�WKH�WDEOH�DV�QHHGHG��G\QDPLF�WDEOH�H[WHQVLRQ�

6WDWLF�GDWD�REMHFWV�H[FHSW�IRUW\SHV�675,1*��;675,1*�

n Internal tables are data objects that allow you to store datasets with a fixed structure in memory. Thedata is stored line by line in memory. Each line has the same structure.

n You can also refer to each component in a line as a column in the internal table. You refer to eachline in the internal table as a table line or table entry.

n 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 withwhich you are working.

n You can define the line type of an internal table to include elementary, structured, or even tabletypes, and can be as complex as you want to make it.

Page 54: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-6

SAP AG 2002

$WWULEXWHV�RI�,QWHUQDO�7DEOHV

/LQH�W\SH/LQH�W\SH

$$ ���� ���������������0,

4) ���� ������ �������0,

8$ ���� ����� �������0,

/+ ���� ����� �������.0 /+ ���� ����� �������.0

7DEOH�NLQG7DEOH�NLQGStandard tableStandard tableSorted tableSorted tableHashed tableHashed table

.H\�GHILQLWLRQ.H\�GHILQLWLRQ(( .H\�ILHOGV.H\�ILHOGV(( 6HTXHQFH6HTXHQFH(( 8QLTXHQHVV8QLTXHQHVV

&$55,'&$55,' &211,'&211,' ',67$1&(',67$1&( ',67,'',67,'

�/LQHLQGH[

,QGH[�DFFHVV�

.H\�DFFHVV64�����64����� 64 ���� ������� �������0,

�����

n Each internal table has the following attributes:

é /LQH�W\SH�You use the line type to specify which columns are to be included in your internal table. You mustassign a name and type to each column. You generally define a line type using a structure type.

é .H\�Internal tables have a key, just like database tables. The key is defined by: Specifying the FROXPQVthat should belong to the NH\; the VHTXHQFH; and whether or not the key should be XQLTXH or QRQ�XQLTXH. Depending on how you define your internal table, the runtime system may create an DGGLWLRQDOXQLTXH�NH\�(the LQGH[) for some internal tables (this will be discussed in more detail later). Forsimplicity’s sake you can think of the index as a OLQH�QXPEHU.

é 7DEOH�NLQG�There are three table kinds in ABAP. Each kind has different attributes, which specify whether thekey is unique, how the index is managed, and how the table data is addressed internally. The tablekind specifies the possible accesses. The purpose of this is to provide particular support for specialaccess types from the runtime system (will be discussed later).

Page 55: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-7

SAP AG 2002

/LQN�%HWZHHQ�7DEOH�.LQG�DQG�$FFHVV�7\SH

,QGH[�DFFHVV

.H\�DFFHVV

8QLTXHQHVV ;=<>;@?BAC;ED FGA@H AE;ED FIAJHLKM;N<I;O?PAE;CD FIACH AJ;@D FQAEH

$FFHVV�E\� R>SUTMV W XZY\[�]6^8_ R`S6T'V W Xba�^8X c=^8XdSe[%W X

HASHED TABLEHASHED TABLESTANDARD TABLESTANDARD TABLE SORTED TABLESORTED TABLE

/LQN�%HWZHHQ�7DEOH�.LQG�DQG�$FFHVV�7\SH

Q

,QGH[�WDEOHV +DVKHG�WDEOH7DEOH�NLQG

f�g6h W ^QT�i g [f%g%h W ^>T�i g [j Y�[ g6k Xj Y�[ g�k XTM^ g%k i%lTU^ g6k iUl

m=g T%lmNg Tnlo�p [�i8V Y SB[o�p [�i'V Y SB[

The table kind is linked to the access type as follows:

n You can only perform an index access (the fastest type of access) on STANDARD and SORTEDtables.

n The runtime system implements key accesses of SORTED and HASHED tables in a way thatoptimizes 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.

n 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 keyfields. With the runtime-optimizing access, the runtime system keeps SORTED tables sorted by keyfields,In all other cases, the system performs a WDEOH�VFDQ.

n The runtime optimizes key accesses of HASHED tables IRU�IXOO\�TXDOLILHG�NH\V�RQO\. The tableentries are addressed by means of a special hash algorithm. This minimizes access time for readingsingle records with fully qualified keys. The access time is thus independent of the number of linesin the table.In all other cases, the system performs a WDEOH�VFDQ. This means that you should only use HASHEDtables if you want to access table entries using a fully-qualified key.

Page 56: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-8

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV����

,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV

'HILQLWLRQ'HILQLWLRQ'HILQLWLRQ

2SHUDWLRQV2SHUDWLRQV

6SHFLDO�VLWXDWLRQV6SHFLDO�VLWXDWLRQV

1RWHV�RQ�SHUIRUPDQFH1RWHV�RQ�SHUIRUPDQFH

Page 57: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-9

SAP AG 2002

2YHUYLHZ��7\SHV�RI�'HILQLWLRQV�IRU�,QWHUQDO�7DEOHV

:LWK�ORFDO�WDEOH�W\SH:LWK�ORFDO�WDEOH�W\SH ,PSOLFLW�WDEOH�W\SH,PSOLFLW�WDEOH�W\SH :LWK�JOREDO�WDEOH�W\SH:LWK�JOREDO�WDEOH�W\SH

7<3(6���ORFDOBW\SH����

'$7$���WBOLVW�7<3(������ORFDOBW\SH�

'$7$���WBOLVW�7<3(����

'$7$���WBOLVW�7<3(������JOREDOBW\SH�7DEOH

DWWULEXWHVORFDOBW\SHORFDOBW\SH JOREDOBW\SHJOREDOBW\SH

/RFDO�WDEOH�W\SH

,QWHUQDO�WDEOH ,QWHUQDO�WDEOH ,QWHUQDO�WDEOH

7DEOHDWWULEXWHV

*OREDO�WDEOH�W\SH

ORFDOBW\SHORFDOBW\SH JOREDOBW\SHJOREDOBW\SH7DEOHDWWULEXWHV

n There are several ways to define an internal table in ABAP:

é Use the TYPES statement to define a table type. Then use this to assign a type to one or more dataobjects or interface variables.

é Alternatively, declare the table attributes directly when you define the data object (that is, use aERXQG type).

é The third option is to define an internal table using a global table type (defined in the ABAPDictionary).

n The tables you define using the first or second option are visible only in the program in which youdefine them. Whenever you need table type visible from all programs, create a global table type (thatis, use the third option).

Page 58: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-10

SAP AG 2002

7KH�'DWD�7\SH�RI�DQ�,QWHUQDO�7DEOH

��QDPH�7<3(�WDEOHBW\SH�������2)���OLQHBW\SH�������:,7+�NH\BGHI�����>�,1,7,$/�6,=(�Q�@�

7<3(7<3( WDEOHBNLQGWDEOHBNLQG2)2):,7+:,7+ NH\BNH\BGHIGHI,1,7,$/�6,=(,1,7,$/�6,=(

( 7DEOH�NLQG( /LQH�W\SH��FROXPQV�( .H\( ,QLWLDO�VL]H��RSWLRQDO�

/RFDO�WDEOH�W\SH�LQWHUQDO�WDEOH/RFDO�WDEOH�W\SH�LQWHUQDO�WDEOH7<3(6 RU '$7$ $WWULEXWHV

*OREDO�WDEOH�W\SH*OREDO�WDEOH�W\SH

JOREDOBW\SH,QSXW�RQ�VFUHHQ�XVHGWR�VSHFLI\�DWWULEXWHV�

( 7DEOH�NLQG( /LQH�W\SH��FROXPQV�( .H\

$WWULEXWHV

n To define a table type (explicitly or implicitly), you must give the type (or data object) a name, aswell as specifying a WDEOH�NLQG� OLQH�W\SH� and NH\�

n If you are defining a local table type, enter the kind after TYPE and the line type after OF. You mustlist the key fields after WITH.

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

n For table types defined in a program, you can enter the number of lines that the runtime systemshould reserve when it initializes the data object, after the INITIAL SIZE addition. This makessense 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 theruntime system frees the necessary memory dynamically.(Internal tables are G\QDPLF data objects).

Page 59: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-11

SAP AG 2002

7DEOH�$WWULEXWH��/LQH�7\SH

7<3(6���%(*,1�2)�OLQHBW\SH����������(1'�2)�OLQHBW\SH�

*OREDO�VWUXFWXUH�W\SH

OLQHBW\SHOLQHBW\SH OLQHBW\SHOLQHBW\SH

/RFDO�VWUXFWXUH�W\SH

'$7$���VWUXFWXUH�7<3(����VWUXFWXUHVWUXFWXUH

/RFDO�VWUXFWXUH/LQH�W\SH/LQH�W\SH

����QDPH�7<3(�WDEOHBNLQG���������2)���OLQHBW\SH���������:,7+�NH\BGHI���������>�,1,7,$/�6,=(�Q�@�

7<3(7<3(OLQHBW\SHOLQHBW\SH2)2)

����QDPH�/,.(�WDEOHBNLQG���������2)���VWUXFWXUH���������:,7+�NH\BGHI���������>�,1,7,$/�6,=(�Q�@�

VWUXFWXUHVWUXFWXUH/,.(/,.(

7DEOH�W\SH7DEOH�W\SH

'$7$'$7$7<3(67<3(6

2)2)

n To specify the line type of an internal table, you can use all the local and global data types or dataobjects.

n Internal tables are most frequently used to display contents of the database tables. Normally, non-nested structured data types are used for this.

n 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 isthe name of the structure type you are using.

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

Page 60: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-12

SAP AG 2002

7DEOH�$WWULEXWH��7DEOH�.LQG�DQG�.H\

7<3(6���LWDEW\SH�7<3(�WDEOHBNLQG�����������2)���OLQHBW\SH�����������:,7+�NH\BGHI���������>�,1,7,$/�6,=(�Q�@�'$7$���WBQDPH�7<3(�LWDEW\SH�

WDEOHBNLQGWDEOHBNLQG

:,7+:,7+ NH\NH\BBGHIGHI

WDEOHBNLQGWDEOHBNLQG NH\NH\BBGHIGHI67$1'$5'�7$%/(

6257('���7$%/(+$6+('���7$%/(

>121�81,48(@�^�.(<�FRO������FROQ�_���������������'()$8/7�.(<�`^81,48(�_�121�81,48(`�.(<�FRO�����FROQ�81,48(�.(<�FRO������FROQ

n There are three parameters that you use to specify a table kind for your internal table: STANDARDTABLE, SORTED TABLE and HASHED TABLE

n You specify the table key with the :,7+ NH\BGHI addition. NH\BGHI includes the names of allthe key fields in order and specifies whether the key is to be UNIQUE or NON-UNIQUE.

n The combination of the table kind and the key definition is very significant, because of the specialsupport that certain table kinds receive with specific types of read access. You can use any of thefollowing combinations:

é For STANDARD tables:Either create a user-defined key by naming the key fields after NON-UNIQUE KEY, or specify thestandard key using the WITH DEFAULT KEY addition. The standard key consists of all the fieldswith 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.

n Alternatively, use the pseudo-component table_line, if you are specifying a table without astructured line type, or if the entire table line is being used as the key. This will be discussed in moredetail later in the unit.

Page 61: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-13

SAP AG 2002

([DPSOH��6WDQGDUG�7DEOH�ZLWK�/RFDO�7\SH

7<3(6���%(*,1�2)�VBGLVWDQFHBW\�����FDUULG���7<3(�VBFDUUBLG�����FRQQLG���7<3(�VBFRQQBLG�����GLVWDQFH�7<3(�VBGLVWDQFH�����GLVWLG���7<3(�VBGLVWLG���(1'�2)�VBGLVWDQFHBW\�

7<3(6���WWBGLVWDQFHBW\�7<3(�67$1'$5'�7$%/(�����������������2)�VBGLVWDQFHBW\�����������������:,7+�121�81,48(�����������������.(<�GLVWDQFH�GLVWLG�

'$7$���WWBGLVWDQFH�7<3(�WWBGLVWDQFHBW\�

/LQH�W\SH�

7DEOH�W\SH�

,QWHUQDO�WDEOH,QWHUQDO�WDEOH,QWHUQDO�WDEOHRI�WKH�WDEOH�NLQGRI�WKH�WDEOH�NLQG67$1'$5'�7$%/(67$1'$5'�7$%/(

VBGLVWDQFHBW\VBGLVWDQFHBW\

WWWWBGLVWDQFHBGLVWDQFH

WWBGLVWDQFHBW\WWBGLVWDQFHBW\

WWWWBGLVWDQFHBBGLVWDQFHBW\W\

VBGLVWDQFHBW\VBGLVWDQFHBW\

n The above example shows the definition of an internal table (tt_distance) using a local tabletype (tt_distance_ty), which itself uses a local line type (s_distance_ty).

n The internal table defined here is a STANDARD table with the line type s_distance_ty. Itcontains the columns carrid, connid, distance and distid.

n The distance and distid fields are key fields. The key is not unique.

Page 62: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-14

SAP AG 2002

([DPSOH��6RUWHG�DQG�+DVKHG�7DEOH

'$7$���GLVWDQFHBUDQF����7<3(�6257('�7$%/(����2)�VBGLVWDQFHBW\����:,7+�121�81,48(�.(<������GLVWLG�GLVWDQFH�

6RUWHG�WDEOH

GLVWDQFHBUDQFGLVWDQFHBUDQF

'$7$���GLVWDQFHBEXIIHU����7<3(�+$6+('�7$%/(����2)�VBGLVWDQFHBW\����:,7+�81,48(�.(<������FDUULG�FRQQLG�

+DVKHG�WDEOH�

GLVWDQFHBEXIIHUGLVWDQFHBEXIIHU

/+/+$$�40

����������������

���������������������

.0

.00,0,

FDUULG FRQQLG GLVWDQFH GLVWLG��

����

/+$$40/+

����������������

���������������������

.00,0,.0

FDUULG FRQQLG GLVWDQFH GLVWLG� �

6HTXHQFH

6HTXHQFH

6HTXHQFH

VBGLVWDQFHBW\VBGLVWDQFHBW\

VBGLVWDQFHBW\VBGLVWDQFHBW\

n The above example shows the definition of a SORTED table and a HASHED table. Both tables havethe same line type as the STANDARD table on the previous slide.

n Note that the contents of the table are in a different order. For SORTED tables, the sequence of theentries in the internal table is determined by the sequence of fields in the key definition.

Page 63: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-15

SAP AG 2002

7KH�6WDQGDUG�7DEOH�7\SH

'$7$���WBQDPH�7<3(�7$%/(���������2)�OLQHBW\SH�

7$%/(7$%/(

'$7$���WBQDPH�7<3(�67$1'$5'��7$%/(���������2)�OLQHBW\SH���������:,7+�'()$8/7�.(<���

67$1'$5'

:,7+�'()$8/7�.(<

7<3(7<3(2)2)

,QWHUSUHWHG�E\WKH�V\VWHP�DV�

'HILQLWLRQ�E\VWDQGDUG�WDEOH�W\SH

'$7$���WBVIOLJKW�7<3(�7$%/(�2)�VIOLJKW�

([DPSOH�

7<3(�7$%/(�2)7<3(�7$%/(�2)

7<3(7<3( 7$%/(7$%/(2)2)

n A standard type exists for defining standard tables with bound types. (Table type STANDARDTABLE, key WITH DEFAULT KEY). When you create such a table, you can omit the STANDARDand WITH DEFAULT KEY additions, since the runtime system supplies them automatically.

n Note however, that the standard table type exists for data objects only. If you are defining a tabletype that you want to use to provide a type for data objects, you must specify all its attributescompletely.

n Incomplete table types are known as JHQHULF. You can only use them to assign types to table-typeinterface parameters.

Page 64: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-16

SAP AG 2002

,QWHUQDO�7DEOHV�ZLWK�DQ�8QVWUXFWXUHG�/LQH�7\SH

'$7$���LWBILHOGV�7<3(�67$1'$5'�7$%/(�2)�GG��O�ILHOGQDPH������������:,7+�.(<�WDEOHBOLQH�

��ZDBILHOG��7<3(�GG��O�ILHOGQDPH�

6(/(&7�ILHOGQDPH�)520�GG��O��WDEOH�RI�WUDQVS��WDEOHV�������,172�7$%/(�LWBILHOGV�������:+(5(�WDEQDPH� �63)/,�

WDEOHWDEOHBBOLQHOLQH

'HILQLWLRQ�

)LOO�

5($'�7$%/(�LWBILHOGV�,172�ZDBILHOG�����������:,7+�.(<�WDEOHBOLQH� �&,7<)520�

.H\�DFFHVV� WDEOHWDEOHBBOLQHOLQH

n Use an unstructured line type if you need a single-column internal table. The slide shows an exampleof a single-column table with the line type field_name.

n To declare an explicit key, you must use the pseudo-component table_line.

n You can use internal tables with an unstructured line type in the following ABAP statement (amongothers):

é SET PF-STATUS .. EXCLUDING itab. (deactivate function codes dynamically)

é SPLIT .. INTO TABLE itab. (split a string dynamically)

n You can use a key access, for example, to ascertain whether a specific entry H[LVWV.

Page 65: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-17

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV����

,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV

'HILQLWLRQ'HILQLWLRQ

2SHUDWLRQV2SHUDWLRQV2SHUDWLRQV

6SHFLDO�VLWXDWLRQV6SHFLDO�VLWXDWLRQV

1RWHV�RQ�SHUIRUPDQFH1RWHV�RQ�SHUIRUPDQFH

Page 66: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-18

SAP AG 2002

2YHUYLHZ��7\SHV�RI�2SHUDWLRQ

,QWHUQDO�WDEOH,QWHUQDO�WDEOHDV�D�GDWD�REMHFWDV�D�GDWD�REMHFW

2SHUDWLRQV�RQ�RQH�RU2SHUDWLRQV�RQ�RQH�RUPRUH�OLQHVPRUH�OLQHV

(GLWLQJ�OLQHV(GLWLQJ�OLQHVLQ�D�ORRSLQ�D�ORRS

RU

( &RS\ ��029(� �( ,QLWLDOL]H ��&/($5��

.HHS�PHPRU\�DYDLODEOH( ,QLWLDOL]H ��)5((����

5HOHDVH�PHPRU\( &RPSDUH�XVLQJ�RSHUDWRUV

��!�!��� �� �!��( 6RUW�����������6257��( $UUD\�)HWFK���6(/(&7�������������,172�7$%/(��

( ,QVHUW ��,16(57 �( 5HDG ��5($' �( &KDQJH ��02',)< �( 'HOHWH ��'(/(7( �( ,QVHUW�VXPPDWHG

�������������������&2//(&7��

( (GLW�OLQHVVHTXHQWLDOO\

/223���� �FRPPDQG�EORFN(1'/223����

You can perform three different types of operations on internal tables:

n 2SHUDWLRQV�RQ�WKH�ZKROH�GDWD�REMHFW:Elementary operations available include MOVE (copy the content line by line), CLEAR (delete thecontent, but keep the memory allocated), and FREE (delete the content and release the memory).You can also use all the numerical comparison operators with internal tables in logical expressions.With these operators, the number of lines is compared first and then, if necessary, the line contents.(This is recursive for deep types). For more information, refer to the documentation on internaltables.You can use the SORT statement to sort the content of STANDARD and HASHED tables. Thisstatement allows you to sort the table by one or columns, specifying DVFHQGLQJ or GHVFHQGLQJ foreach column. For more details, refer to the keyword documentation for the SORT�statement.You can use the SELECT ... INTO TABLE statement to copy the contents of database tablesdirectly into internal tables.

n 2SHUDWLRQV�RQ�OLQHV:You can use the INSERT, READ, MODIFY, DELETE, and COLLECT statements to performoperations on one or more lines in the internal table. The syntax of the first four of these statementsis dealt with in the slides that follow. For the syntax of the COLLECT statement, refer to the onlinedocumentation.

n /RRS�SURFHVVLQJ�You can use the LOOP AT ... ENDLOOP statement to process the lines of the internal tablesequentially. The exact syntax for this is dealt with in the slides that follow.

Page 67: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-19

SAP AG 2002

/LPLWLQJ�WKH�6FRSH�RI�/LQH�2SHUDWLRQV��7DUJHW5HFRUGV�DQG�$FFHVV�7\SH

Q�FRQGLWLRQ!>�Q������Q��@

7DEOH�NH\/LQH�LQGH[��IRU�LQGH[�WDEOHV�&RQGLWLRQ�DSSOLHG�WR�DQ\�QXPEHU�RI�FROXPQV,QGH[�LQWHUYDO��IRU�LQGH[�WDEOHV�

:RUN�DUHD

&RS\LQJ�SURFHVV&RS\LQJ�SURFHVV 'LUHFW�DFFHVV�E\'LUHFW�DFFHVV�E\GHUHIHUHQFHGGHUHIHUHQFHG�SRLQWHU�SRLQWHU

)LHOG�V\PERO

,QVHUW,QVHUWXVLQJ�WDEOHXVLQJ�WDEOH

7DEOH

6SHFLI\�VHW�RI�WDUJHW�UHFRUGV6SHFLI\�VHW�RI�WDUJHW�UHFRUGV

6SHFLI\�DFFHVV�W\SH���GDWD�WUDQVIHU�W\SH6SHFLI\�DFFHVV�W\SH���GDWD�WUDQVIHU�W\SH

n Almost all line operations and loop processing statements offer you several options for specifyingthe target set of records to be processed. You can specify these target records by declaring a key orindex, by formulating a condition that applies to some of the columns, or by specifying an indexinterval. 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.

n When accessing table rows, you have two possibilities for a number of line operations and for loopprocessing: You can either copy the data into a structure with the same type as the line type of theinternal table, or you can set a pointer to a table line and access the data in that line directly.

n In particular, when you insert table lines, you can pass the data to be inserted using another internaltable.

n Note that all statements either set a return value or trigger a runtime error. If a return code has beenset, it is stored in the sy-subrc field after the statement has been executed. Runtime errors are onlytriggered if the data passed at runtime are crucial in determining whether or not the operation can beperformed.Example:You try to insert a new line into a SORTED table using an index. If you insert the new line in exactlythe right place in a sorted table, the system performs the operation. If not, the system returns aruntime error.

Page 68: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-20

SAP AG 2002

3UHYLHZ��)LHOG�6\PER,V����

�YDUBD

�IV!'$7$��YDUBD�7<3(�L�9$/8(���),(/'�6<0%2/6���IV!�7<3(�L�

'DWD�REMHFWV�LQ�WKH�$%$3�SURJUDP

),(/'�6<0%2/6),(/'�6<0%2/6

$66,*1�YDUBD�72��IV!�

�IV!� ����

YDUBD

�IV!

YDUBD

�IV!

��

7LPH

$66,*1$66,*1 7272

'HILQLQJ�D�ILHOG�V\PERO�

$VVLJQLQJ�D�GDWD�REMHFW�WR�D�ILHOG�V\PERO�

$VVLJQLQJ�D�YDOXH�WR�D�GDWD�REMHFWXVLQJ�D�ILHOG�V\PERO�

n You can create a pointer to a data object in ABAP using a field symbol.

n First, declare a data object using the ),(/'�6<0%2/6 statement. This data object can contain apointer to another data object at runtime. Where possible, you should give the field symbol the sametype as the data object (7<3(�L, in this example).

n Note that the angle brackets (<>) are part of the name of the field symbol: In this example, the nameis <fs>.

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

n You can use the field symbol to access the content of the data object to which it points - either toread or to change this content.

n You can "redirect" a field symbol to a different data object at runtime using the $66,*1 statement.

Page 69: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-21

SAP AG 2002

3UHYLHZ��)LHOG�6\PER,V����

'$7$��LWBVIOLJKW�7<3(�7$%/(�2)�VIOLJKW�

),(/'�6<0%2/6���IV!�7<3(�VIOLJKW�

qNg V g S h r ^�i8VsTtY*[IV2lM^`u j uIvxw k S%y kzgU{'HILQLQJ�WKH�ILHOG�V\PERO�

$FFHVVLQJ�WKH�OLQH�FRQWHQW�XVLQJ�WKH�ILHOG�V\PERO�

�IV!

LWBVIOLJKW

$VVLJQLQJ�D�OLQH�WR�WKH�ILHOG�V\PERO�

�����WR�EH�FRQWLQXHG����

:5,7(��IV!�FRQQLG��IV!�FDUULG� �/+�

�IV!

LWBVIOLJKW

�IV!

LWBVIOLJKW

n In the above example, a field symbol is assigned the line type of an internal table. This makes itpossible to assign a table line to this field symbol. The syntax required for this is discussed later inthis unit.

n After a field symbol has been assigned a line, it is also possible to access the individual columnvalues of the assigned line.

n As well as being able to read the data contents, you can also change the contents of the individualcomponents.

Page 70: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-22

SAP AG 2002

,QVHUW

ZDLWDE

�ILOO�ZRUNDUHD��ZD�,16(57�ZD�,172�7$%/(�LWDE�,16(57,16(57 ,172�7$%/(,172�7$%/(

ZDLWDE �ILOO�ZRUNDUHD��ZD�

,16(57�ZD�,172�LWDE�,1'(;�Q�,16(57,16(57 ,172,172 ,1'(;,1'(;Q Q

LWDE� LWDE�,16(57�/,1(6�2)�LWDE�����>�)520�Q��72�Q��@����WDUJHW�>Q�@

>Q�@

2QO\�LI�LWDE��LVDQ�LQGH[�WDEOH

,16(57�/,1(6�2),16(57�/,1(6�2)

,172�7$%/(�LWDE��,172�LWDE��>�,1'(;�Q�@�Q

WDUJHWQ

,172�7$%/(,172�7$%/(,172,172

.H\�IURP��ZD�VSHFLILHV�WKH�OLQH

To insert lines into a table, use the INSERT statement:

n Using INSERT wa INTO TABLE itab, you insert the line whose data are stored in wa into theinternal table. The record is inserted with reference to the table key. In STANDARD tables, the newline is appended to the end of the table.

n Using INSERT wa INTO itab INDEX n, you insert the data from ZD into the internal table atthe line with the index Q. If Q is an index value within the internal table, the lines with the indexvalue greater than or equal to Q are pushed downwards. If n-1 is greater than the number of lines inthe internal table, the line is not inserted.

n Always add new lines to a SORTED table using the key. If you try to insert a line into a SORTEDtable using the index, you will not succeed unless the line has exactly the same index number as itwould have when correctly sorted. Otherwise, a runtime error occurs.

n Use INSERT LINES OF itab1 INTO TABLE itab2 to insert the lines of the internal tableitab1 into the internal table itab2. Also, if itab1 is an index table, you can limit the number oflines to be inserted using an index interval (FROM n1 TO n2).

n If your target table is an index table, you can use INSERT LINES OF itab1 INTO itab2 toinsert the lines of the internal table itab1 into the internal table itab2. You can then specify theline 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).

Page 71: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-23

SAP AG 2002

5HDGLQJ�D�6LQJOH�5HFRUG�LQWR�D�:RUN�$UHD�8VLQJWKH�/LQH�1XPEHU

,QGH[ZD

LWDE

5($'�7$%/(�LWDE�,1'(;�Q�,172�ZD�>�RSWLRQV�@�5($'�7$%/(5($'�7$%/( ,1'(;,1'(;

Q Q

,172,172

( I������IP( $//�),(/'6

>�RSWLRQV�@��&203$5,1*����FRPSBOLVW��75$163257,1*�WUDQVBOLVW

&203$5,1*&203$5,1*75$163257,1*75$163257,1*

( I������IN( 12�),(/'6

FRPSBOLVW�

WUDQVBOLVW�

%HOLHELJH�)HOGOLVWH$Q\�ILHOG�OLVW

n You can read single table lines from index tables using the READ TABLE itab INDEX n INTOwa statement. After INDEX, enter the index of the line you want to read. If the system was able toread the line, it sets the return code to sy-subrc = 0 and stores the data from the line in wa. Thetotal number of lines is then in sy-tfill, the length of the lines in sy-tleng. If the systemcould not read the line, the return code sy-subrc is unequal to 0. In this case, the content of wa isnot changed.

n Use the option TRANSPORTING addition to specify the columns for which you want to transportdata:

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

n You can use the optional addition COMPARING I� ... IP to ascertain whether or not the line tobe read Q has specific column contents: To do this, copy the value of all columns that point to therecords you want to read, into wa and list the columns after the COMPARING addition to the READstatement. If the system was able to read the line and if all the columns listed contain the valuesstored in ZD, it sets the return code sy-subrc to zero. If the system was able to read the line, but ifone or more of the columns does not contain the value stored for it in ZD, it sets the return code sy-subrc to two. If it could not read the line, it sets the return code to greater than two.

n The COMPARING ALL FIELDS addition provides a shorter syntax for comparing all the columns.

Page 72: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-24

SAP AG 2002

5HDGLQJ�D�6LQJOH�5HFRUG�LQWR�D�:RUN�$UHD�8VLQJWKH�)LHOG�&RQWHQWV

.H\

5($'�7$%/(�LWDE�VHDUFKBFODXVH�,172�ZD�>�RSWLRQV�@�

ZDLWDE ZD� 7DEOH�NH\�IURP�ZD�

([SOLFLW�WDEOH�NH\

5($'�7$%/(5($'�7$%/(

7DEOH�NH\ )520�ZD�:,7+�7$%/(�.(<�N�� �I������NQ� �IQ:,7+�.(<�FRO�� �I������FROP� �IP��������������>%,1$5<�6($5&+@

$Q\�FRQWHQW�IURP�ILHOG�OLVW

,172,172

VHDUFKBFODXVH

$Q\�ILHOGFRQWHQWV

$V�LQ�LQGH[DFFHVV

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

n If you want to use a table key, you have two options:

é Copy the key field values of the entry you want to read into the work area wa1 and use READTABLE itab FROM wa1 INTO wa. Provided sy-subrc = 0, the system stores the resultof the READ statement in the work area wa. Note that the values in wa1 that you have notexplicitly filled contain appropriately-typed initial values. Thus, the READ TABLE itab FROMwa1 statement searches for a line that has initial values in the key fields that were not declaredexplicitly.Note:You can also use a single work area wa for both declaring the key fields and receiving the result:READ TABLE itab FROM wa INTO wa.

é Evaluate the key fields explicitly using the call READ TABLE itab WITH TABLE KEY k1= f1 ... kn = fn statement. In this case, you must fill all the key fields.

n You can specify a formulated search condition to be applied to any columns using READ TABLEitab WITH KEY ....

n However, you can only use dynamically formulated read accesses using the key and the READTABLE itab FROM wa statement.

Page 73: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-25

SAP AG 2002

5HDGLQJ�D�6LQJOH�5HFRUG�8VLQJ�D�)LHOG�6\PERO

,QGH[

.H\

Q

LWDE

),(/'�6<0%2/6���IV!�/,.(�/,1(�2)�LWDE�

5($'�7$%/(�LWDE����������$66,*1,1*��IV!�

:5,7(�����)6!�ILHOGB������

�IV!

Q ,QGH[

.H\

RU

$66,*1,1*$66,*1,1* �� !!5($'�7$%/(5($'�7$%/(,1'(;�Q

NH\,1'(;,1'(;

��IV!�ILHOGB�IV!�ILHOGB�

6DPH�DV�ZKHQ�UHDGLQJ�LQWR�D�ZRUN�DUHD

n You can use either a work area or a field symbol to access the individual lines in a table you want toread.

n To use a field symbol, first define it using FIELD-SYMBOLS <fs>. Give it the same type as theline type of the internal table that you want to read.Example: FIELD-SYMBOLS <fs> LIKE LINE OF itab.

n Set the pointer to the correct line using the ASSIGNING <fs> addition instead of INTO. You canuse any search criterion you want.

n 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 sub-components of this structured component, you need another field symbol.

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

Page 74: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-26

SAP AG 2002

&KDQJLQJ�WKH�7DEOH�8VLQJ�D�:RUN�$UHD

ZDLWDE �ILOO�ZRUNDUHD��ZD�

02',)<�LWDE�)520�ZD�,1'(;�Q�>�75$163257,1*�I������IQ�@�

Q

ZDLWDE �ILOO�ZRUNDUHD��ZD�

02',)<�7$%/(�LWDE�)520�ZD>�75$163257,1*�I������IQ�@�

02',)<02',)< )520)520 ,1'(;,1'(;75$163257,1*75$163257,1*

02',)<�7$%/(02',)<�7$%/( )520)52075$163257,1*75$163257,1*

Q

�02',)<�LWDE�)520�ZD��75$163257,1*�I��I�������:+(5(�ORJBH[SU�

02',)<02',)< )520)52075$163257,1*75$163257,1*:+(5(:+(5(

ZDLWDE

:+(5(ORJBH[SU�

.H\�IURP��ZD�VSHFLILHV�WKH�OLQH

n The MODIFY TABLE itab FROM wa statement allows you to change the content of RQH line ofthe internal table itab. The runtime system specifies the line to be changed using the key valuesfrom the work area ZD and changes the non-key fields using the other fields. If your table has a non-unique key, the system changes the first entry only (using a linear search algorithm in STANDARDtables, and a binary search algorithm in SORTED tables). If you want to change only some of thefields (that is, columns) in a line, you must specify these after TRANSPORTING.

n You can change the Qth line in an index table using MODIFY itab FROM wa INDEX n. Notethat all the fields of the structure wa will be copied. Since changes to key fields in SORTED andHASHED tables can cause non-catchable runtime errors, you PXVW use the TRANSPORTING additionin this case.

n If you want to make the same changes to several lines in a table, use the MODIFY itab FROM waTRANSPORTING f1 f2 ... WHERE log_expr statement. Specify all the columns to bechanged after TRANSPORTING. You must place the new values for these columns in the work areawa. Specify a condition for the line using the WHERE clause.

Page 75: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-27

SAP AG 2002

&KDQJLQJ�D�6LQJOH�5HFRUG�8VLQJ�D�)LHOG�6\PERO

,QGH[

.H\

Q

LWDE

),(/'�6<0%2/6���IV!�/,.(�/,1(�2)�LWDE�

5($'�7$%/(�LWDE����������$66,*1,1*��IV!�

�)6!�ILHOGB�� �����

�IV!

Q ,QGH[

.H\

RU

$66,*1,1*$66,*1,1*5($'�7$%/(5($'�7$%/(,1'(;�Q

NH\

��IVIV!�ILHOGB�!�ILHOGB�

�IV!�ILHOGB�� ����

&DQQRW�EH�NH\�ILHOG�LQVRUWHG�RU�KDVKHG�WDEOHV 6DPH�DV�ZKHQ�UHDGLQJ

LQWR�D�ZRUN�DUHD

n Instead of changing an individual line in a table using MODIFY, you can also change it using fieldsymbols. First use READ TABLE ... ASSIGNING <fs> to assign a field symbol to the line tobe changed. Then change the components directly using the field symbol components <fs>-field_1 = ....

n When you assign the field symbol using READ TABLE ... ASSIGNING you can specify the lineto be read using a key (WITH TABLE KEY), index (for index tables), or a condition (WITH KEY).

n Note that you cannot change key fields in SORTED or HASHED tables. - trying to do so causes aruntime error.

Page 76: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-28

SAP AG 2002

'HOHWH

LWDE

'(/(7(�LWDE�,1'(;�Q�Q

LWDE �ILOO�ZRUNDUHD��ZD�'(/(7(�7$%/(�LWDE�NH\�

ZD

([SOLFLW

'(/(7('(/(7( ,1'(;,1'(;

'(/(7(�7$%/('(/(7(�7$%/(

( )520�ZD( :,7+�7$%/(�.(<�

���N�� �I������NQ� �IQ

7DEOH�NH\

�'(/(7(�LWDE�:+(5(�ORJBH[SU�'(/(7('(/(7( :+(5(:+(5('HOHWH�XVLQJ�D�FRQGLWLRQ

Q

:+(5(ORJBH[SU�

NH\

n Use the READ TABLE itab INDEX n INTO wa statement to delete single table lines fromindex tables. Specify the line you want to delete using the table key. There are two ways of doingthis:

é Copy the key field values of the entry you want to delete into the work area wa and use DELETETABLE itab FROM wa.

é Fill the key fields directly in the DELETE statement using the WITH TABLE KEY k� = f�... kQ = fQ addition.

n You can delete the Qth line from the index table itab using DELETE itab INDEX n

n If you want to delete�several lines from an internal table, use the DELETE itab WHERElog_expr statement. The condition that specifies the lines you want to delete is declared inORJBH[SU, in the WHERE clause (where ORJBH[SU is any logical expression applied to thecolumns).

Page 77: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-29

SAP AG 2002

/RRS�3URFHVVLQJ

�/223�$7�LWDE�UHVXOW���������>75$163257,1*�12�),(/'6@���������^�>�)520�Q��@�>�72�Q��@�_�����������>�:+(5(�ORJBH[SU�@�`���������(1'/223�

Q ,QGH[�WDEOHV�RQO\

( ,172�ZD( $66,*1,1*��IV!

/223�$7/223�$7

(1'/223(1'/223

)520)520 7272:+(5(:+(5(

UHVXOWZD LWDE

�IV!

UHVXOW

,QGH[�WDEOH�V\�WDEL[�FRQWDLQV�OLQHLQGH[�RI�FXUUHQW�OLQH

n Use the LOOP AT itab UHVXOW ... ENDLOOP statement to perform loop processing on thelines of the internal table itab. The system then executes the statement block between LOOP ATitab ... 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. Usethis addition if, for example, you simply want to determine the number or indexes of the linesprocessed by the loop.

n Specify the number of lines that the loop is to process using a WHERE clause (condition for anycolumns).

n In an index table, limit the number of lines processed in the loop by declaring an index interval.

n While the loop is being processed, the system field sy-tabix contains the index of the table row ofthe current loop pass. The total number of lines is in sy-tfill, the length of the lines in sy-tleng.

n Note that in index tables, you can perform all the line operations (which you previously performedon the line specified in the INDEX n addition) ZLWKRXW this addition in the loop. The operation isthen performed on the current line.

Page 78: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-30

SAP AG 2002

2YHUYLHZ�RI�2SHUDWLRQV|@}�~'�B�U~M�2��}�~'�%�)� |O�U�#})�'�b}�~'�%��� �C~��U�B�'�Z}\~'�%�\�

,16(575($'�7$%/(02',)<'(/(7(

c=^8X g i'i�^MT'T

D�[�]6^�_ g i'i'^'T'T

/223�$7

,16(575($'�7$%/(02',)<'(/(7(/223�$7

6257

� S k VTM^M� p ^6[�i'^i'S p W�] h ^� Y SBW g VP^M] �e�����'�'���

���8� �����8���`���� � � � ���� �M��� ���

 =¡'¡�¢8£6¤I¥*¦G¢8£�¤�¦�§¥�¨'©�ª ¢

««

n Index accesses (such as APPEND, INSERT ... INDEX, LOOP AT ... FROM ... TO) arepossible for standard and sorted tables. However, caution is advised when using INSERT orAPPEND on sorted tables; this would violate the sort sequence and cause a runtime error.

n You can use the SORT statement to sort standard and hashed tables. Sorted tables are sorted by theruntime system.

n You can use key accesses with any table type, but their effect differs. A key access with INSERT hasthe same effect on standard tables as an APPEND, that is the relevant line is appended to the end ofthe table. In a sorted table however, the record is inserted in accordance with the sort sequence. Inthe case of a hashed table, the line is appended, but the system also changes the hash indexinternally.

Page 79: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-31

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV����

,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV

'HILQLWLRQ'HILQLWLRQ

2SHUDWLRQV2SHUDWLRQV

1RWHV�RQ�SHUIRUPDQFH1RWHV�RQ�SHUIRUPDQFH

6SHFLDO�VLWXDWLRQV6SHFLDO�VLWXDWLRQV6SHFLDO�VLWXDWLRQV

Page 80: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-32

SAP AG 2002

,QWHUQDO�7DEOHV�ZLWK�D�+HDGHU�/LQH

'$7$���LWDE�7<3(�7$%/(�2)�VFDUU�������:,7+�'()$8/7�.(<�������:,7+�+($'(5�/,1(�:,7+�+($'(5�/,1(:,7+�+($'(5�/,1( 'DWD�REMHFW�LWDE

'$7$���LWDE�7<3(�VFDUU�2&&856���������:,7+�+($'(5�/,1(�:,7+�+($'(5�/,1(:,7+�+($'(5�/,1(

'$7$���%(*,1�2)�LWDE�2&&856�������FDUULG�7<3(�VBFDUUBLG������������XUO����7<3(�VBFDUUXUO���(1'�2)�LWDE�

2&&8562&&856

2&&8562&&856

6\QWD[�YDULDQW���

6\QWD[�YDULDQW�����REVROHWH�

6\QWD[�YDULDQW�����REVROHWH�%(*,1�2)%(*,1�2)

(1'�2)(1'�2)

LWDELWDE

LWDELWDE

:RUN�DUHD� KHDGHU

%RG\

n 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 sometimessimplifies the syntax for table accesses.

n There are several possible syntax variants used to define internal tables with header lines, some ofwhich are shown in the slide.

n Internal tables with header lines are the oldest form of internal table.

n However, note that

é Many statements have a different effect on internal tables with a header than they would onnormal internal tables. You can then address the body separately using ">@" .([DPSOH�The CLEAR itab statement initializes only the header line, whereas in an internal with noheader, it initializes the content of the entire table.You can delete the body of an internal table with a header using CLEAR itab[] or REFRESHitab.

é You FDQQRW use internal tables with a header line in an object-oriented environment - that is,within classes or interfaces.

Page 81: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-33

SAP AG 2002

6HOHFWLRQ�2SWLRQV�DQG�6HOHFWLRQ�7DEOHV

'$7$���FDUULG�7<3(�VBFDUUBLG�6(/(&7�237,216���VRBFDUU�)25�FDUULG�6(/(&7�237,2166(/(&7�237,216

)25)25

VRBVRBFDUUFDUU

'$7$���VRBFDUU�/,.(����������5$1*(�2)�FDUULG�

( /LQN�WR�VHOHFWLRQ�VFUHHQ( 'DWD�REMHFW

,GHQWLFDOO\�W\SHG�GDWD�REMHFW�ZLWKRXWKHDGHU�RU�OLQN�WR�VHOHFWLRQ�VFUHHQ

5$1*(�2)5$1*(�2)¬%­'®%¯G°2±�²³%´Bµ'¶¸·

¹�ºe»n­'¹6¯G° ¼e²³6´nµ�¶½·

¾ ¹�¿ÁÀeÂ8à ¶·6Ä%Å%Å ­MÆ

Ç ­�® Ç ÀBÂ�à ¶·%Ä6Å�Å ­MÆ

n Use the 6(/(&7�237,21 statement to create an internal table with a header, for which the runtimesystem automatically creates an input dialog for value sets for a selection screen. The systemautomatically inserts the appropriate entries in the internal table, from the user input. For moredetails, refer to the keyword documentation for the 6(/(&7�237,216�statement.

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

Page 82: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-34

SAP AG 2002

8VLQJ�,QWHUQDO�7DEOHV����

,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV,QWURGXFWLRQ�DQG�DGYDQWDJHV�RI�LQWHUQDO�WDEOHV

'HILQLWLRQ'HILQLWLRQ

2SHUDWLRQV2SHUDWLRQV

1RWHV�RQ�SHUIRUPDQFH1RWHV�RQ�SHUIRUPDQFH1RWHV�RQ�SHUIRUPDQFH

6SHFLDO�VLWXDWLRQV6SHFLDO�VLWXDWLRQV

Page 83: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-35

SAP AG 2002

6LQJOH�5HFRUG�$FFHVV��&RPSOHWHO\�DQG�3DUWLDOO\4XDOLILHG�.H\

�5($'�7$%/(�LWDE�,172�ZD���:,7+�7$%/(�.(<�NH\�:,7+�7$%/(�.(<:,7+�7$%/(�.(<

�5($'�7$%/(�LWDE�,172�ZD���:,7+�.(<�NH\�:,7+�.(<:,7+�.(<

�7DEOH�VFDQ

67$1'$5'

&RPSOHWH�SDUW�NH\&RPSOHWH�SDUW�NH\OHIW�DOLJQHG�ZLWKRXW�JDSVOHIW�DOLJQHG�ZLWKRXW�JDSV

%\�TXDOLILHG�NH\%\�TXDOLILHG�NH\

$Q\�FRPSRQHQW$Q\�FRPSRQHQWFRQGLWLRQFRQGLWLRQ

NH\

6257(' +$6+('67$1'$5'NH\

7DEOH�NLQG

7DEOH�NLQG

%LQDU\VHDUFK

7DEOHVFDQ

6257('

7DEOHVFDQ

+$6+('

7DEOH�VFDQ %LQDU\VHDUFK

+DVK�DOJRULWKP

0XVW�EH�FRPSOHWHO\0XVW�EH�FRPSOHWHO\TXDOLILHGTXDOLILHG

n Whenever you want to read individual table lines by declaring a FRPSOHWH key, use the READTABLE ... WITH 7$%/( KEY statement (fastest single record access by key). The runtimesystem supports this syntax variant especially for SORTED and HASHED tables. If the table is aSTANDARD 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 thework area wa and are then use READ TABLE itab FROM wa.

n The runtime system carries out the syntax variant READ TABLE ... WITH KEY (read an entryafter 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 theBINARY SEARCH addition.

6XPPDU\�n Whenever possible, use READ TABLE ... WITH 7$%/( KEY or the variant with a

correspondingly-filled work area.

n If you need to use READ TABLE ... WITH KEY, make your internal table a SORTED table.

Page 84: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-36

SAP AG 2002

/RRS�3URFHVVLQJ�DQG�7DEOH�.LQGV

�/223�$7�LWDE�,172�ZD���:+(5(�ORJBH[SU��������(1'/223�

ORJBORJBH[SUH[SU

�7DEOH�VFDQ

67$1'$5'

)LUVW�Q�NH\�ILHOGV�ILOOHG)LUVW�Q�NH\�ILHOGV�ILOOHGZLWK�� ��ZLWKRXW�JDSVZLWK�� ��ZLWKRXW�JDSV

$Q\�ORJLFDO�H[SUHVVLRQ$Q\�ORJLFDO�H[SUHVVLRQIRU�FROXPQVIRU�FROXPQV

ORJBH[SU

7DEOH�NLQG

%LQDU\�VHDUFK�IRUVWDUWLQJ�SRLQW�WKHQ�ORRS�RQO\WKURXJK�JURXS

OHYHO7DEOH�VFDQ

6257('

7DEOH�VFDQ

+$6+('

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

n SORTED tables are the only exception to this rule. For these, the runtime system optimizes theruntime 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 tothe number of all key fields). As a result, the loop is only performed on the lines that match thecondition in the WHERE clause. Since the table is sorted, the first line can be specified to optimizeperformance at runtime (using a binary search).

Page 85: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-37

SAP AG 2002

3DUWLDO�6HTXHQWLDO�/RRS�7KURXJK�6WDQGDUG�7DEOHV

&$55,' &211,' &,7<)520 ���$$ �� 6$1�)5$1&,6&2 ���/+ ��� ���/+ ���� %(5/,1 ���8$ ��� ���8$ ���� 1(:�<25. ���

'HVLUHG��/RRS�WKURXJK�GDWD�UHFRUGV�ZKHUH�&,7<)520� �)5$1.)857

)5$1.)857

)5$1.)857&$55,' &211,' &,7<)520 ���

/+ ���� %(5/,1 ���/+ ��� ���8$ ��� ���8$ ���� ���$$ �� ���

)5$1.)857

6257�����%<�FLW\IURP���������67$%/(�

)5$1.)8571(:�<25.6$1�)5$1&,6&2

5($'�7$%/(������:,7+�.(<�FLW\IURP� �)5$1.)857��75$163257,1*�12�),(/'6��%,1$5<�6($5&+�VWDUWOLQH� �V\�WDEL[�

$VFHUWDLQ�VWDUWLQJ�SRLQW�

/223�$7�����,172������)520�VWDUWOLQH�WR�HQGOLQH������(1'/223�

ì

5($'�7$%/(�����,172������:,7+�.(<�FLW\IURP� �1(:�<25.��75$163257,1*�12�),(/'6��%,1$5<�6($5&+�HQGOLQH� �V\�WDEL[�

$VFHUWDLQ�HQG�SRLQW�

ó

,QGH[�FRQWUROOHG�ORRS�

n 7R�UHFDS�If you program a loop through a standard table to match a specific field criterion, the runtime systemalways executes a WDEOH�VFDQ.

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

n Since the SORT and READ TABLE statements require additional runtime, this procedure is onlyuseful if the loop can be repeated several times according to the field criterion.

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

Page 86: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-38

SAP AG 2002

$FFHVV�8VLQJ�)LHOG�6\PEROV

�IV�!

�IV�!

5($'�7$%/(�LWDE���$66,*1,1*��IV�!��:,7+�7$%/(�.(<���������5($'�7$%/(��IV�!�FRO���$66,*1,1*��IV�!��:,7+�7$%/(�.(<������

ZD�

ZD�

&RS\

&RS\

FRO� FRO� FRO� FRO� FRO� FRO� FRO� FRO�LWDE

5($'�7$%/(�LWDE���,172�ZD���:,7+�7$%/(�.(<���������5($'�7$%/(�ZD��FRO���,172�ZD���:,7+�7$%/(�.(<������

$66,*1,1*$66,*1,1*

$66,*1,1*$66,*1,1*

,172,172

,172,172

n 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 orequal to 1000 bytes. If you then change the read line using MODIFY, READ ... ASSIGNINGalready improves runtime with a line width of 100 bytes.

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

n Both field symbol variants are much faster than work area variants, in particular when you use nestedinternal 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).

n Always assign a type to field symbols, if you know their static type (again, for performance reasons).

n 1RWH�If you use READ TABLE ... ASSIGNING the field symbol points to the originally assignedtable line, even DIWHU the internal table has been sorted.

n Note that when using field symbols, you cannot change key fields in SORTED or HASHED tables.Trying to do so causes a runtime error.

n The following restrictions apply to LOOP ... ASSIGNING <fs>:

é You cannot use the SUM�statement in control level processing.

é You cannot reassign field symbols within the loop. The statements ASSIGN do TO <fs> andUNASSIGN <fs> will cause runtime errors.

Page 87: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-39

SAP AG 2002

l 'HILQH�DQ�LQWHUQDO�WDEOHl 3HUIRUP�RSHUDWLRQV�RQ�LQWHUQDO�WDEOHVl ,GHQWLI\�WDEOH�NLQGV�DQG�XVH�WKHP�DSSURSULDWHO\�LQ

GLIIHUHQW�VLWXDWLRQV

<RX�DUH�QRZ�DEOH�WR�

8VLQJ�,QWHUQDO�7DEOHV��8QLW�6XPPDU\

Page 88: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-40

�([HUFLVH��8QLW� 8VLQJ�,QWHUQDO�7DEOHV7RSLF� 6LQJOH�&ROXPQ�7DEOHV�RI�WKH�7\SH�VWULQJ

At the conclusion of these exercises, you will be able to:

• Define single-column tables of the data type VWULQJ• Split character strings with internal tables

• Process internal tables in loops

Now, the character string will contain PRUH�WKDQ�RQH�data record.

Extend your program in such a way that the various data records in therows of an internal table are split. Then separate the individual datarecords into single components and output them as before.

3URJUDP� =%&���B��B63/,7B,7$%7HPSODWH� 6$3%&���B'726B63/,7B675,1*0RGHO�VROXWLRQ� 6$3%&���B7$%6B63/,7B,7$%���LV�\RXU�WZR�GLJLW�JURXS�QXPEHU

1-1 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 solution6$3%&���B'726B63/,7B675,1* and give it the new name=%&���B��B63/,7B,7$%.

1-2 Ensure that the function module %&���B*(7B6(3B675,1* creates a characterstring consisting of 30 data records. For this you must assign a suitably typedconstant (suggested name FBQXPEHU) to the parameter LPBQXPEHU.

1-3 Define a single-column internal table of the type 6WDQGDUG. The column componentis to have the data type VWULQJ (suggested name: LWBVHWV).

1-4 Change the 63/,7 statement so that always one partial character string thatcontains a data record is placed into a row of its internal table.

1-5 Ensure that your internal table containing the partial character strings is used for thesubsequent splitting of the individual data record and output of the components in aloop.

Page 89: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-41

([HUFLVH�����2SWLRQDO8QLW� 8VLQJ�,QWHUQDO�7DEOHV7RSLF� 7DEOH�7\SHV

At the conclusion of these exercises, you will be able to:

• Select the appropriate types of internal table

• Process data using internal tables

Now the character string will contain GXSOLFDWH data records.

Extend your program in such a way that the various data records are keptsorted in internal tables. Those data records that appear twice are to besorted into a separate internal table.

3URJUDP� =%&���B��B7$%.,1'7HPSODWH� 6$3%&���B7$%6B63/,7B,7$%0RGHO�VROXWLRQ� 6$3%&���B7$%6B7$%.,1'���LV�\RXU�WZR�GLJLW�JURXS�QXPEHU

2-1 Copy your solution to the last exercise =%&���B��B63/,7B,7$% or thecorresponding model solution 6$3%&���B7$%6B63/,7B,7$% and give it thenew name =%&���B��B7$%.,1'.

2-2 Ensure that the function module %&���B*(7B6(3B675,1* creates a characterstring containing duplicate data records. For this, assign the parameterLPBXQLTXH the value VSDFH.Test your program.

2-3 Define an internal table in such a way that it can contain flight data sorted byairline, flight number, and flight date (suggested name LWBIOLJKWV).Insert the separated data records (contents of the structure ZDBIOLJKW) into thisinternal table, instead of displaying them. (You can still use the output statementlater.)Make use of the return value V\�VXEUF for the ,16(57 statement to ascertainwhether or not a data record appears twice. For this, you must have defined the keyof your internal table accordingly.Define an additional internal table for the data records that appear twice (suggestedname LWBGRXEOHV).Insert the duplicate data records into this internal table.

2-4 Display the contents of both internal tables with the flight data.

Page 90: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-42

([HUFLVH�����2SWLRQDO8QLW� 8VLQJ�,QWHUQDO�7DEOHV7RSLF� 3URFHVVLQJ�'DWD�8VLQJ�,QWHUQDO�7DEOHV

At the conclusion of these exercises, you will be able to:

• Select the appropriate types of internal table

• Process data using internal tables

Extend your program so that it displays the flight data sorted by the flightdate. The data is to be displayed in different colors, according to a keydate selected by the user.

If desired, the data is to be displayed using the SAP Grid Control.

3URJUDP� =%&���B��B352&(66B'$7$7HPSODWH� 6$3%&���B7$%6B7$%.,1'0RGHO�VROXWLRQ� 6$3%&���B7$%6B352&(66B'$7$���LV�\RXU�WZR�GLJLW�JURXS�QXPEHU

3-1 Copy your solution to the last exercise =%&���B��B7$%.,1' or thecorresponding model solution 6$3%&���B7$%6B7$%.,1' and give it the newname =%&���B��B352&(66B'$7$.

3-2 Define a selection screen parameter for entering the key date (suggested nameSDBGDWH).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 themessage 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 aredisplayed in color according to the following criteria: Use the )250$7�&2/25FROB��� statement and load the type group FRO. (You can reverse any color setusing the )250$7�5(6(7 statement.)

• Display flights that occurred in the past with the background colorcol_negative.

• Display flights that occurred between today’s data and the key date with thebackground color col_total.

Page 91: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-43

• Display flights that occurred in the past with the background colorcol_positive.

3-5 Define a selection screen check box for displaying the data with the standard toolSAP Grid Control (suggested name SDBDOY, addition $6�&+(&.%2;).Ensure that the data is only displayed in the usual ABAP list if the user does QRWcheck the SAP Grid Control box.

Otherwise, proceed as follows:

3-5-1 Call the function module %&���B$/9B/,67B287387. Terminate theprogram if the function module raises an exception. (In this case, there is aproblem with the system configuration.)

Detailed information on using the SAP Grid Control is not part ofthis course. For this reason, you use a function module thatencapsulates all the necessary technical details, which you can treatas a “black box.”

For more information on the SAP Grid Control, see:

• The online documentation

• The example programs under(QYLURQPHQW�→�([DPSOHV�→�&RQWURO�([DPSOHV

• The training course %&�����$%$3�'LDORJ�3URJUDPPLQJ�ZLWK(QMR\6$3�&RQWUROV

3-5-2 Find out about the types of the two internal tables that can/must betransferred to the function module as a parameter.Define two corresponding internal tables and an appropriate work area inyour program (suggested names LWBFROBIOLJKWV, LWBFROBGRXEOHV,and ZDBFROBIOLJKW).

3-5-3 The line type of the internal table that you must/can transfer contains acolumn &2/25, in which you can set the color values FROBQHJDWLYH,FROBWRWDO, FROBSRVLWLYH, and FROBEDFNJURXQG for every singleline.Copy the contents of the tables LWBIOLJKWV and LWBGRXEOHV to theinternal tables LWBFROBIOLJKWV�and LWBFROBGRXEOHV respectively.Use two loops to do this, and to fill the &2/25 column.LWBIOLJKWV:

/RJLFDO�FRQGLWLRQ &RORU�YDOXHfldate < sy-datum col_negative

fldate BETWEEN sy-datum AND p_date

col_total

fldate > p_date col_positive

Always use FROBEDFNJURXQG to display LWBGRXEOHV.

Page 92: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-44

6ROXWLRQ��8QLW� 8VLQJ�,QWHUQDO�7DEOHV7RSLF� 6LQJOH�&ROXPQ�7DEOHV�RI�WKH�7\SH�VWULQJ

5(3257��VDSEF���BWDEVBVSOLWBLWDE�

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(����

Page 93: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-45

DATA:

datastring TYPE string,

set_string TYPE string,

wa_flight_c TYPE st_flight_c,

wa_flight TYPE st_flight.

'$7$���LWBVHWV�7<3(�67$1'$5'�7$%/(�2)�VWULQJ����������:,7+�121�81,48(�'()$8/7�.(<����������,1,7,$/�6,=(�FBQXPEHU�

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.

SHIFT datastring BY 2 PLACES IN CHARACTER MODE.

FIND ’##’ IN datastring.

IF sy-subrc <> 0.

MESSAGE a702(bc401).

ENDIF.

��63/,7�GDWDVWULQJ�$7����,172�7$%/(�LWBVHWV�

��/223�$7�LWBVHWV�,172�VHWBVWULQJ�

Page 94: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-46

SPLIT set_string AT ’#’ INTO

wa_flight_c-mandt

wa_flight_c-carrid

wa_flight_c-connid

wa_flight_c-fldate

wa_flight_c-price

wa_flight_c-currency

wa_flight_c-planetype

wa_flight_c-seatsmax

wa_flight_c-seatsocc

wa_flight_c-paymentsum.

MOVE-CORRESPONDING wa_flight_c TO 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.

��(1'/223�

Page 95: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-47

6ROXWLRQ�����2SWLRQDO8QLW� 8VLQJ�,QWHUQDO�7DEOHV7RSLF� 7DEOH�7\SHV

5(3257��VDSEF���BWDEVBWDENLQG�

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.

Page 96: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-48

DATA:

datastring TYPE string,

set_string TYPE string,

wa_flight_c TYPE st_flight_c,

wa_flight 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�������������:,7+�81,48(�.(<�FDUULG�FRQQLG�IOGDWH�������������,1,7,$/�6,=(�FBQXPEHU�

��LWBGRXEOHV�7<3(�6257('�7$%/(�2)�VWBIOLJKW�������������:,7+�121�81,48(�.(<�FDUULG�FRQQLG�IOGDWH�������������,1,7,$/�6,=(�FBQXPEHU�

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.

Page 97: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-49

SHIFT datastring BY 2 PLACES IN CHARACTER MODE.

FIND ’##’ IN datastring.

IF sy-subrc <> 0.

MESSAGE a702(bc401).

ENDIF.

SPLIT datastring AT ’##’ INTO TABLE it_sets.

LOOP AT it_sets INTO set_string.

SPLIT set_string AT ’#’ INTO

wa_flight_c-mandt

wa_flight_c-carrid

wa_flight_c-connid

wa_flight_c-fldate

wa_flight_c-price

wa_flight_c-currency

wa_flight_c-planetype

wa_flight_c-seatsmax

wa_flight_c-seatsocc

wa_flight_c-paymentsum.

MOVE-CORRESPONDING wa_flight_c TO wa_flight.

����,16(57�ZDBIOLJKW�,172�7$%/(�LWBIOLJKWV�����,)�V\�VXEUF��!���������,16(57�ZDBIOLJKW�,172�7$%/(�LWBGRXEOHV�����(1',)�

ENDLOOP.

* output:

*********

��/223�$7�LWBIOLJKWV�,172�ZDBIOLJKW�����:5,7(���������ZDBIOLJKW�FDUULG�������ZDBIOLJKW�FRQQLG�������ZDBIOLJKW�IOGDWH�''�00�<<<<�������ZDBIOLJKW�SULFH�&855(1&<�ZDBIOLJKW�FXUUHQF\�

Page 98: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-50

������ZDBIOLJKW�FXUUHQF\�������ZDBIOLJKW�SODQHW\SH�������ZDBIOLJKW�VHDWVPD[�������ZDBIOLJKW�VHDWVRFF���(1'/223�

��6.,3���:5,7(����GXSOLFDWH�GDWD�UHFRUGV��GRE��&2/25�&2/B+($',1*�

��/223�$7�LWBGRXEOHV�,172�ZDBIOLJKW�����:5,7(���������ZDBIOLJKW�FDUULG�������ZDBIOLJKW�FRQQLG�������ZDBIOLJKW�IOGDWH�''�00�<<<<�������ZDBIOLJKW�SULFH�&855(1&<�ZDBIOLJKW�FXUUHQF\�������ZDBIOLJKW�FXUUHQF\�������ZDBIOLJKW�SODQHW\SH�������ZDBIOLJKW�VHDWVPD[�������ZDBIOLJKW�VHDWVRFF���(1'/223�

Page 99: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-51

6ROXWLRQ�����2SWLRQDO8QLW� 8VLQJ�,QWHUQDO�7DEOHV7RSLF� 3URFHVVLQJ�'DWD�8VLQJ�,QWHUQDO�7DEOHV

5(3257�VDSEF���BWDEVBSURFHVVBGDWD�

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.

Page 100: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-52

CONSTANTS c_number TYPE i VALUE 30.

DATA:

datastring TYPE string,

set_string TYPE string,

wa_flight_c TYPE st_flight_c,

wa_flight 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,

it_col_flights TYPE bc401_t_flights_color,

it_col_doubles LIKE it_col_flights,

wa_col_flight LIKE LINE OF it_col_flights.

3$5$0(7(56���SDBGDWH�/,.(�V\�GDWXP���SDBDOY��$6�&+(&.%2;�'()$8/7�;�

/2$'�2)�352*5$0���SDBGDWH� �V\�GDWXP������

$7�6(/(&7,21�6&5((1���,)�SDBGDWH���V\�GDWXP�����0(66$*(�H����EF����������������������GDWH�LQ�WKH�SDVW��(1',)�

Page 101: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-53

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.

SHIFT datastring BY 2 PLACES IN CHARACTER MODE.

FIND ’##’ IN datastring.

IF sy-subrc <> 0.

MESSAGE a702(bc401).

ENDIF.

SPLIT datastring AT ’##’ INTO TABLE it_sets.

LOOP AT it_sets INTO set_string.

SPLIT set_string AT ’#’ INTO

wa_flight_c-mandt

wa_flight_c-carrid

wa_flight_c-connid

wa_flight_c-fldate

wa_flight_c-price

wa_flight_c-currency

wa_flight_c-planetype

wa_flight_c-seatsmax

wa_flight_c-seatsocc

wa_flight_c-paymentsum.

Page 102: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-54

MOVE-CORRESPONDING wa_flight_c TO wa_flight.

INSERT wa_flight INTO TABLE it_flights.

IF sy-subrc <> 0.

INSERT wa_flight INTO TABLE it_doubles.

ENDIF.

ENDLOOP.

* output:

*********

,)�SDBDOY� �;�

����/223�$7�LWBIOLJKWV�,172�ZDBIOLJKW�������029(�&255(6321',1*�ZDBIOLJKW�72�ZDBFROBIOLJKW�������,)�ZDBFROBIOLJKW�IOGDWH���V\�GDWXP���������ZDBFROBIOLJKW�FRORU� �FROBQHJDWLYH�������(/6(,)�ZDBFROBIOLJKW�IOGDWH���SDBGDWH���������ZDBFROBIOLJKW�FRORU� �FROBWRWDO�������(/6(���������ZDBFROBIOLJKW�FRORU� �FROBSRVLWLYH�������(1',)�������,16(57�ZDBFROBIOLJKW�,172�7$%/(�LWBFROBIOLJKWV�����(1'/223�

����/223�$7�LWBGRXEOHV�,172�ZDBIOLJKW�������029(�&255(6321',1*�ZDBIOLJKW�72�ZDBFROBIOLJKW�������ZDBFROBIOLJKW�FRORU� �FROBEDFNJURXQG�������,16(57�ZDBFROBIOLJKW�,172�7$%/(�LWBFROBGRXEOHV�����(1'/223�

����&$//�)81&7,21�%&���B$/9B/,67B287387������(;3257,1*��������LWBOLVW������� �LWBFROBIOLJKWV��������LWBOLVW������� �LWBFROBGRXEOHV������(;&(37,216��������FRQWUROBHUURU� ����������27+(56�������� �������,)�V\�VXEUF��!���������0(66$*(�D����EF���������(1',)�

Page 103: 003 - Bc401 - Abap Objects

© SAP AG BC401 3-55

��(/6(� LOOP AT it_flights INTO wa_flight.

������,)�ZDBIOLJKW�IOGDWH���V\�GDWXP���������)250$7�&2/25� �FROBQHJDWLYH�������(/6(,)�ZDBIOLJKW�IOGDWH���SDBGDWH���������)250$7�&2/25� �FROBWRWDO�������(/6(���������)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.

Page 104: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-1

SAP AG 2002

l 7HFKQLTXHV�IRU�FDOOLQJ�SURJUDPVl 0HPRU\�PRGHOl 7HFKQLTXHV�IRU�SDVVLQJ�GDWDl 8VH

&RQWHQWV�

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD

Page 105: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-2

SAP AG 2002

l 'HVFULEH�WKH�6$3�5���PHPRU\�PRGHOl &DOO�H[HFXWDEOH�SURJUDPVl &DOO�WUDQVDFWLRQVl 8VH�WKH�YDULRXV�PHPRU\�DUHDV�WR�SDVV�GDWD

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD��8QLW�2EMHFWLYHV

Page 106: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-3

SAP AG 2002

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD�,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

����� � � ���� � � ����� � ��� � � � � ����� ��� ���� � � � ��� � � � ��� � � ��� � � � � ��� � � ������ ���! ��" #%$ ��&(') &�* ' # �

+ & " & )(,%- # � & �/.+ & " &�0 *21 #(3 " � � �+ # " & � ' 4�5�6�798�:<;�= : 7 =�> :%?

Page 107: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-4

SAP AG 2002

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD����

&DOOLQJ�SURJUDPV&DOOLQJ�SURJUDPV

0HPRU\�PDQDJHPHQW0HPRU\�PDQDJHPHQW

3DVVLQJ�GDWD3DVVLQJ�GDWD

Page 108: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-5

SAP AG 2002

&DOOLQJ�3URJUDPV

22

11

0DLQPHPRU\

22

11

0DLQPHPRU\

11

1HZ�SURJUDP

(QG�LQVHUW

����

3URJUDP��3URJUDP��

7LPH

,QVHUWLRQ

5HVWDUW

,QVHUW�SURJUDP

� There are two ways of starting an ABAP program from another ABAP program that is alreadyrunning:

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

• The current program is terminated and the called program is started.

� Complete ABAP programs within a single user session can only run sequentially. We refer to thistechnique as VHTXHQWLDO�FDOOLQJ.

� If you want to run functions in parallel, you must use function modules. For further informationabout this technique, refer to the course %&�����&RPPXQLFDWLRQ�,QWHUIDFHV�LQ�$%$3��or thedocumentation for the &$//�)81&7,21�����67$57,1*�1(:�7$6.���� statement.

Page 109: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-6

SAP AG 2002

&DOOLQJ�DQ�([HFXWDEOH�3URJUDP

3URJUDP��

���68%0,7�SURJBQDPHB�����

���68%0,7�SURJBQDPHB����$1'�5(7851����

���68%0,7�SURJBQDPHB���9,$�6(/(&7,21�6&5((1��$1'�5(7851�����

SURJBQDPHB�352*5$0�������

/LVW

6HOHFWLRQ�6FUHHQ

3URJUDP��

)�

)�)�

SURJBQDPHB�352*5$0�������

/LVW

)�

SURJBQDPHB�352*5$0�������

/LVWH

,QVHUWLRQ

5HVWDUW

,QVHUWLRQ

� To start an executable program, use the 68%0,7 statement.

� If you use the 9,$�6(/(&7,21�6&5((1 addition, the system displays the standard selectionscreen of the program (if one has been defined).

� If you use the $1'�5(7851 addition, the system resumes processing with the first statement afterthe 68%0,7 statement once the called program has finished.

� For further information, refer to the documentation for the 68%0,7 statement.

Page 110: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-7

SAP AG 2002

&DOOLQJ�D�7UDQVDFWLRQ

6$30�WBQDPH

���/($9(�352*5$0����

3URJUDP��

���/($9(�72�75$16$&7,21 ’T_CODE’>$1'�6.,3�),567�6&5((1@����

���6FUHHQ

���6FUHHQ)��

3URJUDP����7UDQVDFWLRQ TCODETCODE

6$30�WBQDPH

���/($9(�352*5$0����

���&$//�75$16$&7,21 ’T_CODE’>$1'�6.,3�),567�6&5((1@����

���6FUHHQ

���6FUHHQ)��

,QVHUWLRQ

5HVWDUW

� With the /($9(�72�75$16$&7,21�T_CODE statement you terminate the current programand start the transaction with transaction code 7B&2'(� The statement is the equivalent to entering�Q<T_CODE> in the command field.

� &$//�75$16$&7,21 ’T_CODE’ allows you to insert ABAP programs that have a transactioncode.

� To terminate an ABAP program, use the /($9(�352*5$0 statement. If the statement is used in aprogram that you called using &$//�75$16$&7,21 ’T_CODE’ or 68%0,7�prog_name$1'�5(7851, the system resumes processing at the next statement after the call in the callingprogram.Otherwise, the user returns to the application menu from which he or she started the program.

� If you use the�����$1'�6.,3�),567�6&5((1 addition, the system does QRW display theVFUHHQ�FRQWHQWV of the first screen. However, it does process the flow logic.

� If the transaction T_CODE you called with &$//�75$16$&7,21 uses update techniques, you canuse the 83'$7(��� addition to specify the update technique (asynchronous (default),synchronous, or local) that the program should use. For further information, refer to course %&����3URJUDPPLQJ�'DWDEDVH�8SGDWHV� and the online documentation.

Page 111: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-8

SAP AG 2002

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD����

&DOOLQJ�SURJUDPV&DOOLQJ�SURJUDPV

0HPRU\�PDQDJHPHQW0HPRU\�PDQDJHPHQW0HPRU\�PDQDJHPHQW

3DVVLQJ�GDWD3DVVLQJ�GDWD

Page 112: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-9

SAP AG 2002

/RJLFDO�0HPRU\�0RGHO

([WHUQDO�VHVVLRQ��ZLQGRZ���$%

$3�P

HPRU

\��

,QWHUQDO�VHVVLRQ����

3URJUDP����

([WHUQDO�VHVVLRQ��ZLQGRZ���

$%$3

�PHP

RU\��

,QWHUQDO�VHVVLRQ����

3URJUDP����

6$3�PHPRU\

8VHU�WHUPLQDO�VHVVLRQ

,QWHUQDO�VHVVLRQ����

3URJUDP����

� The way in which the main memory is organized from the program’s point of view can berepresented in the above logical model. There is a distinction between internal and externalsessions:

• Generally, an H[WHUQDO�VHVVLRQ is connected to an R/3 window. You can create a new sessionby choosing 6\VWHP�→�1HZ�6HVVLRQ or by entering �R<T_CODE> in the command field. Youcan have up to six external sessions open simultaneously in one terminal session.

• External sessions are subdivided into LQWHUQDO�VHVVLRQV (placed on a VWDFN). Each programthat you run occupies its own internal session. Each external session can contain up to nineinternal sessions.

� Data for a program is RQO\�YLVLEOH�ZLWKLQ�an internal session. The visibility of the data is generallyrestricted to the relevant program.

� The following slides illustrate how the stack inside an external session changes with variousprogram calls.

Page 113: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-10

SAP AG 2002

,QVHUWLQJ�D�3URJUDP����

External session (window) 1

AB

AP

mem

ory

1

,QWHUQDO�VHVVLRQ����

3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

,QWHUQDO�VHVVLRQ����

3URJUDP����

SAP memory

User terminal session

Page 114: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-11

SAP AG 2002

,QVHUWLQJ�D�3URJUDP����

External session (window) 1

AB

AP

mem

ory

1

,QWHUQDO�VHVVLRQ����

3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

,QWHUQDO�VHVVLRQ����

3URJUDP����

SAP memory

User terminal session

,QWHUQDO�VHVVLRQ����

3URJUDP����3URJUDP����

,QVHUWLRQ

� When you insert a program, the system creates a new internal session, which in turn creates a newprogram context.

� The new session is placed on the stack The program context of the calling program also remainsintact.

Page 115: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-12

SAP AG 2002

7HUPLQDWLQJ�WKH�,QVHUWHG�3URJUDP

External session (window) 1

AB

AP

mem

ory

1

,QWHUQDO�VHVVLRQ����

3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

,QWHUQDO�VHVVLRQ����

3URJUDP����

SAP memory

User terminal session

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

Page 116: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-13

SAP AG 2002

5HVWDUWLQJ�DQ�([HFXWDEOH�3URJUDP����

External session (window) 1

AB

AP

mem

ory

1

,QWHUQDO�VHVVLRQ����

3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

SAP memory

User terminal session

,QWHUQDO�VHVVLRQ����

3URJUDP����

,QWHUQDO�VHVVLRQ����

3URJUDP����

,QWHUQDO�VHVVLRQ����

3URJUDP����

� When you end a program and start a new one, there is a distinction between calling an executableprogram and calling a transaction, with regard to memory areas.

Page 117: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-14

SAP AG 2002

5HVWDUWLQJ�DQ�([HFXWDEOH�3URJUDP����

External session (window) 1

AB

AP

mem

ory

1

,QWHUQDO�VHVVLRQ����

3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

SAP memory

User terminal session

,QWHUQDO�VHVVLRQ����

3URJUDP����

,QWHUQDO�VHVVLRQ����

3URJUDPP3URJUDPP��������

,QWHUQDO�VHVVLRQ����

3URJUDP����

5HVWDUW

� If you call an executable program using its program name (terminating the calling program), thesystem deletes the internal session of the program that you are terminating (the top one from thestack).

� The system creates a new internal session, which in turn creates the program context of the calledprogram.

� The new session is placed on the stack Existing program contexts remain intact. The topmostinternal session on the stack is replaced.

Page 118: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-15

SAP AG 2002

5HVWDUWLQJ�D�7UDQVDFWLRQ����

([WHUQDO�VHVVLRQ��ZLQGRZ���$%

$3�P

HPRU

\��

,QWHUQDO�VHVVLRQ����

3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

SAP memory

User terminal session

,QWHUQDO�VHVVLRQ����

3URJUDP����

,QWHUQDO�VHVVLRQ����

3URJUDP����

,QWHUQDO�VHVVLRQ����

3URJUDP����

Page 119: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-16

SAP AG 2002

5HVWDUWLQJ�D�7UDQVDFWLRQ����

([WHUQDO�VHVVLRQ��ZLQGRZ���$%

$3�P

HPRU

\��

,QWHUQHU�0RGXV����

3URJUDP����3URJUDP����

External session (window) 2

AB

AP

mem

ory

2

SAP memory

User terminal session

,QWHUQDO�VHVVLRQ����

3URJUDP����

5HVWDUW�FRPSOHWH

LQLWLDOL]DWLRQ�

� If you start a program using its WUDQVDFWLRQ�FRGH�(that is, if one was defined), DOO�of the internalsessions on the stack are deleted.

� The system creates a new internal session, which in turn creates the program context of the calledprogram.

� After the call, the ABAP memory is LQLWLDOL]HG.

Page 120: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-17

SAP AG 2002

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD����

&DOOLQJ�SURJUDPV&DOOLQJ�SURJUDPV

0HPRU\�PDQDJHPHQW0HPRU\�PDQDJHPHQW

3DVVLQJ�GDWD3DVVLQJ�GDWD3DVVLQJ�GDWD

Page 121: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-18

SAP AG 2002

2YHUYLHZ��3DVVLQJ�'DWD�%HWZHHQ�3URJUDPV

3URJUDP�$ 11

44

DBDB

,QWHUIDFH

6$3�PHPRU\�6(7�*(7�SDUDPHWHUV�

$%$3�PHPRU\22

33

3URJUDP�%

55

� There are various ways of passing data to programs running in separate internal sessions:

� You can use:

� The interface of the called program (usually a standard selection screen)

ó ABAP memory

ì SAP memory

ö Database tables

ú Local files on your presentation server

� 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$'.

Page 122: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-19

SAP AG 2002

3DVVLQJ�'DWD�8VLQJ�WKH�3URJUDP�,QWHUIDFH

3URJUDP�$

Dat

aD

ata 3URJUDP�%

6WDQGDUG�VHOHFWLRQ�VFUHHQ

� When you call ABAP programs that have a standard selection screen, you can pass data for theinput 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

Page 123: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-20

SAP AG 2002

3UHDVVLJQLQJ�9DOXHV�IRU�,QSXW�)LHOGV

'$7$�VHW�7<3(_/,.(�5$1*(�2)�W\SH_GDWDREMHFW`�68%0,7�SURJBQDPH�$1'�5(7851�>9,$�6(/(&7,21�6&5((1@:,7+�SDUDPHWHU���(4_1(_����YDO:,7+�VHOBRSW�����(4_1(_����YDO��������6,*1�,_(`:,7+�VHOBRSW�����%(7:((1�YDO��$1'�YDO���6,*1�,_(`:,7+�VHOBRSW�127�%(7:((1�YDO��$1'�YDO���6,*1�,_(`:,7+�VHOBRSW�,1�VHW�����

@BADC�E<F GIH�JDGKGLE�F(A

M2M2M

N�O P Q R(S TUO O Q R V WYX/Z�[�\^]

M2M2MM2M2M

3DWWHUQ�IRU�68%0,7VWDWHPHQW�LQVHUWV�WKHDSSURSULDWH�IUDPH�IRU�WKHSURJUDP�FDOO�LQWR�WKHVRXUFH�FRGH

� The :,7+ addition to the 68%0,7 statement allows you to preassign values for parameters andselection 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*( statementinstead of individual :,7+�additions. The RANGES statement creates a selection table, which youcan fill as though it were a selection option. You then pass the whole table to the executableprogram.

� If you want to display the standard selection screen when you call the program, use the 9,$6(/(&7,21�6&5((1 addition.

� When you use the 68%0,7 statement, use the Pattern function in the ABAP Editor to insert anappropriate statement pattern for the program you want to call. It automatically supplies the namesof 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.

Page 124: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-21

SAP AG 2002

$%$3�0HPRU\�DQG�6$3�0HPRU\

([WHUQDO�VHVVLRQ��ZLQGRZ���$%

$3�P

HPRU

\��

,QWHUQDO�VHVVLRQ����

3URJUDP����

([WHUQDO�VHVVLRQ��ZLQGRZ���

$%$3

�PHP

RU\��

,QWHUQDO�VHVVLRQ����

3URJUDP����

6$3�PHPRU\��6(7��*(7��SDUDPHWHUV�6$3�PHPRU\��6(7��*(7��SDUDPHWHUV�

8VHU�WHUPLQDO�VHVVLRQ

,QWHUQDO�VHVVLRQ����

3URJUDP����

,QWHUQDO�VHVVLRQ����

3URJUDP����

� You can use SAP memory and ABAP memory to pass data between programs.

n 6$3�PHPRU\ is a user-specific memory area for storing ILHOG�YDOXHV. It is only of limited value forpassing data between internal sessions. Values in SAP memory are retained for the duration of theuser’s terminal session. The memory can be used between sessions in the same terminal session. Youcan use the contents of SAP memory as default values for screen fields. All H[WHUQDO�VHVVLRQV canuse the SAP memory.

n $%$3�PHPRU\ is also user-specific. There is a local ABAP memory for each external session. Youcan 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 ABAPmemory is automatically initialized or released.

Page 125: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-22

SAP AG 2002

3DVVLQJ�'DWD�8VLQJ�WKH�$%$3�0HPRU\

352*5$0�S�������'$7$��S�BVSIOL�7<3(�VSIOL�������LWBVSIOL�7<3(�67$1'$5'�7$%/(������2)�VSIOL����

352*5$0�S�������'$7$��S�BVSIOL�7<3(�VSIOL�������LWBVSIOL�7<3(�67$1'$5'�7$%/(������2)�VSIOL����

ZDBIOL

LWBVSIOL

0<B,'$%$3�PHPRU\

0<B,'� ���

(;3257(;3257��ZD��ZDBBIOL�)520�S�BVSIOLIOL�)520�S�BVSIOL��LWBVSIOL��LWBVSIOL��72�0(025<�,'�0<B,'���72�0(025<�,'�0<B,'�

,03257,03257��ZD��ZDBBIOL�72�S�BVSIOLIOL�72�S�BVSIOL��LWBVSIOL��LWBVSIOL��)520�0(025<�,'�0<B,'���)520�0(025<�,'�0<B,'�

,QWHUQDO�VHVVLRQ��

,QWHUQDO�VHVVLRQ��

� The (;3257�����72�0(025< statement allows you to copy any number of ABAP data objectswith 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 willoverwrite the old.

� The ,03257�����)520�0(025<�,'���� statement allows you to copy the data from theABAP 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 sametypes 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 ABAPmemory to pass data ZKHQ�LQVHUWLQJ�(&$//�75$16$&7,21).

Page 126: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-23

SAP AG 2002

3DVVLQJ�3DUDPHWHUV�8VLQJ�WKH�6$3�0HPRU\

6$3�PHPRU\6$3�PHPRU\&$5&$5 /+

Program AProgram A

$LUOLQH&RQQHFWLRQ

/+ RU

RU

Program BProgram B

$LUOLQH&RQQHFWLRQ

/+

6(7_

*(7_

&21&21 ���

���

���

6(7�3$5$0(7(5�,'�&21�),(/'�VG\QBFRQQ�FRQQLG�&$//�75$16$&7,21������

*(7�3$5$0(7(5�,'�&21�),(/'�VG\QBFRQQ�FRQQLG�

� 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 theparameter 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 theparameter (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 forthe 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 withvalues.

Page 127: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-24

SAP AG 2002

3UHYLHZ��3DVVLQJ�'DWD�8VLQJ�DQ�,QWHUQDO�7DEOH

6$30�WBQDPH���/($9(�352*5$0����

�VW�VFUHHQ �QG�VFUHHQ)��

3URJUDP����7UDQVDFWLRQ T_CODET_CODE3URJUDP�����'$7$���ELBLWDE�7<3(�7$%/(�2)�EGFGDWD���ELBZD���7<3(�EGFGDWD� �ILOO�ELBLWDE��� �FDOO�RWKHU�SURJUDP&$//�75$16$&7,21�7B&2'(��86,1*�ELBLWDE�,)�V\�VXEUF� ������(/6(����

ELBLWDE

� The &$//�75$16$&7,21�T_CODE�86,1*�bi_itab statement allows you to insert thetransaction T_CODE, and the screens are processed according to the internal table bi_itab.

� 7KLV�LQWHUQDO�WDEOH�PXVW�EH�W\SHG�DFFRUGLQJ�WR�WKH�VWUXFWXUH�EGFGDWD�DQG�ILOOHGDSSURSULDWHO\�

� The 02'( addition allows you to specify whether the screen contents should all be displayed (’A’- the default setting), only when an error occurs (’E’), or not at all (’N’).

� The 0(66$*(6�,172�mess_itab addition is used to specify where the system messages sentduring the execution of the called transaction are written.

� 7KH�LQWHUQDO�WDEOH�PXVW�EH�W\SHG�DFFRUGLQJ�WR�WKH�VWUXFWXUH�EGFPVJFROO�� You can find out if the transaction was executed successfully from the system field V\�VXEUF.

� This technique is useful if, for example:

• You are processing in the foreground, but the input fields have not been filled using GETparameters

• You want to process the transaction in the background. In this case, you normally have to passfunction 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.

Page 128: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-25

SAP AG 2002

)LHOGV�RI�WKH�*OREDO�7\SH�EGFGDWD

IQDPIQDPG\QSURG\QSURSURJUDPSURJUDP

'HVFULSWLRQ

G\QEHJLQG\QEHJLQ

`IF^a�bcFLJedfA�J<dgE

)LHOG�QDPH)LHOG�QDPH IYDOIYDO

������������ ��/HQJWK/HQJWK ������

1RWH�ZKHQILOOLQJ

h A�i jlk Alm�C�GFnE�oDacF^pqarAsC�o<FnEDE<A

t o<F E�E�AuA�vedxw�E�F y<kBF C�GIF E�o�acFnp y<k E<i pxAeJ�dgE y<k E<i pgz�J�i9v�E

h A�i jxk Axm�C�GFnE�oDacF^pqaIAsC�oeFnEDE<A

{ |}{�~^acF�m�C�GFnE�oDarFnpqarAC�oeFnE�E<Ae�

aeG%��E�F2��k C�E�{�{

� J�C�E��C�E�ADC�k G�k zeE

� Filling the internal table in batch input format:

• Each screen that is to be processed and filled automatically in the transaction must be identifiedby 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 youwant to fill. These records use the table fields IQDP and IYDO. The following fields can befilled:

- Input/output fields, with data- The command field (EGFBRNFRGH), with function codes- The cursor position field (EGFBFXUVRU), with field names.

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

Page 129: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-26

SAP AG 2002

([DPSOH��3DVVLQJ�'DWD�8VLQJ�DQ�,QWHUQDO�7DEOH

IQDPIQDPG\QSURG\QSURSURJUDPSURJUDP G\QEHJLQG\QEHJLQ�����D���r�<�����������D�����e�<���I�e���������D�<���I�<�

IYDOIYDO���e��� �

���������<������ ��<�r�¢¡�£�¤r¥<¥e¦<§r¨���§<©Dªc¦!«�<�D�����<���¬���r­D��®¯¡�£�¤c¥<¥e¦e§�¨���£�°�¨�±c«�<�!���r�<²������!� ���<³r�

'$7$���ZDBEGFGDWD�7<3(�EGFGDWD���LWBEGFGDWD�/,.(�7$%/(�2)�ZDBEGFGDWD� �ILOO�WKH�EGFGDWD�WDEOH����

&$//�75$16$&7,21�%&���B&$/'B&5(B&867�����������������86,1*�LWBEGFGDWD�����������������02'(�1�,)�V\�VXEUF��!�����0(66$*(�����:,7+�V\�VXEUF�(1',)�

EGFGDWDEGFGDWD

86,1*86,1*02'(02'(

,'�IRU�FRPPDQGILHOG

6DYH

� This example refers to the transaction %&���B&$/'B&7$B8. If you request the creation of a newcustomer entry here, the transaction %&���B&$/'B&5(B&867 is inserted. This transaction hasnot implemented an import from the ABAP memory, and its input fields are not set as *(7parameters. The customer data is therefore passed using an internal table and the transactionprocessed 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 thecustomer 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 istriggered by the user choosing a function key, pushbutton, or menu entry during the dialog flow (orby entering a code directly in the command field).

Page 130: 003 - Bc401 - Abap Objects

© SAP AG BC401 4-27

SAP AG 2002

l 'HVFULEH�WKH�6$3�5���PHPRU\�PRGHOl &DOO�H[HFXWDEOH�SURJUDPVl &DOO�WUDQVDFWLRQVl 8VH�WKH�YDULRXV�PHPRU\�DUHDV�WR�SDVV�GDWD

<RX�DUH�QRZ�DEOH�WR�

&DOOLQJ�3URJUDPV�DQG�3DVVLQJ�'DWD��8QLW�6XPPDU\

Page 131: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-1

SAP AG 2002

l 3URFHGXUDO�SURJUDPPLQJl 2EMHFW�RULHQWHG�SURJUDPPLQJl $LPV�RI�WKH�$%$3�2EMHFWV�SURJUDPPLQJ�ODQJXDJH

&RQWHQWV�

,QWURGXFWLRQ�WR�2EMHFW�2ULHQWHG�3URJUDPPLQJ

Page 132: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-2

SAP AG 2002

l 1DPH�WKH�PRVW�VLJQLILFDQW�GLIIHUHQFHV�EHWZHHQSURFHGXUDO�DQG�REMHFW�RULHQWHG�SURJUDPPLQJ

l 6WDWH�WKH�DLPV�EHKLQG�GHYHORSLQJ�WKH�$%$32EMHFWV�SURJUDPPLQJ�ODQJXDJH

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

,QWURGXFWLRQ�WR�2EMHFW�2ULHQWHG�3URJUDPPLQJ��8QLW2EMHFWLYHV

Page 133: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-3

SAP AG 2002

,QWURGXFWLRQ�WR�2EMHFW�2ULHQWHG�3URJUDPPLQJ�,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������������ ��� � � � ��������� ��� � � � � ��� � �� ����� ��� � � � � ������ ��� � � � � � � � ������� � � � � � � ��� � �� ����� ��� � � � � � ! "$# # % &('*)�+-, ' + "$.0/" &�1 ) " / / % & '*2 "$3 "465�7 8(9;: 8(< =�> 8�?$@A ?�B @ = 5C ? < ? A$D�E = 5 ? 8GFC ? < ?�H BJI =$K < 5 7 8C = < ? 7 @ LNMPOGQ R$S�TVU S Q U$W S�X

Page 134: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-4

SAP AG 2002

+LVWRU\�RI�3URJUDPPLQJ�/DQJXDJHV

Machine language

Assembler

Y[Z]\^Z

_ `[`

$%$3�2EMHFWV

acb*acd

n Before ABAP, SAP used a macro assembler.

n $%$3 was created with the intention of improving reporting. ABAP developed almostindependently as an in-house programming language, although it was influenced by otherprogramming languages, for example, COBOL and PASCAL.

n $%$3�2EMHFWV is a true extension of ABAP. ABAP Objects unites the most promising aspects ofother object-oriented programming languages, such as Java, C++, and Smalltalk.

Page 135: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-5

SAP AG 2002

,QWURGXFWLRQ��3URJUDPPLQJ�0RGHOV����

3URFHGXUDO�SURJUDPPLQJ3URFHGXUDO�SURJUDPPLQJ

2EMHFW�RULHQWHG�SURJUDPPLQJ2EMHFW�RULHQWHG�SURJUDPPLQJ

Page 136: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-6

SAP AG 2002

&KDUDFWHULVWLFV�RI�3URFHGXUDO�3URJUDPPLQJ

&KDUDFWHULVWLFV�l 6HSDUDWLRQ�RI�IXQFWLRQV�DQG�GDWDl 3RVVLELOLW\�RI�HQFDSVXODWLQJ

IXQFWLRQV�XVLQJ�PRGXODUL]DWLRQl 'LUHFW�DFFHVV�WR�GDWD�SRVVLEOH��

YLVLELOLW\"

e^f�ghfe0f�gif e[fPghfe0f�gif

e0f�gife^fPghf e[fPghfe^f�g�f

e0f�ghfe^f�gif

j�kGlGm�n o p6lj�kGlGmqnro p�ljPkGlGmqnro pPlj(k(l(mqn-o pPlj�kGlGmqnro p�ljPkGlGmqn-o pPlj�kGl(m�n o p6lj�kGlGm�n o p6l

jPkGlGm�n o p6lj�kGlGm�n o p6lj�kGlGm�n o p6ljPkGlGmqnro pPlj�kGlGm�n-o p�ljPkGlGm�n o p6lj�kGlGm�n o p6ljPkGlGm�n-o p6l

n Information systems were previously defined primarily by their functions: Data and functions werestored separately and linked using input-output relationships.

Page 137: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-7

SAP AG 2002

5(3257�=$%$3B'(02� ����������������������7<3(6�����

'$7$�����

���

3(5)250�IRUP�����

&$//�)81&7,21�)%����&$//�)81&7,21�)%����

����������������������)250�I�����������(1')250�

5(3257�=$%$3B'(02� ����������������������7<3(6�����

'$7$�����

���

3(5)250�IRUP�����

&$//�)81&7,21�)%����&$//�)81&7,21�)%����

����������������������)250�I�����������(1')250�

7\SLFDO�$%$3�3URJUDP

l 7\SH�GHILQLWLRQVl 'DWD�GHFODUDWLRQV

l 0DLQ�SURJUDP� &DOOLQJ�VXEURXWLQHV� &DOOLQJ�IXQFWLRQ�PRGXOHV

l 'HILQLWLRQ�RI�VXEURXWLQHV

n A typical ABAP program consists of type definitions and data declarations, which describe theblueprint of the data the program uses when it is executed.

n To make your program more readable and for better program structure, it is recommended that youwork with modularization units (encapsulated units with functions), such as form routines orfunction modules. These components can be reused in many different programs.

Page 138: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-8

SAP AG 2002

$%$3�0DLQ�0HPRU\�DQG�)XQFWLRQ�*URXSV

,QWHUQDO�VHVVLRQ,QWHUQDO�VHVVLRQ

sutJvwyx*t{zx |ix}vw�~N�{��|i���J�� x{�J�����Pv�y�(x �

�;�V�N�J�V� g�� �����

�y� ���|-�Jvy�����v � ���

)XQFWLRQ�JURXS��

� � ����g�� � ��� �]�[�N  �¢¡� � ����g�� � ��� �]�[�N  � £

)XQFWLRQ�JURXS��

� � ����g�� � ��� �]�[�N  �¢¡� � ����g�� � ��� �]�[�N  � £� � �6�Pg�� � ��� �V�V�V  ��¤ ¤ ¤

¥�¦ §�¨�©�¦NªN©�«-©

¥;¦ §�¨�©�¦Nª�©�« ©InterfaceInterface

InterfaceInterface

n This slide provides an overview of the main program components during program execution in aninternal session (main memory area on application server).

n The loaded main program and the two function groups with their encapsulated modules (the functionmodules) are both in separate memory areas.

n From the main program, you can use function modules to access function group components, forexample their global data. Therefore, a function group is a unit consisting of data and functions thatmanage this data.

n A user "client" (here, the main program) can only access the function groups and their services usingthe interface, that is the function modules. The function group acts as a "server", because it providesservices.

Page 139: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-9

SAP AG 2002

)XQFWLRQ�*URXS�([DPSOH

)81&7,21�322/�VBYHKLFOH�

�VSHHG�LV�D�JOREDO�YDULDEOH �XVHG�LQ�WKH�IXQWLRQ�SRRO'$7$��VSHHG�7<3(�,��������������)81&7,21�,1&B63(('��������$''�LPSBVSHHG�72�VSHHG�(1')81&7,21�

)81&7,21�'(&B63(('��������68%75$&7�,03B63(('�IURP�VSHHG�(1')81&7,21�

)81&7,21�*(7B63(('���H[SBVSHHG� �VSHHG�(1')81&7,21�

���

)81&7,21�322/�VBYHKLFOH�

�VSHHG�LV�D�JOREDO�YDULDEOH �XVHG�LQ�WKH�IXQWLRQ�SRRO'$7$��VSHHG�7<3(�,��������������)81&7,21�,1&B63(('��������$''�LPSBVSHHG�72�VSHHG�(1')81&7,21�

)81&7,21�'(&B63(('��������68%75$&7�,03B63(('�IURP�VSHHG�(1')81&7,21�

)81&7,21�*(7B63(('���H[SBVSHHG� �VSHHG�(1')81&7,21�

���

¬®­y¯±° ²�³�´¯¶µu·0´¸­y¹»º�³(²�¼½ ­y¯®°�²�³�´¯u¾ ½ ´y·�°�´¯®²�·�´¿À¿�³q¯�µ

²�¼®Á¾ ¹±Á�Á ö´ ½yÄ ° Ä ·

6B9(+,&/(

� ���GÅ��VÆ���� �� �P�(ÅN�VÆP��� �Ç �Pg Å��]Æ��P� ���g � Æ

VSHHG

n The function group s_vehicle provides a user or client with the services inc_speed,dec_speed, and get_speed.

n These services make up the function group interface and access the internally encapsulatedcomponent speed.

Page 140: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-10

SAP AG 2002

5(3257�]YHKLFOHGHPR�

7<3(6�����

'$7$��ZDBFDU�7<3(����

�QR�GLUHFW�DFFHVV�WR�VSHHG

�XVH�IXQFWLRQV�RI�SRRO

&$//�)81&7,21�,1&B63(('������������������

&$//�)81&7,21�*(7B63(('������������������

&$//�)81&7,21�6723��������������������������������

5(3257�]YHKLFOHGHPR�

7<3(6�����

'$7$��ZDBFDU�7<3(����

�QR�GLUHFW�DFFHVV�WR�VSHHG

�XVH�IXQFWLRQV�RI�SRRO

&$//�)81&7,21�,1&B63(('������������������

&$//�)81&7,21�*(7B63(('������������������

&$//�)81&7,21�6723��������������������������������

8VH�RI�)XQFWLRQ�*URXS

� ���GÅ��VÆ���� �� �P�(ÅN�VÆP��� �Ç �Pg Å��]Æ��P� ���g � Æ

È Ä ³À¯¶¹�·0´µu· Ä�É ²�¼ Ä ²�­¾ÊÁ*¾½ ­¯�°y²�³�´¯ É ´�Ã�­y¿(Á;¾Ë´ ½ ²�¼u³À¾

½ ­y¯±° ²�³(´y¯¶µ¸·�´¸­y¹

6B9(+,&/(

VSHHG

n The main program cannot access speed directly.

Page 141: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-11

SAP AG 2002

6HYHUDO�,QVWDQFHV�RI�2QH�)XQFWLRQ�*URXS

6B9(+,&/(

� ���GÅ��VÆ���� �� ���GÅN�VÆ6��� �Ç �Pg Å���Æ��6� ���g � Æ

VSHHG

Ì ³À¾�¹�¿ Ä�Í ³À¯uµ Ä ¯±­ ɶΠÁ;·}´ ½Ï Á;¼u³(°*¿(Á*¾ Ä ¯®Ã¶²�¼�Á;³À·±¾y¹�Á�ÁyÃгq¾

´y¯�¿ Í ¹®´y¾y¾�³ Î ¿(ÁѺ�³(²�¼Ä Ã�Ãy³�²�³�´¯ Ä ¿ Ä Ã É ³À¯�³À¾Ê²�· Ä ²�³�´y¯

n If the main program is to work with several vehicles, this is not possible without extra programmingand administration effort.

Page 142: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-12

SAP AG 2002

,QVWDQWLDWLRQ�DQG�2EMHFW�2ULHQWHG�/DQJXDJHV

VSHHGVSHHG

VSHHG

VSHHG

È ­y¿(²�³q¹u¿(Á³À¯�¾c² Ä ¯±° Á;¾ËÒG´ Î�Ó Á�°�²�¾;Ô´ ½ ²�¼�Á¾ Ä�É Á Î ¿(­uÁ�¹u·�³q¯±²uÒ(² Í ¹®ÁÑ´·

°;¿ Ä ¾y¾;Ô Ä ·0Á Ä˽ ­y¯±Ã Ä�É Á*¯±² Ä ¿Ä ²(²�·�³ Î ­¸²(ÁÑ´ ½ ´ Î�Ó Á�°�²�Õ�´y·�³PÁ�¯�²GÁ�Ã

¿ Ä ¯�µ�­ Ä µ®Á*¾

n The ability to create multiple instances of a "class", such as a vehicle, is one of the central attributesof object-oriented languages.

Page 143: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-13

SAP AG 2002

,QWURGXFWLRQ��3URJUDPPLQJ�0RGHOV����

3URFHGXUDO�SURJUDPPLQJ3URFHGXUDO�SURJUDPPLQJ

2EMHFW�RULHQWHG�SURJUDPPLQJ2EMHFW�RULHQWHG�SURJUDPPLQJ

Page 144: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-14

SAP AG 2002

3URJUDPPLQJ�0RGHOV

'DWD�DQG�IXQFWLRQV 2EMHFW�PRGHO�DVDEVWUDFWLRQ�RI�UHDO

ZRUOGÖ �{x�t ×cv��tJzØNÙ�n Ù ØNÙqn Ù

Ø�Ù�n-Ù ØNÙ�n-ÙØ�Ùqn-Ù

j�kGlGmqnro p�lj(k(l(mqn-o pPlj�k(l�m�nro pPlj(k(lGm�n-o pPlj�kGlGm�n-o p�lj�kGl(m�n o p6lj(k(l�m�n-o pPlj(k(l�m�nro p6ljPk(l�m�n o p6lj�kGlGm�n o p6lj�kGl(m�n o p6lj�kGlGmqnro p�lj�kGl(m�n o p6lj�kGlGm�n o p6lj(k(lGm�n-o pPlj(k(l�m�n-o pPl

ØNÙ�n-Ù jPkGlGm�n o p6l�Új�kGlGm�n o p6l�Új�kGlGm�n o p6l�Ú

Ø�Ù�n-Ù j(k(lGm�n-o pPl$Új(k(l�m�n-o p�l$Új�kGlGm�n o p6l�Ú

ØNÙ�n-Ù jPkGlGm�n o p6l�Új�kGlGmqnro p�l�Új(k(l�m�nro p6l$Ú

n The left part of the slide shows that, with procedural software systems, data and functions are often:

é Created separately

é Stored separately

é Linked with input-output relations

n Objects form capsules containing the data itself and the behavior of that data. Objects enable you todraft a software solution that is a one-to-one reflection of the real-life problem area.

Page 145: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-15

SAP AG 2002

&KDUDFWHULVWLFV�RI�WKH�2EMHFW�2ULHQWHG�$SSURDFK

l 2EMHFWV�DUH�DQ�DEVWUDFWLRQ�RI�WKH�UHDOZRUOG

l 3URFHVVHV�FDQ�EH�LPSOHPHQWHGUHDOLVWLFDOO\

l 2EMHFWV�DUH�XQLWV�PDGH�XS�RI�GDWD�DQG�WKHIXQFWLRQV�EHORQJLQJ�WR�WKDW�GDWD

l ,PSURYHV�VRIWZDUH�VWUXFWXUH�DQGFRQVLVWHQF\�LQ�WKH�GHYHORSPHQW�SURFHVV

l 0DLQWHQDQFH�UHTXLUHPHQWV�DUH�UHGXFHGl 0RGHOHU��HQG�XVHU��DQG�GHYHORSHU�DUH�DOO

LQFOXGHG�LQ�DQDO\VLV�DQG�GHVLJQ�SURFHVV

Ö �{x;t�×uvy�(tJz

n In object-oriented programming, data and functions are developed together. Object orientationfocuses on objects that represent either abstract or concrete things in the real world. They are firstviewed in terms of their characteristics, which are displayed using the object’s internal structure andattributes (data).

n The EHKDYLRU�RI�REMHFWV is described through methods (functions) and events.

n Consistency throughout the software development process:The "language" used in the various phases of software development (analysis, specification, design,and implementation) is uniform. Ideally, changes made to the design during the implementationphase will flow back into the design automatically.

n The aim is to use this concept to:

é Implement processes realistically and, at the same time, better involve the modeler and developerin the software design

é Achieve optimized structuring and maintenance of the software and hence reduce the workrequired

Page 146: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-16

SAP AG 2002

7HFKQLFDO�,PSOHPHQWDWLRQ�RI�WKH�2EMHFW�2ULHQWHG$SSURDFK

Û pPÜ(Ý[Þ

Û p�ÜGÝ�ßÛ p�ÜGÝ0à

Û p�ÜGÝ0áÜPo ÚGâGã Ù�äÀå(ÜPÙqn Ù

ØNÙ�n ÙØ�Ù�n Ù j�kGlGm�n o p6l�Új(k(l�m�nro p6l$Új�kGlGm�n-o p�l�Új(k(l(mqn-o pPl$Úl (QFDSVXODWLRQ�RI�GDWD�DQG�IXQFWLRQV

l 3RO\PRUSKLVP�IRU�VXSSRUW�RI�JHQHULFSURJUDPPLQJ

l ,QKHULWDQFH

l ,PSURYHG�VWUXFWXULQJ�DQG�FRQVLVWHQF\�LQ�WKH�VRIWZDUHGHYHORSPHQW�SURFHVV

��� Æ�� � �   f6�6����V� �   f6�P��¡ ���V� �   f��6� £

n (QFDSVXODWLRQEncapsulation means that the implementation of an object is hidden from other components in thesystem, so that they cannot make assumptions about the internal status of the object and thereforedependencies on specific implementations do not arise.

n 3RO\PRUSKLVPPolymorphism (ability to have multiple forms) in the context of object technology signifies thatobjects in different classes react differently to the same messages.

n ,QKHULWDQFHInheritance defines the implementation relationship between classes, in which one class (thesubclass) shares the structure and the behavior defined in one or more other classes (superclasses).Note: ABAP Objects only allows single inheritance.

Page 147: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-17

SAP AG 2002

&OLHQW�6HUYHU�%HKDYLRU

l 2EMHFWV�EHKDYH�WRZDUGV�HDFK�RWKHU�MXVW�OLNHFOLHQW�VHUYHU�V\VWHPV

l 2EMHFWV�QRUPDOO\�DGRSW�ERWK�UROHVl 5HVSRQVLELOLWLHV�PXVW�EH�VSHFLILHG�EHWZHHQ�WKH�REMHFWV

æ�çuèêéÊçuèëíì�î�çÊï�ð

'DWD'DWD0HWKRGV0HWKRGV

'DWD'DWD

0HWKRGV0HWKRGV &$//�0(7+2'�VHUYHU�!GRBLW

GRBLWFDOOBLW

n Objects behave like client/server systems: When an object calls a method of another object, itautomatically 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

n Objects normally adopt both roles Every object is a potential server object, and when it is called by amethod of another object, it becomes a client object too.

n Establishing logical business and software/technical responsibilities between classes results in a trueclient/server software system in which redundancy is avoided.

Page 148: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-18

SAP AG 2002

&RPSDWLELOLW\�DQG�'HVLJQ�$LPV

&RPSDWLELOLW\�l $%$3�2EMHFWV�LV�D�WUXH�

FRPSDWLEOH�H[WHQVLRQ�RI�$%$3l $%$3�2EMHFWV�VWDWHPHQWV�FDQ

EH�XVHG�LQ�SURFHGXUDO�$%$3SURJUDPV

l 2EMHFWV��FODVVHV��WKHPVHOYHVFRQWDLQ�FODVVLF�$%$3VWDWHPHQWV

�'HVLJQ�DLPV�ZHUH�l $V�VLPSOH�DV�SRVVLEOHl 2QO\�REMHFW�RULHQWHG�FRQFHSWV

WKDW�KDYH�SURYHG�XVHIXOl ,QFUHDVHG�XVH�RI�W\SH�FKHFNV

�$%$3�2EMHFWV�3URJUDPP

'$7$��FRXQWHU�7<3(�L�������ZD�W\SH�.1$�����

&/$66�OFOBFDU�'(),1,7,21�������(1'&/$66�

����PDLQ�SURJUDP����������

&5($7(�2%-(&7����

FRXQWHU� �FRXQWHU�����029(�ZD�72����

�$%$3�2EMHFWV�3URJUDPP

'$7$��FRXQWHU�7<3(�L�������ZD�W\SH�.1$�����

&/$66�OFOBFDU�'(),1,7,21�������(1'&/$66�

����PDLQ�SURJUDP����������

&5($7(�2%-(&7����

FRXQWHU� �FRXQWHU�����029(�ZD�72����

n ABAP Objects is not a new language, but has been developed as an extension of ABAP. It integratesseamlessly into ABAP syntax and the ABAP programming model. All enhancements are strictlyupward compatible.

n In ABAP objects, types have to be assigned more strictly than in ABAP. - for example, whendefining interface parameters for methods, you must type the parameters. The correct pass by valueis then checked by the system when the method is called.

n In ABAP Objects, the ABAP language has been cleaned up. As part of this language clean up, thesystem sometimes executes stricter syntax checks for previously permitted constructions andobsolete statements are not allowed. The stricter syntax checks usually result in a syntax that shouldalso be used outside ABAP Objects, but where the old forms cannot be prohibited for compatibilityreasons.For further information, refer to the ABAP Objects documentation under 5HSODFHPHQW�RI�2EVROHWH6WDWHPHQWV�

Page 149: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-19

SAP AG 2002

$%$3�0DLQ�0HPRU\�DQG�(QFDSVXODWLRQ

ñ ò�ó ô(õiò�öP÷Gø�ô6øGø(ù ú6òñ ò6ó ôPõrò�ö�÷(ø(ô6ø(øGù ú6ò

ñ ò�ó ôPõrò�öP÷Gø�ô6ø�ø(ù ú�òñ ò�ó ô�õhò�öP÷(ø(ô6ø(øGù ú6ò

û�ü�ý üû�ü�ý üþqÿ����iý � �����þqÿ����hý � ���

¥�¦-§N¨]©�¦NªN©N« ©�§N¨��� ��P« �

¥*¦ §�¨]©�¦Nª�©N«-©�§�¨��� ��P« �Ø�Ù�n ÙØNÙ�n-Ù

j�kGl(m�n o p6l�Új(k(l�m�n-o pPl$Ú lÌ Ä ² Ä �DQG�EXVLQHVV½ ­y¯�°�²�³(´y¯�¾ DUHHQFDSVXODWHG�LQ�REMHFWV

��������� ����� ���! � ��"#��"$� ! % l 0RGXODUL]DWLRQ�XQLWV�IRUHQFDSVXODWLQJ� ½ ­y¯±° ²�³(´y¯¸¾

l :RUNLQJ�ZLWK�JOREDO�GDWDRI�WKH�PDLQ�SURJUDP

n One feature and design aim of object-oriented languages is that business functions are heldseparately.

n The client (in this case, the main program) uses the addresses of the objects (pointers or referencevariables) to access their encapsulated functions. Amongst other things, this concept is to improvethe structuring, reusability, and maintainability of the software.

Page 150: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-20

SAP AG 2002

7KH�6RIWZDUH�'HYHORSPHQW�3URFHVV

,PSOHPHQWDWLRQ�$%$3�2EMHFWV�

$QDO\VLV�DQGGHVLJQ

5HTXLUHPHQW�LGHD 7HVW

& tJ�(�{x�z(' �V�u�*�;��)��J�;z*)�vy� |,+�� � �À��vy�*�*� v()�|�+�J�}�{v � ���;�

n In object-oriented programming, the analysis and design phase is even more important than it is forprocedural programming. The reason for this is that in object-oriented programming, decisions takenduring the analysis and design phase have even more pronounced effects on implementation thanthey do in procedural programming.

Page 151: 003 - Bc401 - Abap Objects

© SAP AG BC401 5-21

SAP AG 2002

l 1DPH�WKH�PRVW�VLJQLILFDQW�GLIIHUHQFHV�EHWZHHQSURFHGXUDO�DQG�REMHFW�RULHQWHG�SURJUDPPLQJ

l 6WDWH�WKH�DLPV�EHKLQG�GHYHORSLQJ�WKH�$%$32EMHFWV�SURJUDPPLQJ�ODQJXDJH

<RX�DUH�QRZ�DEOH�WR�

,QWURGXFWLRQ�WR�2EMHFW�2ULHQWHG�3URJUDPPLQJ�6XPPDU\

Page 152: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-1

SAP AG 2002

l &ODVVLILFDWLRQ�RI�REMHFWVl 80/l &ODVV�GLDJUDPVl 6HTXHQFH�GLDJUDPV

&RQWHQWV�

$QDO\VLV�DQG�'HVLJQ

Page 153: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-2

SAP AG 2002

l /LVW�VRPH�LPSRUWDQW�GLDJUDP�W\SHV�LQ�80/l &UHDWH�D�FODVV�GLDJUDPl &UHDWH�D�VHTXHQFH�GLDJUDP

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

$QDO\VLV�DQG�'HVLJQ��8QLW�2EMHFWLYHV

Page 154: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-3

SAP AG 2002

$QDO\VLV�DQG�'HVLJQ��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

����������� � � ����� ��� �� � ������� � � � � � � � �

� ��� ��������� � � � � � �� "!$#�% � � & � � % � � % �('� ��) �+*-, , � � ) .0/-1 1 2 3�4�576+8 4 6 /-9;:/ 3=< 5 / : : 2 3 4�> /-? /

@BA�C D�E�F D�G HJI D�K-LM K�N L H AO K G K M-PJQ H A K DSRO K G KUT N�V H-W G A C DO H G K C L XZYB[-\+]-^U_a` ^ \ `-b ^Jc

Page 155: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-4

SAP AG 2002

$QDO\VLV�DQG�'HVLJQ����

&ODVVLILFDWLRQ&ODVVLILFDWLRQ

80/���WKH�PRGHOLQJ�VWDQGDUG80/���WKH�PRGHOLQJ�VWDQGDUG

Page 156: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-5

SAP AG 2002

&ODVVLILFDWLRQ�RI�2EMHFWV

d�e�f�gih�j k=gml

OFOBYHKLFOH OFOBSHRSOHnporqtsuswvyx{zuqu|0}�~�������~���z�o7}B���

n In the real world, there are objects, such as various airplanes , cars, and people. Some of theseobjects are very similar, that is, they can be described using the same attributes or characteristics andprovide the same functions.

n Similar objects are grouped together in classes. Each class is described once, and each object is thencreated in accordance with this blueprint.A class is therefore a description of a quantity of objects characterized by the same structure and thesame 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.

Page 157: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-6

SAP AG 2002

'LIIHUHQW�$EVWUDFWLRQV�RI�D�&ODVV

�u���u�����7�y�����-����� ��� �"���¡ ¢��� � �����y�

0DNH£¥¤§¦�¨7\SH�£¥©aªa¨Z«

36

'RRUV�ª¬©a©�­+®¯�°�±²° ³´7µ7¶·µZ¸

¹tº�»½¼ ¾½¾�¼²¿ ÀÁ7Â"Ã7Ä Å¬ÅrÄ Æ§Ç

ÈÊÉ�¼ À½Ë�ÌÍ Á=Ä ÇaÎrÏ

:KHHOVÐÒÑ ¨7¨Z« ®Ó�Ô�³ÖÕ ¾Ø× Õ�± »�É

Ù§´=¸$ÚSÅ�Û�Ú7¶�çÁ

Ü ³ ¼ Ô ÉÅÖ¸�Ä·´=Á

OFOBYHKLFOHÝ £i¤a¦�¨Ý"Þ Þ Þß Å"¸²Ä ´7Áß Á�ÂrÃBÄ Å§ÅÖÄ Æ¬Çß ´�µ§¶ µ§¸

OFOBYHKLFOHÝ £i¤¬¦-¨Ý"Þ Þ Þß Í Á�Ä Ç¬Î¬Ïß Ù0´�¸�Ú�ÅàÛ�Ú�¶áÃZÁ

n In this context, abstractions are a simplified representations of complex relationships in the realworld. 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.

n This example concerns cars. Software for a car enthusiast and software for a scrap merchant containdifferent abstractions (classes) for these objects.

n A class can contain very different objects depending on the DEVWUDFWLRQ.

n While in one software system the class lcl_vehicle describes all transport vehicles (includingbikes), in another system it describes all motorized machines with wheels.Both classes have the same name but describe different objects.

n In our examples, the class lcl_vehicle describes all motorized vehicles with 4 wheels.

Page 158: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-7

SAP AG 2002

5HSUHVHQWDWLRQ�RI�D�&ODVV

OFOBYHKLFOHPDNHPRGHOO����SULFHFRORU

VHWBPDNHGLVSOD\BDWWULEXWHVLQFUHDVHBVSHHG����

â �7���r�Jã��y�B�½�

�uäJ�����å���� ¢�

æ �½�7��ç�è��é-ê=ëUì�í�îðï�j ì�ñ"ò

Assoziationsme

Optional, doesnot have to be

specified

n UML notation:A class is represented with its name, attributes, and methods. With UML, you also have the option ofomitting either the attribute or the method part.

n Attributes describe the data that can be stored in the objects in a class.They define the state of an object.

n 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 object’s "behavior".

n ABAP Objects events are not included in class diagrams.

Page 159: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-8

SAP AG 2002

&ODVVHV�DQG�2EMHFWV

OFOBYHKLFOH&ODVV�l *HQHUDO�GHVFULSWLRQ�RI�REMHFWV

�EOXHSULQW�l 6SHFLILHV�VWDWXV�GDWD��DWWULEXWHV�

DQG�EHKDYLRU��PHWKRGV�

2EMHFW��LQVWDQFH��l 5HSUHVHQWDWLRQ�RI�UHDO�ZRUOGl 6SHFLILF�LQVWDQFH�RI�D�FODVV

d�e(ó k=e�ñ(eàì�î-eàlôï�ìôí(j�ëUk=ñàeöõ ÷�ø

« ùZ« ú0û§¨ Ѭü ù§« ¨

ý+þðÿ « ù0« ú7ûZ¨ Ѭü ùa« ¨

j k

j k

Þ Þ Þ

Þ Þ Þ

n A FODVV is a set of objects that have the same structure and the same behavior. A class is thereforelike a blueprint, in accordance with which all objects in that class are created.

n Every REMHFW has an identity, a status (set of attribute values), and behavior (set of methods andevents). The structure and behavior of similar objects are defined in their shared class.

n Identity is an attribute that distinguishes each object from all other objects. Identity is often confusedwith having the same attribute values or with a unique name. 7ZR�GLIIHUHQW�REMHFWV�FDQ�KDYHLGHQWLFDO�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.

n In German literature on the subject of object orientation, we often speak of instances. In objectorientation an instance is simply an object. Another synonym of REMHFW is LQVWDQFH.

Page 160: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-9

SAP AG 2002

$QDO\VLV�DQG�'HVLJQ����

&ODVVLILFDWLRQ&ODVVLILFDWLRQ

80/���WKH�PRGHOLQJ�VWDQGDUG80/���WKH�PRGHOLQJ�VWDQGDUG

Page 161: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-10

SAP AG 2002

l 80/�VWDQGV�IRU�8QLILHG�0RGHOLQJ�/DQJXDJHl ,W�LV�D�JOREDO�VWDQGDUGl ,W�LV�D�ODQJXDJH�DQG�IRUP�RI�QRWDWLRQ�IRU�WKH�VSHFLILFDWLRQ��

FRQVWUXFWLRQ��YLVXDOL]DWLRQ��DQG�GRFXPHQWDWLRQ�RI�PRGHOVIRU�VRIWZDUH�V\VWHPV� 'LDJUDP�W\SHV��

w &ODVV�GLDJUDPw %HKDYLRU�GLDJUDP��VXFK�DV�D�VHTXHQFH�GLDJUDPw &RPSRQHQW�GLDJUDPw 'LVWULEXWLRQ�GLDJUDPw ���

0RGHOLQJ�ZLWK�80/

n UML (Unified Modeling Language) is a standardized modeling language. It is used for thespecification, construction, visualization, and documentation of models for software systems andenables uniform communication between various users. UML does not describe the steps in theobject-oriented development process.

n 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 developingit further. SAP uses UML as the company-wide standard for object-oriented modeling.

n You can find the UML specifications on the OMG homepage at:http://www.omg.org

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

n %HKDYLRU�GLDJUDPV demonstrate the relationships and method calls between objects. Theyemphasize the timing sequence of method calls.

n Component diagrams show the organization and dependencies of components.

n Distribution diagrams represent the dependencies of software and hardware.

Page 162: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-11

SAP AG 2002

([DPSOH�RI�D�&ODVV�'LDJUDP

OFOBUHQWDO

OFOBYHKLFOH

OFOBFDU OFOBWUXFN OFOBEXV

OFOBERRNLQJ OFOBFXVWRPHU

OFOBZKHHO

10..*0..*1

1

1..*11..*

n A class diagram describes the elements contained in the model and their various static relationships.There are two basic forms of static relationships:

• Associations (for example, a car rental customer books a car)

• Generalization / specialization (for example, a car and a bus are both vehicles)

n In class diagrams, classes can also be shown with their attributes and methods.

Page 163: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-12

SAP AG 2002

$VVRFLDWLRQ

A customer can rentseveral cars

¯ °���� ° ¿ Ô�Õà³�� ¼ ¿ Õ�± ¼²ÌS¼²É��

ERRNV

Association name

* or 0..* Many1 Exactly one1..* One or more0..1 Zero or one

10..*OFOBERRNLQJ OFOBFXVWRPHU

A booking refers toa customer

� An association describes a semantic relationship between classes. The specific relationship betweenobjects in these classes is known as an object link. Object links are therefore the instances of anassociation.

� An association is usually a relationship between different classes. However, an association can alsobe recursive; in this case, the class would have a relationship with itself. In most cases, recursiveassociations 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 couldbe called "reservation").

� Each role has a cardinality that shows how many instances participate in this relationship. Themultiplicity is the number of participating objects in one class that have a relationship to an objectin the other class.

� UML notation:

• An association is represented by a line between the class symbols

• 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 iswritten in italics above the line and may have an arrow to show the read direction. Both areoptional.

Page 164: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-13

SAP AG 2002

$JJUHJDWLRQ�DQG�&RPSRVLWLRQ

$JJUHJDWLRQ�LV�D�VSHFLDO�FDVH�RI�DVVRFLDWLRQ��D�ZKROH�SDUW�UHODWLRQVKLS

&RPSRVLWLRQ�LV�D�VSHFLDO�FDVH�RI�DJJUHJDWLRQ��DQ�H[LVWHQFH�GHSHQGHQW�ZKROH�SDUW�UHODWLRQVKLS

Composition symbol

Aggregation symbol

OFOBUHQWDO OFOBERRNLQJ1..*1

OFOBYHKLFOH OFOBZKHHO1 0..*

n Aggregation is a special kind of association. Aggregation describes one object that contains anotheror consists of other objects (whole-part). A vehicle consists of wheels. The relationship can bedescribed by the words "consists of" or "is a part of".

n UML notation for aggregation:An aggregation, like an association, is represented by a line between two classes, which thenadditionally has a small rhombus at one end. The rhombus is always at the aggregate end, that is, thewhole object end. Otherwise the notation conventions are the same as for associations.

n Composition is a special kind of aggregation. Composition describes the fact that the objectcontained cannot exist without the aggregate (for example, a car reservation cannot exist without thecar rental).

n 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 islinked to the lifetime of the aggregate: Parts are created either with or immediately after theaggregate, and they are destroyed either with or immediately before the aggregate.

n UML notation for composition:Like aggregation, composition is shown as a line between two classes and marked with a smallrhombus on the aggregate side. However, in contrast to aggregation, the rhombus is filled in.

Page 165: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-14

SAP AG 2002

*HQHUDOL]DWLRQ�DQG�6SHFLDOL]DWLRQ

RU

Gen

eral

izat

ion

Spe

cial

izat

ion

Gen

eral

izat

ion

Spe

cial

izat

ion

OFOBYHKLFOH

OFOBFDU OFOBWUXFN

OFOBYHKLFOH

OFOBFDU OFOBWUXFN

Inheritance arrow

n UML notation:Generalization and specialization are denoted by triangular arrows that point from the subordinateclass to the superclass.Several arrows can be combined into a tree.

Page 166: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-15

SAP AG 2002

6HTXHQFH�'LDJUDPV��([DPSOH

Life lineof object

Control focus

5HWXUQ�YDOXH

��0HWKRG��SDUDPHWHU�Time

OFOBGULYHU OFOBFDU

3URFHVVGHVFULSWLRQ�RSWLRQDO�

n Sequence diagrams, unlike class diagrams, show the dynamics between objects. They are used torepresent a particular process or a particular situation. Sequence diagrams focus on the time sequenceof the information exchange:a) Creation and deletion of objectsb) Message exchange between objects

n Sequence diagrams have no notation for representing static methods.

n The object life line is represented by vertical dotted lines.

n The control focus is shown as a vertical rectangle on the object life line. The control focus shows theobject’s "active" period:

• An object is active when actions are executed

• An object is indirectly active if it is waiting for a subordinate procedure to end.

n Messages are shown as horizontal arrows between the object lines. The message is written above thearrow in the form PHWKRG��SDUDPHWHU�. There are various ways of representing the reply; in thisexample, the arrow is shown as a returning arrow.

n You can also include a description of the process and add comments to the object life line asrequired.

Page 167: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-16

SAP AG 2002

���JHWBIXHOBOHYHO������JHWBIXHOBOHYHO���

UHBOHYHO�UHBOHYHO

OFOBGULYHU OFOBFDU OFOBWDQN

UHBOHYHO� ���IXHO���IXHOBPD[� �����

UHBOHYHO� ���WDQN�!JHWBIXHOBOHYHO����

6HTXHQFH�'LDJUDP��'HOHJDWLRQ

n In delegation, two objects are involved in handling a request: The recipient of the request delegatesthe execution of the request to a delegate.

n Example:The driver (lcl_driver) calls the method get_fuel_level for the class car (lcl_car). Thecar cannot carry out this task itself. Therefore, the car calls the get_fuel_level method for theclass (lcl_tank), that is the car GHOHJDWHV the execution of the method to the tank.

n The main advantage of delegation (as a re-use mechanism) lies in the option of changing thebehavior of the recipient by substituting the delegate (at runtime). For example, delegation enablesthe car to be equipped with a new tank, without the call changing for the client or for the car class.

n Good encapsulation often forces the use of delegation: If tank in the above example were a privateattribute of the class lcl_car, the user could not address the tank directly, but only through the car.

Page 168: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-17

SAP AG 2002

l /LVW�VRPH�GLDJUDP�W\SHV�LQ�80/l &UHDWH�D�FODVV�GLDJUDPl &UHDWH�D�VHTXHQFH�GLDJUDP

<RX�DUH�QRZ�DEOH�WR�

$QDO\VLV�DQG�'HVLJQ��6XPPDU\

Page 169: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-18

�([HUFLVHV8QLW��$QDO\VLV�DQG�'HVLJQ7RSLF��80/�&ODVV�'LDJUDPV

At the conclusion of these exercises, you will be able to:

• Create a UML class diagram

An airline needs to manage its airplanes.

1-1 Use a pencil and paper to create a UML class diagram that contains the followingclasses:- 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 indicatepossible cardinalities.

Page 170: 003 - Bc401 - Abap Objects

© SAP AG BC401 6-19

�6ROXWLRQV8QLW��$QDO\VLV�DQG�'HVLJQ7RSLF��80/�&ODVV�'LDJUDPV

Page 171: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-1

SAP AG 2002

l &ODVVHVl 2EMHFWVl $WWULEXWHVl 0HWKRGVl 9LVLELOLW\�HQFDSVXODWLRQl ,QVWDQWLDWLRQl &RQVWUXFWRUl *DUEDJH�FROOHFWRU

&RQWHQWV�

3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJ

Page 172: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-2

SAP AG 2002

l &UHDWH�FODVVHVl &UHDWH�REMHFWVl &DOO�PHWKRGVl ([SODLQ�KRZ�WKH�JDUEDJH�FROOHFWRU�ZRUNV

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJ� 8QLW2EMHFWLYHV

Page 173: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-3

SAP AG 2002

3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJ�,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �� ��� ��� ����� � � � � ������� � � � � � � � � � ��! � � � � ��"

" � � #%$�& & ' (�)�*,+.- ) + $�/10$ (324* $ 0 0 ' ( )�5 $�6 $7�8�9 :�;4< :�= >�? :�@�AB @�C A > 8D @ = @ B�E�F > 8 @ :HGD @ = @JI CLK >�M = 8 9 :D > = @ 9 A N�O�P�Q R�SUTWV S Q V�X S�Y

Z\[W]L^J_ ] `ba c�dfehgjilk�m c _�no i [ ] c ^ n c4pZ [ ehq [srut t ] ^ q

Z [ ] ^J_ ] `ba c d ehg ilk�m c _ no i [ ] c ^ n c pZ [ ehq [ r t t ]v^ q

Page 174: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-4

SAP AG 2002

3ULQFLSOHV����

&ODVVHV��DWWULEXWHV��DQG�PHWKRGV&ODVVHV��DWWULEXWHV��DQG�PHWKRGV

2EMHFWV��LQVWDQFHV�RI�FODVVHV2EMHFWV��LQVWDQFHV�RI�FODVVHV

$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV

7KH�FRQVWUXFWRU7KH�FRQVWUXFWRU

$GGLWLRQDO�SULQFLSOHV$GGLWLRQDO�SULQFLSOHV

Page 175: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-5

SAP AG 2002

([DPSOH�RI�D�&ODVV

OFOBYHKLFOH3ULYDWHcomponents

0HWKRGV�Implementation

3XEOLFcomponents

3ULYDWH�DFFHVV•�Encapsulation• Normally attributes

3XEOLF�DFFHVV•�Interface• Normally methods, events

wyx1zW{wl|J}1{J~��{��L�Uw�xJz�{

�1{��L�Uw�xJz�{

�����h�s�U���4�b���U���L����h� �������h�

n In the graphic, the public component of the class is accessed using the green node or starting pointdisplayed on the left. A user or client can use this node to access the public components and hencealso indirectly access the private components. However, the private components of the class cannotbe addressed directly . They are not visible to the outside user.

n Why are the private components of a class hidden?This principle is called LQIRUPDWLRQ�KLGLQJ or HQFDSVXODWLRQ and is used to protect the user.Let us assume that a class changes its private components, while its interface remains unchanged.Any user who simply needs to access the interface of the class can carry on working with the class asusual. The user does not notice the change.However, if an class changes its public components, then any user who accesses these publiccomponents must take these changes into account.

Page 176: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-6

SAP AG 2002

'HILQLQJ�&ODVVHV

&/$66�OFOBYHKLFOH�'(),1,7,21�

(1'&/$66�

&/$66�OFOBYHKLFOH�,03/(0(17$7,21�

(1'&/$66�

OFOBYHKLFOH����� � x���{components

� {�� ¡|J}¢ £¥¤�¦ §�£¨§W©�ª¬«�ª�¢ ­�©

��®4¯ ~ � °components

±³²f±µ´·¶¹¸yºy»

n A FODVV is a set of objects that have the same structure and the same behavior. A class is thereforelike a EOXHSULQW, in accordance with which all objects in that class are created.

n 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 theimplementation part.

n The CLASS statement cannot be nested, that is, you cannot define a class within a class.

Page 177: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-7

SAP AG 2002

$WWULEXWHV

l $WWULEXWHV�FDQ�KDYH�DQ\�NLQG�RIGDWD�W\SH�l &��1��,��3�������675,1*l 'LFWLRQDU\�W\SHVl 8VHU�GHILQHG�W\SHVl 7<3(�5()�72

GHILQHV�D�UHIHUHQFH�WR�DQREMHFW��LQ�WKLV�FDVH�³UBFDU´

��� OFOBFDUPrivatePublic

PDNH��7<3(�675,1*����

�����������UBPRWRU�������7<3(�5()�72����

��� OFOBUHQWDO

¼�½b¾�¿À¼

n Attributes describe the data that can be stored in the objects of a class.

n Class attributes can be of any type:

• Data types: scalar (for example data element), structured, in tables

• ABAP elementary types (C, I, ...)

• Object references

• Interface references

n Examples of attributes for the class lcl_car are:

• make (car make)

• modell (type , model)

• ser_no (serial number)

• color (color)

• car_type (estate, convertible, ...)

• max_seats (number of seats)

• r_motor (reference to class lcl_motor)

• ...

Page 178: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-8

SAP AG 2002

$WWULEXWHV��7\SHV��DQG�&RQVWDQWV��6\QWD[

&/$66��FODVVQDPH!�'(),1,7,21������������7<3(6���QRUPDOH�7\SGHILQLWLRQ!�����&2167$176��FRQVWDQW�7<3(��W\SH!�9$/8(��YDOXH!�����'$7$��YDULDEOH��7<3(��W\SH!�����������YDULDEOH��7<3(��GGLFBW\SH!�����������YDULDEOH��/,.(�YDULDEOH������������YDULDEOH��7<3(��W\SH!�9$/8(��YDOXH!�����������YDULDEOH��7<3(��W\SH!�5($'�21/<�����������YDULDEOH��7<3(�5()�72��FODVVQDPH!�����������YDULDEOH��7<3(�5()�72��LQWHUIDFH!�����&/$66�'$7$��RQO\BRQFH�7<3(������(1'&/$66�

n In classes, you can only use the TYPE addition to refer to data types.

n You can only use the LIKE reference for local data objects.

n The READ-ONLY addition means that a public attribute declared with DATA can be read fromoutside, but can only be changed by methods of the class.

n You can currently only use the READ-ONLY addition in the public visibility section (PUBLICSECTION) of a class declaration or in an interface definition.

Page 179: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-9

SAP AG 2002

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����...��35,9$7(�6(&7,21�����'$7$��PDNH������7<3(�VWULQJ�(1'&/$66�

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����'$7$��PDNH�7<3(�VWULQJ���35,9$7(�6(&7,21�(1'&/$66�

$WWULEXWHV�DQG�9LVLELOLW\

l 3XEOLF�DWWULEXWHVÁ &DQ�EH�YLHZHG�DQGFKDQJHG�E\�DOO�XVHUV�DQGLQ�DOO�PHWKRGV

Á 'LUHFW�DFFHVV

l 3ULYDWH�DWWULEXWHVÁ &DQ�RQO\�EH�YLHZHG�DQGFKDQJHG�IURP�ZLWKLQ�WKHFODVV

Á 1R�GLUHFW�DFFHVVIURP�RXWVLGH�WKH�FODVV

n You can protect attributes against access from outside by characterizing them as private attributes(defined in the PRIVATE SECTION).

n Attributes and their values that may be used directly by an external user are public attributes and aredefined in the PUBLIC SECTION.

n In the above example for the class lcl_car, the attribute make is defined as a public attribute.

n Public attributes belong to the LQWHUIDFH of the class, that is their implementation is publicized. If youwant to hide the internal implementation from users, you must define internal and external views ofattributes.

n As a general rule, you should define as few public attributes as possible.

Page 180: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-10

SAP AG 2002

$FFHVVLQJ�3ULYDWH�$WWULEXWHV

��� OFOBYHKLFOH3ULYDWH3XEOLF

��ÂÄÃ4��Åh� �UÆ �

PDNH�����

ÇÉÈ�Ê »jËyÌ

VHWBPDNHJHWBPDNH...

n You can access an object’s private attributes using public methods, which in turn output this attributeor change it.

n It is not possible to directly access private attributes from outside (for example, main program orother object). The only exception lies in the so-called "friend" concept, which will be dealt with laterin the course.

Page 181: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-11

SAP AG 2002

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����������...��35,9$7(�6(&7,21�����'$7$��PDNH������7<3(�VWULQJ�����������...

����&/$66�'$7$��QBRBYHKLFOHV�7<3(�L�(1'&/$66�

,QVWDQFH�$WWULEXWHV�DQG�6WDWLF�$WWULEXWHV����

l ,QVWDQFH�DWWULEXWHVÁ 2QH�SHU�LQVWDQFHÁ 6WDWHPHQW��'$7$

l 6WDWLF�DWWULEXWHVÁ 2QO\�RQH�SHU�FODVVÁ 6WDWHPHQW��&/$66�'$7$Á $OVR�NQRZQ�DV�FODVVDWWULEXWHV

n There are two kinds of attributes:

• Static attributes

• Instance attributes

n Instance attributes are attributes that exist separately for each object.Instance attributes are defined using the ABAP keyword DATA.

n 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:

• Data that is the same in all instances

• Administrative information about the instances in that class (for example, counters, ...)

n Static attributes are defined using the CLASS-DATA keyword.

n You may come across the expression "class attributes" in documentation, however, the official termin ABAP Objects (as in C++, Java) is "static" attributes.

Page 182: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-12

SAP AG 2002

,QVWDQFH�$WWULEXWHV�DQG�6WDWLF�$WWULEXWHV����

,QWHUQDO�VHVVLRQ,QWHUQDO�VHVVLRQÍ Æv� �hÎ Æ �hÎÀ��Î � �WÏ�� � �v�

Ð Ñ ÒHÓ Ô�Ó Õ×ÖLØ×Ù×Ú ÔLÓ Ø

Ð Û ÒJÓ Ô�Ó Õ�ÖLØLÙÜÚ Ô�Ó Ø���

QBRBYHKLFOHV �

UBYHKLFOH��

UBYHKLFOH�UBYHKLFOH�

Ð Ý ÒHÓ Ô�Ó Õ�ÖLØ×Ù�Ú Ô�Ó ØÞàß á�ß â ãäásß ß å�â æ%ç�ß è�éÞ�ß ásß â ãäá�ß ß å�â æ%ç,ß è�é

6WDWLF�DWWULEXWHVRQO\�H[LVW�RQFH

,QVWDQFHV��REMHFWV

���

n The graphic shows the static attribute n_o_vehicles.It exists only once, regardless of how many instances there are of lcl_vehicle.

n Therefore, you can say that instances share their common attributes.

Page 183: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-13

SAP AG 2002

0HWKRGV��6\QWD[

&/$66��FODVVQDPH!�,03/(0(17$7,21���0(7+2'��PHWKRGBQDPH!����������(1'0(7+2'�(1'&/$66�

&/$66��FODVVQDPH!�'(),1,7,21����������0(7+2'6���PHWKRGBQDPH!��������������>�,03257,1*��LPBYDU!�7<3(��W\SH!����������������(;3257,1*��H[BYDU!�7<3(��W\SH!����������������&+$1*,1*���FKBYDU!�7<3(��W\SH!����������������5(7851,1*��9$/8(��UHBYDU!��7<3(��W\SH!����������������EXCEPTIONS <exception>

�5$,6,1*�����FODVVBH[FHSWLRQ! @�(1'&/$66�

0HWKRGV�FDQ�KDYHD�VLJQDWXUH

0HWKRGV�FRQWDLQ�WKHVRXUFH�FRGH��DQG�KHQFHGHILQH�WKH�EHKDYLRU�RI�DQREMHFW

n Methods are internal procedures in classes that determine the behavior of an object. They can accessall attributes in their class and can therefore change the state of an object.

n Methods have a parameter interface (called VLJQDWXUH) that enables them to receive values when theyare called and pass values back to the calling program.

n In ABAP Objects, methods can have IMPORTING, EXPORTING, CHANGING, and RETURNINGparameters as well as exception parameters. All parameters can be passed by value or reference. (Asof SAP R/3 Basis Release 6.10, you should no longer use the EXCEPTIONS parameter forexceptions but use the RAISING addition instead; this will be discussed in more detail later.)

n You can define a return code for methods using RETURNING. You can only do this for a singleparameter, which additionally must be passed as a value. Also, you cannot then define EXPORTINGand CHANGING parameters. You can define functional methods using the RETURNING parameter(this will be explained in more detail).

n All input parameters (IMPORTING, CHANGING parameters) can be defined as optional parametersin the declaration using the OPTIONAL or DEFAULT additions. These parameters then do notnecessarily have to be passed when the object is called. If you use the OPTIONAL addition, theparameter remains initialized according to type, whereas the DEFAULT addition allows you to entera start value.

Page 184: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-14

SAP AG 2002

$FFHVVLQJ�3ULYDWH�0HWKRGV

��� OFOBYHKLFOH3ULYDWH3XEOLF

��¨Ã���Åu� �UÆ � LQLWBPDNHVHWBPDNH

PDNHêWêWê

���

ÇÉÈ�Ê »jËyÌ

n Methods also have to be assigned to a visibility area. This determines whether the methods can becalled from outside or only from within the class.

n It is not possible to directly access private methods from outside. However, a private method can becalled by a public method.

n Both method types can access the public and private attributes.

Page 185: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-15

SAP AG 2002

0HWKRGV�DQG�9LVLELOLW\

l 3XEOLF�PHWKRGVÁ &DQ�EH�FDOOHG�IURPDQ\ZKHUH

l 3ULYDWH�PHWKRGVÁ &DQ�RQO\�EH�FDOOHGZLWKLQ�WKH�FODVV

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����0(7+2'6��VHWBPDNH�LPSRUWLQJ������������������LPBPDNH�7<3(�VWULQJ���35,9$7(�6(&7,21�����0(7+2'6��LQLWBPDNH�����'$7$��PDNH�7<3(�VWULQJ�(1'&/$66�

&/$66�OFOBYHKLFOH�,03/(0(17$7,21���0(7+2'�LQLWBPDNH�����PDNH� �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�

n In this example, init_make is a private method that is called by the public method set_make.

Page 186: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-16

SAP AG 2002

,QVWDQFH�0HWKRGV�DQG�6WDWLF�0HWKRGV

l ,QVWDQFH�PHWKRGVÁ &DQ�XVH�ERWK�VWDWLF�DQG�LQVWDQFH�FRPSRQHQWV�LQ�WKHLULPSOHPHQWDWLRQ�SDUW

Á &DQ�EH�FDOOHG�XVLQJ�DQ�LQVWDQFH

l 6WDWLF�PHWKRGVÁ &DQ�RQO\�XVH�VWDWLF�FRPSRQHQWV�LQ�WKHLU�LPSOHPHQWDWLRQSDUW

Á &DQ�EH�FDOOHG�XVLQJ�WKH�FODVV

5XOHV�

n Static methods are defined at class level. They are similar to instance methods, but with therestriction that they can only use static components (such as static attributes) in the implementationpart. This means that static methods do not need instances and can be called from anywhere. Theyare defined using the CLASS-METHODS statement, and they are bound by the same syntax andparameter rules as instance methods.

n The term "class method" is common, but the official term in ABAP Objects (as in C++, Java) is"static method".

Page 187: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-17

SAP AG 2002

,QVWDQFH�0HWKRGV�DQG�6WDWLF�0HWKRGV��([DPSOH

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����0(7+2'6��������VHWBPDNH�,03257,1*�LPBPDNH�7<3(�VWULQJ�����&/$66�0(7+2'6��JHWBFRXQW�(;3257,1*�H[BFRXQW�7<3(�L���35,9$7(�6(&7,21�����'$7$��������PDNH�7<3(�VWULQJ�����&/$66�'$7$��QBRBYHKLFOHV�7<3(�L�(1'&/$66�&/$66�OFOBYHKLFOH�,03/(0(17$7,21��������0(7+2'�JHWBFRXQW������H[BFRXQW� �QBRBYHKLFOHV���(1'0(7+2'(1'&/$66�

n In the static method get_count, you can only use the static attribute n_o_vehicles. All otherattributes of the class are instance attributes and can only appear in instance methods.

Page 188: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-18

SAP AG 2002

6XPPDU\�DQG�80/�1RWDWLRQ

ë �3��ìÜ�����í� � � � � Æ � �î���hï � � �u�4�¹���ð �3��ìÜ�����í� � �h�3�vÃ4Ψ�¬� ���hï � � �h���¹�¬�Âñ�Ä�.ÎÄ�¬� �ò�4�uï � � �u���b���òÎ4�3� � �h�h�4� Æ �L�h���

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����0(7+2'6��������VHWBPDNH��,03257,1*�LPBPDNH�7<3(�VWULQJ�����&/$66�0(7+2'6��JHWBFRXQW�(;3257,1*�H[BFRXQW�7<3(�L���35,9$7(�6(&7,21�����'$7$��������PDNH��7<3(�VWULQJ����������&/$66�'$7$��QBRBYHKLFOHV�7<3(�L�����0(7+2'6��LQLWBPDNH�(1'&/$66�

OFOBYHKLFOH��PDNH��PRGHO��QBRBYHKLFOHV������VHWBPDNH����LQLWBPDNH��JHWBFRXQW����

n A UML class diagram shows firstly the class name and, underneath that, the class attributes andmethods.

n 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 ofmodeling 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 areclose to implementation.

n Static components are marked with an underscore.

n The method signature is represented as follows (optional):

• The input and output parameters and the parameters to be changed are shown in brackets.

• The return code is separated from the type name by a colon.

Page 189: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-19

SAP AG 2002

3ULQFLSOHV����

&ODVVHV��DWWULEXWHV��DQG�PHWKRGV&ODVVHV��DWWULEXWHV��DQG�PHWKRGV

2EMHFWV��LQVWDQFHV�RI�FODVVHV2EMHFWV��LQVWDQFHV�RI�FODVVHV

$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV

7KH�FRQVWUXFWRU7KH�FRQVWUXFWRU

$GGLWLRQDO�SULQFLSOHV$GGLWLRQDO�SULQFLSOHV

Page 190: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-20

SAP AG 2002

&UHDWLQJ�2EMHFWV

5XOHV� l 2EMHFWV�DUH�FUHDWHG�XVLQJ�WKH�&5($7(�2%-(&7VWDWHPHQW

l 2EMHFWV�FDQ�RQO\�EH�FUHDWHG�DQG�DGGUHVVHG�XVLQJUHIHUHQFH�YDULDEOHV

OFOBYHKLFOH��PDNH��PRGHO��VHUBQR��QBRBYHKLFOHV���������VHWBPDNH����LQLWBPDNH��JHWBFRXQW���

����OFOBYHKLFOHPrivatePublic ó¡ô�õWöó¡÷�øWöWùWùú öWû üàý

÷êWê�ê

ú öàþ üó¡ô�õ�ö

ÿ öàþ ü��÷ � ý

þê�êWê

&5($7(�2%-(&7�UBYHKLFOH

��������� ����

n A class contains the generic description of an object. It describes all the characteristics that arecommon to all objects in that class. During the program runtime, the class is used to create specificobjects (instances). This process is called instantiation.

n Example:The specific object “Car xy with Ser-No. 0815" is created through instantiation from the classlcl_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.

n Implementation:Objects are instantiated using the statement:�CREATE OBJECT.During instantiation, the runtime environment dynamically requests main memory space and assignsit to the object.

Page 191: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-21

SAP AG 2002

5HIHUHQFH�9DULDEOHV

���&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����������35,9$7(�6(&7,21��������(1'&/$66�

&/$66�OFOBYHKLFOH�,03/(0(17$7,21������(1'&/$66�

'$7$��UBYHKLFOH��7<3(�5()�72�OFOBYHKLFOH�������UBYHKLFOH��7<3(�5()�72�OFOBYHKLOFH�

��������������������� ���!"��#���$&% ...

�'�(����)�* +�*�(,

�������)�* ����- ""

:KDW�GR�WKHVH�UHIHUHQFH�YDULDEOHV�

SRLQW�WR"

n DATA: r_vehicle1 TYPE REF TO lcl_vehicle declares a reference variable that acts asa pointer to an object.

Page 192: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-22

SAP AG 2002

&UHDWLQJ�2EMHFWV��6\QWD[

'$7$��UBYHKLFOH��7<3(�5()�72�OFOBYHKLFOH�������UBYHKLFOH��7<3(�5()�72�OFOBYHKLFOH�&5($7(�2%-(&7�UBYHKLFOH���&5($7(�2%-(&7�UBYHKLFOH��

�.�������� �*�/,

���(����)�* +�*�-

02143�5 6�5 7�8�9":�; 6�5 9PrivatePublic

02<43�5 6"5 7�8�9�:�; 6�5 9PrivatePublic

n The CREATE OBJECT statement creates an object in the main memory. The attribute values of thisobject are either initial values or correspond to the VALUE entry.

n Reference variables can also be assigned to each other.For the above example this would mean that r_vehicle1 and r_vehicle2 point to the sameobject.

Page 193: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-23

SAP AG 2002

*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�� �UBYHKLFOH�

�'�(����)�* +�*��- 02<43�5 6"5 7�8�9�:�; 6�5 9PrivatePublic

���(�+��)�* ��*�(,

02143�5 6�5 7�8�9":�; 6�5 9PrivatePublic

n As soon as no more references point to an object, the Garbage Collector removes it from thememory.

n The Garbage Collector is a system routine that automatically deletes objects that can no longer beaddressed from the main memory and releases the memory space they occupied.

Page 194: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-24

SAP AG 2002

*DUEDJH�&ROOHFWRU��3URFHGXUH

l $OO�LQGHSHQGHQW�UHIHUHQFHV�LQ�WKH�JOREDO�PDLQ�PHPRU\�DUH�FKHFNHG��7KHUHIHUHQFHV�SRLQW�WR�DFWLYH�REMHFWV��ZKLFK�DUH�PDUNHG�LQWHUQDOO\�

l 5HIHUHQFHV�RI�FODVV�RU�LQVWDQFH�DWWULEXWHV�WR�RWKHU�REMHFWV�DUH�IROORZHG�7KHVH�REMHFWV�DUH�DOVR�PDUNHG�

l 2EMHFWV�WKDW�DUH�QRW�PDUNHG�DUH�GHOHWHG�IURP�WKH�PDLQ�PHPRU\�

02=>3/5 6�5 7+?@BAC9�6ED

02<43�5 6"5 7�?�@BAC9F6�D

0*GH3/5 6"5 7�?�@BAC9F6�D

I �����/J��+K

n Independent references are references that have not been defined within a class.

Page 195: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-25

SAP AG 2002

%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�

&5($7(�2%-(&7�UBYHKLFOH��$33(1'�UBYHKLFOH�72�LWDE�

02=>3/5 6�5 7+?@BAC9�6ED

021L3/5 6"5 7+?@BAC9F6MD

/223�$7�LWDE�,172�UBYHKLFOH� ��ZRUN�ZLWK�WKH�FXUUHQW�LQVWDQFH�(1'/223�...

n If you want to keep several objects from the same class in your program, you can define an internaltable, which, for example, only consist of one column containing the object references for this class.

n You can process the objects using a LOOP through the internal table.

Page 196: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-26

SAP AG 2002

$JJUHJDWLRQ�([DPSOH

����OFOBYHKLFOHPrivatePublic N þ%ô4O üFP�Q

ö�ö�ù ú

0SRETU3/5 6�5 7�VW:F9F9�5

02=�G43/5 6�5 7�VX:F9F9"5

02=�YH3�5 6�5 7�VZ:F9M9+5

021"[\3�5 6�5 7"VW:F9M9+5

n If a class defines object references to a second class as attributes (in the above example: Referencesto objects of the class lcl_wheel), only these object references will be stored in an objectbelonging to that class.

n The objects in the second class lcl_wheel have their own identity. They are encapsulated in thefirst class lcl_vehicle and can only be addressed from this class using reference variables.

Page 197: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-27

SAP AG 2002

3ULQFLSOHV����

&ODVVHV��DWWULEXWHV��DQG�PHWKRGV&ODVVHV��DWWULEXWHV��DQG�PHWKRGV

2EMHFWV��LQVWDQFHV�RI�FODVVHV2EMHFWV��LQVWDQFHV�RI�FODVVHV

$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV

7KH�FRQVWUXFWRU7KH�FRQVWUXFWRU

$GGLWLRQDO�SULQFLSOHV$GGLWLRQDO�SULQFLSOHV

Page 198: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-28

SAP AG 2002

&DOOLQJ�0HWKRGV

����OFOBYHKLFOHPrivatePublic

UBYHKLFOH�!PRWRUBRQ���

5HIHUHQFH

&RPSRQHQW�VHOHFWRU

0HWKRG

]_^U`Uacbed

PRWRUBRQ

n Every object behaves in a certain way. This behavior is determined by its methods. There are threetypes 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

n An object that requires the services of another object sends a message to the object providing theservices. This message names the operation to be executed. The implementation of this operation isknown as a method.

n For the sake of simplicity, method will henceforth be used as a synonym for operation and message.

n The example shows the new syntax for method calls, in which the CALL-METHOD prefix is omitted.

Page 199: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-29

SAP AG 2002

&DOOLQJ�,QVWDQFH�0HWKRGV��6\QWD[

f*g)h Jji g ��lkm��Jn�o)K h+p ���&$//�0(7+2'��LQVWDQFH!�!�LQVWDQFHBPHWKRG!��������������� (;3257,1*��LPBYDU!� ��YDULDEOH!��������������� ,03257,1*��H[BYDU!� ��YDULDEOH!��������������� &+$1*,1*���FKBYDU!� ��YDULDEOH!��������������� 5(&(,9,1*��UHBYDU!� ��YDULDEOH!��������������� EXCEPTIONS <exception> = <nr>�q �o)�MJr� g �K g osJ2i/J2�*o g i h o)t�uvxw+y p �LQVWDQFH!�!�LQVWDQFHBPHWKRG!�VHH�RQOLQH�GRFXPHQWDWLRQ� ��>DGGLWLRQV@���

'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�'$7$��PDNHBQDPH�7<3(�675,1*�...

PDNHBQDPH� �WKH�PDNH�RI�WKH�FDU�&$//�0(7+2'�UBYHKLFOH�!VHWBPDNH�(;3257,1*�LPBPDNH� �PDNHBQDPH� �RWKHU�YDULDQW��VLQFH�����DYDLODEOH��VHH�GRFXPHQWDWLRQ�UBYHKLFOH�!VHWBPDNH��PDNHBQDPH�� �QRZ�JHWWLQJ�VRPHWKLQJ�EDFN�IURP�PHWKRG���UBYHKLFOH�!JHWBPDNH��,03257,1*�H[BPDNH� �PDNHBQDPH���

n Instance methods are called using CALL METHOD <reference>-><instance_method>.

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

n Special case: When calling an instance method from within another instance method, you can omitthe instance name. The method is automatically executed for the current object.

n 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, ifthe method has two or more parameters in its interface, all actual and formal parameters within thebrackets must have the EXPORTING addition.

n In the case of methods that return parameters to the caller, the IMPORTING addition must always beused and all actual and formal parameters must be listed.

n In method calls, multiple parameters are separated by spaces.

Page 200: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-30

SAP AG 2002

&DOOLQJ�6WDWLF�0HWKRGV��6\QWD[

6WDWLF�PHWKRGV���&$//�0(7+2'��FODVVQDPH! !�FODVVBPHWKRG!��>DGGLWLRQV@�������FODVVQDPH! !�FODVVBPHWKRG!��>DGGLWLRQV@���

'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�'$7$��PDNHBQDPH�7<3(�675,1*�������FRXQW�7<3(�,�...

�JHWBFRXQW�KDV�RQH�LQWHUIDFH�SDUDPHWHU�UHBFRXQW&$//�0(7+2'�OFOBYHKLFOH !JHWBFRXQW��,03257,1*�UHBFRXQW� �FRXQW��� �RWKHU�YDULDQW��VLQFH�����DYDLODEOH��VHH�GRFXPHQWDWLRQOFOBYHKLFOH !JHWBFRXQW��,03257,1*�UHBFRXQW� �FRXQW���

n Static methods (also referred to as class methods) are called using CALL METHOD<classname>=><class_method>.

n Static methods are addressed with their class name, since they do not need instances.

n Note:If you are calling a static method from within the class, you can omit the class name.

Page 201: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-31

SAP AG 2002

)XQFWLRQDO�0HWKRGV

l z|{~}�����}��'���s�'�s�~�� 2QO\�RQH�5(7851,1*�SDUDPHWHU� 2QO\�,03257,1*�SDUDPHWHUV�DQG�H[FHSWLRQV�DUH�SRVVLEOH

l z|{~}��������.�'���s�e�� D��5(&(,9,1*�SDUDPHWHUV�SRVVLEOH� E��9DULRXV�IRUPV�RI�GLUHFW�FDOO�SRVVLEOH�

w 029(��&$6(��/223w /RJLFDO�H[SUHVVLRQV��,)��(/6(,)��:+,/(��&+(&.��:$,7�w $ULWKPHWLF�H[SUHVVLRQV�DQG�ELW�H[SUHVVLRQV��&20387(��

VHH�H[DPSOH��D� �E���F

D� �E��� )XQFWLRQDO�PHWKRG

��� i�km�����

n Methods that have a RETURNING parameter are described as functional methods. These methodscannot have EXPORTING or CHANGING parameters, but has many (or as few) IMPORTINGparameters and exceptions as required.

n Functional methods can be used directly in various expressions:

• Logical expressions (IF, ELSEIF, WHILE, CHECK, WAIT)

• The CASE statement (CASE, WHEN)

• The LOOP statement

• Arithmetic expressions (COMPUTE)

• Bit expressions (COMPUTE)

• The MOVE statement.

Page 202: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-32

SAP AG 2002

)XQFWLRQDO�0HWKRGV��([DPSOH

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����0(7+2'6��JHWBDYHUDJHBIXHO���������������������,03257,1*�LPBGLVWDQFH����7<3(�VBGLVWDQFH��������������������������������LPBIXHO �7<3(�W\BIXHO �����������������������5(7851,1*�9$/8(�UHBIXHO��7<3(�W\BIXHO�(1'&/$66��

'$7$��UBYHKLFOH��7<3(�5()�72�OFOBYHKLFOH��������UBYHKLFOH��7<3(�5()�72�OFOBYHKLFOH�������DYJBIXHO�7<3(�W\BIXHO�...

�H[DPSOH�IRU�VKRUW�V\QWD[�LQ�DULWPHW��RSHUDWLRQDYJBIXHO� ������UBYHKLFOH��!JHWBDYHUDJHBIXHO��LPBGLVWDQFH� �����LPBIXHO� ���������UBYHKLFOH��!JHWBDYHUDJHBIXHO��LPBGLVWDQFH� �����LPBIXHO� ������

n Depending on the number of IMPORTING parameters, the syntax for functional methods is asfollows (same for static functional methods):

• No IMPORTING parameters: ref->func_method( )

• Exactly 1 IMPORTING parameter: ref->func_method( p1 ) or ref->func_method( im_1 = p1 )

• Several IMPORTING parameters: ref->func_method( im_1 = p1 im_2 =p2 )

n Example of detailed syntax for functional method call:

CALL METHOD r_vehicle->get_average_fuel EXPORTING im_distance = 500 im_fuel = 50 RECEIVING re_fuel = avg_fuel.

Here, re_fuel is the formal parameter of the interface and avg_fuel is the actual parameter ofthe calling program.

Page 203: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-33

SAP AG 2002

([WHUQDO�$FFHVV�WR�3XEOLF�$WWULEXWHV

&/$66�OFOBYHKLFOH�'(),1,7,21� 38%/,&�6(&7,21�����'$7$��PDNH��7<3(�VWULQJ�5($'�21/<�����������“just a demo ����&/$66�'$7$��QBRBYHKLFOHV�7<3(�L�5($'�21/<���“just a demo �����(1'&/$66������'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�'$7$��PDNHBQDPH�7<3(�VWULQJ�������FRXQW�7<3(�L�67$57�2)�6(/(&7,21� ���������������������������������������* PDLQ�SURJUDP��WKLQN�RI�D�FOLHQW��&5($7(�2%-(&7�UBYHKLFOH�PDNHBQDPH� �UBYHKLFOH�!PDNH�������������³RQO\�LI�SXEOLF�DWWU�FRXQW� �OFOBYHKLFOH !QBRBYHKLFOHV�������³RQO\�LI�SXEOLF�DWWU��

��� OFOBYHKLFOHPDNH�����

���&� }�{s�����}

There are different ways of accessing public attributes from outside the class:

n You access static attributes using <classname> !<class_attribute>n You access instance attributes using <instance>�!<instance_attribute>n ! and �! are the component selectors

Page 204: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-34

SAP AG 2002

&ODVVHV��DWWULEXWHV��DQG�PHWKRGV&ODVVHV��DWWULEXWHV��DQG�PHWKRGV

2EMHFWV��LQVWDQFHV�RI�FODVVHV2EMHFWV��LQVWDQFHV�RI�FODVVHV

$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV

7KH�FRQVWUXFWRU7KH�FRQVWUXFWRU

$GGLWLRQDO�SULQFLSOHV$GGLWLRQDO�SULQFLSOHV

3ULQFLSOHV����

Page 205: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-35

SAP AG 2002

&RQVWUXFWRU

l 6SHFLDO�PHWKRG�IRU�FUHDWLQJREMHFWV�ZLWK�GHILQHG�LQLWLDO�VWDWH

l 2QO\�KDV�,03257,1*SDUDPHWHUV�DQG�(;&(37,216

l ,V�H[HFXWHG�RQO\�RQFH�SHULQVWDQFH

&5($7(�2%-(&7

0(7+2'6�FRQVWUXFWRU�,03257,1*��LPBSDUDPHWHU!��������������������(;&(37,216��H[FHSWLRQ!�0(7+2'6�FRQVWUXFWRU�,03257,1*��LPBSDUDPHWHU!��������������������(;&(37,216��H[FHSWLRQ!�

OFOBYHKLFOH��PDNH��PRGHO��QBRBYHKLFOHV������FRQVWUXFWRU���

02143�5 6"5 7�8�9�:�; 6"5 9PrivatePublic

n The constructor is a special instance method in a class with the name constructor. The followingrules apply:

• Each class can have one constructor.

• The constructor is automatically called at runtime within the CREATE OBJECT statement.

• If you need to implement the constructor, then you must define and implement it in the PUBLICSECTION.

n When exceptions are raised in the constructor, instances are QRW created, so no main memory space isoccupied.

Page 206: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-36

SAP AG 2002

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����������0(7+2'6�FRQVWUXFWRU�,03257,1*�LPBPDNH���7<3(�VWULQJ����������������������������������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� ��&/$66�OFOBYHKLFOH�,03/(0(17$7,21���0(7+2'�FRQVWUXFWRU�����PDNH�� �LPBPDNH�����PRGHO� �LPBPRGHO�����$''���72�QBRBYHKLFOHV���(1'0(7+2'�(1'&/$66�

&RQVWUXFWRU��([DPSOH

'$7$�UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH����&5($7(�2%-(&7�UBYHKLFOH��������������(;3257,1*�LPBPDNH��� �)HUUDUL������������������������LPBPRGHO� �)���

�����2���m�/�2�*�������)���n H�)���2�) ¡��¢*¢

&DOO�IURP�WKHPDLQ�SURJUDP

n You need to implement the constructor when, for example:

• You need to allocate (external) resources

• You need to initialize attributes that cannot be covered by the VALUE addition to the DATAstatement

• You need to modify static attributes

n You cannot normally call the constructor explicitly.

Page 207: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-37

SAP AG 2002

3UHYLHZ��6WDWLF�&RQVWUXFWRU

&/$66�OFOBYHKLFOH�'(),1,7,21��38%/,&�6(&7,21����������&/$66�0(7+2'6��&/$66B&216758&725��35,9$7(�6(&7,21����&/$66�'$7$��QBRBYHKLFOHV�7<3(�,�(1'&/$66�

l 6SHFLDO�VWDWLF�PHWKRGl $XWRPDWLFDOO\�FDOOHG�EHIRUH

WKH�FODVV�LV�ILUVW�DFFHVVHGl 2QO\�H[HFXWHG�RQFH�SHU

SURJUDP&/$66�OFOBYHKLFOH�,03/(0(17$7,21���0(7+2'�&/$66B&216758&725�����...��(1'0(7+2'��...(1'&/$66�

�6WDWLF�&RQVWUXFWRU�LV�FDOOHG�EHIRUH�IROORZLQJ�VWDWHPHQWV� �GHPR��&5($7(�2%-(&7�UBYHKLFOH��������� �GHPR��FRXQWHU� �OFOBYHKLFOH !QBRBYHKLFOHV���³LI�DWWULEXWH�LV�SXEOLF �GHPR���OFOBYHKLFOH !JHWBFRXQW��LPSRUWLQJ�UHBFRXQW� �FRXQWHU���

n The static constructor is a special static method in a class with the name class_constructor. Itis executed precisely once per program. The static constructor of a class <classname> is calledautomatically 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 typeREF TO <classname>

• Addressing a static attribute using <classname>=><attribute>

• Calling a static attribute using CALL METHOD <classname>=><classmethod>

• Registering a static event handler method using SET HANDLER<classname>=><handler_method> FOR <obj>

• Registering an event handler method for a static event in class <classname>.

n The static constructor cannot be called explicitly.

Page 208: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-38

SAP AG 2002

3ULQFLSOHV����

&ODVVHV��DWWULEXWHV��DQG�PHWKRGV&ODVVHV��DWWULEXWHV��DQG�PHWKRGV

2EMHFWV��LQVWDQFHV�RI�FODVVHV2EMHFWV��LQVWDQFHV�RI�FODVVHV

$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV$FFHVVLQJ�DWWULEXWHV�DQG�PHWKRGV

7KH�FRQVWUXFWRU7KH�FRQVWUXFWRU

$GGLWLRQDO�SULQFLSOHV$GGLWLRQDO�SULQFLSOHV

Page 209: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-39

SAP AG 2002

1DPHVSDFH�:LWKLQ�D�&ODVV

l 7KH�VDPH�QDPHVSDFH�IRU£ $WWULEXWHV£ 0HWKRGV£ (YHQWV£ 7\SHV£ &RQVWDQWV£ $/,$6�QDPHV

l 7KHUH�LV�D�ORFDO�QDPHVSDFH�ZLWKLQPHWKRGV

OFOBYHKLFOH¤�¥S¦ §�¨E©�ªcomponents

« ªE©­¬�®�¯° ±³²E´ µ�±�µ�¶L·n¸L·C° ¹F¶

¤�º�»�¼r¦ ½components

$WWULEXWHPDNH0HWKRG

PDNH

n Within a class, attribute names, method names, event names, constant names, type names and aliasnames all share the same namespace.

n There is a local namespace within methods. Definitions of local variables can cover components inone class.

Page 210: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-40

SAP AG 2002

7KH�5HIHUHQFH�9DULDEOH�0(

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����������0(7+2'6�FRQVWUXFWRU��������,03257,1*�LPBPDNH��7<3(�VWULQJ�����������������LPBPRGHOO�7<3(�VWULQJ���35,9$7(�6(&7,21�����'$7$�PDNH�7<3(�VWULQJ��...(1'&/$66� ��

&/$66�OFOBYHKLFOH�,03/(0(17$7,21���0(7+2'�FRQVWUXFWRU���³just a demo����'$7$�PDNH�7<3(�VWULQJ�9$/8(�%HQ]�����&21&$7(1$7(�LPBPDNH�PDNH�,172�0(�!PDNH���(1'0(7+2'���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�

OFOBYHKLFOH¤�¥S¦ §�¨E©'ªcomponents

« ªE©­¬�®�¯° ±³²E´ µ�±�µ�¶L·n¸L·'° ¹�¶

¤�º�»+¼r¦ ½components

$WWULEXWHPDNH

0(�!PDNH

n You can address the object itself within instance methods using the implicitly available referencevariable me.

n Description of example:In the constructor, the instance attribute make is covered by the locally defined variable make. Inorder to still be able to address the instance attribute, you need to use me.

n The dummy method demonstrates how to call a class’s own method. You can omit the prefix me->.

n Other important use:An object calls another object’s method and passes its own address.

Page 211: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-41

SAP AG 2002

l &UHDWH�FODVVHVl &UHDWH�REMHFWVl &DOO�PHWKRGVl ([SODLQ�KRZ�WKH�JDUEDJH�FROOHFWRU�ZRUNV

<RX�DUH�QRZ�DEOH�WR�

3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJ��8QLW6XPPDU\

Page 212: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-42

([HUFLVHV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJ7RSLF� &UHDWLQJ�D�&ODVV

At the conclusion of these exercises, you will be able to:

• Create a local class

An airline needs to manage its airplanes.

0RGHO�VROXWLRQ� 6$3%&���B$,56B0$,1B$6$3%&���B$,56B$�include program

1-1 Create the package =%&���B���(where ##: is your group number) and save all therepository objects you have created during the course in this package.

1-2 Create the main program =%&���B��B0$,1B$,53/$1( and the includeprogram =%&���B��B$,53/$1( the main program contains (##: group number).

1-3 Create the class OFOBDLUSODQH�in the include program.

1-3-1 This class has two SULYDWH�LQVWDQFH�DWWULEXWHV:

- QDPH- SODQHW\SH.

The attribute for the airplane name should be of the type STRING. Definethe attribute for the plane type using the table field VDSODQH�SODQHW\SH.

1-3-2 The class has a SULYDWH�VWDWLF�DWWULEXWH: - QBRBDLUSODQHV.This attribute should be type I.

Page 213: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-43

1-3-3 The class has a SXEOLF�LQVWDQFH�PHWKRG VHWBDWWULEXWHV to set the privateinstance attributes name and plane type. Enter two corresponding importingparameters for the declaration of the method in the definition part. Thedefinition of these parameters should be analogous to the two attributes.Implement the method in the implementation part; set both attributes. Eachtime the method is called, the static attribute QBRBDLUSODQHV should increaseby 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 todisplay the instance attributes. Declare this method and, in theimplementation part, output the attributes using the WRITE statement (youcan also display the icon icon_ws_plane, in which case you must addTYPE-POOLS icon to the program.

1-3-5 Declare and implement a SXEOLF�VWDWLF�PHWKRG GLVSOD\BQBRBDLUSODQHV todisplay the static attribute QBRBDLUSODQHV.

(In the remaining exercises in this course, you can always start with the program youcreated in the previous exercise or copy the corresponding model solution and continueworking with this program.)

Page 214: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-44

([HUFLVHV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

3URJUDPPLQJ7RSLF� ,QVWDQWLDWLQJ�2EMHFWVAt the conclusion of these exercises, you will be able to:

• Instantiate objects

0RGHO�VROXWLRQ� 6$3%&���B$,56B0$,1B%6$3%&���B$,56B$�include program

2-1 Create a reference to the class OFOBDLUSODQH.

2-2 Define an internal table for buffering objects of the class OFOBDLUSODQH. The type of theinternal table should therefore be 5()�72�OFOBDLUSODQH.

2-3 Create several objects of the class OFOBDLUSODQH and store them in the internal table.

2-4 Follow the program flow in the Debugger.

(In the remaining exercises in this course, you can always start with the program youcreated in the previous exercise or copy the corresponding model solution andcontinue working with this program.)

Page 215: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-45

([HUFLVHV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

����������3URJUDPPLQJ7RSLF� 0HWKRG�&DOOVAt the conclusion of these exercises, you will be able to:

• Call instance methods and static methods

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

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 theplane 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.�Themethod must be public and have the UHBFRXQW (type I) return parameter (no inputparameters).

3-6 Test your functional method by calling it from the main program.

Page 216: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-46

([HUFLVHV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

3URJUDPPLQJ7RSLF� &RQVWUXFWRUAt the conclusion of these exercises, you will be able to:

• Create a constructor for a class

• Create an object using the constructor

0RGHO�VROXWLRQ� 6$3%&���B$,56B0$,1B'6$3%&���B$,56B'�include program

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 methodVHWBDWWULEXWHV, which you now no longer need.

4-1-1 The constructor must have two importing parameters that fill the instanceattributes QDPH�and SODQHW\SH.

4-1-2 The static attribute QBRBDLUSODQHV should have an ascending sequence ofone in the constructor.

4-2 In the method VHWBDWWULEXWHV, comment out the line in which the static attributeQBRBDLUSODQHV 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 theobject with the constructor interface.Fill the constructor’s interface parameters with the same values you used whencalling the VHWBDWWULEXWHV�method.

4-4 �2SWLRQDO�Comment out the method call VHWBDWWULEXWHV or delete the call.

Page 217: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-47

([HUFLVHV8QLW��3ULQFLSOHV�RI�2EMHFW�2ULHQWHG�3URJUDPPLQJ7RSLF��&DOOLQJ�D�3ULYDWH�0HWKRG

At the conclusion of these exercises, you will be able to:

• Call methods within a class

0RGHO�VROXWLRQ� 6$3%&���B$,56B0$,1B(6$3%&���B$,56B(�include program

5 Create the SULYDWH�PHWKRG JHWBWHFKQLFDOBDWWULEXWHV for the class OFOBDLUSODQH�(inyour include program =%&���B��B$,53/$1().

5-1 The airplane type is the import parameter. (Type saplane-planetype)

5-2 The export parameter is the weight WEIGHT and the tank capacity TANKCAP.Refer to the types in the table SAPLANE.

5-3 The result, both export parameters are ascertained in a database access to thetable SAPLANE.Therefore, both attributes are read, depending on the airplane type.

5-4 If the imported plane type does not exist in the table, set default values (weight100000, tank capacity 10000).

5-5 Test your method JHWBWHFKQLFDOBDWWULEXWHV:

5-5-1 By calling it from the main program

5-5-2 By calling it within the GLVSOD\BDWWULEXWHV method

5-3-3 Which call was successful, and why?

Page 218: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-48

�6ROXWLRQV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

3URJUDPPLQJ7RSLF� &UHDWLQJ�D�&ODVV

*&---------------------------------------------------------------------*

*& Report SAPBC401_AIRS_MAIN_a *

*&---------------------------------------------------------------------*

5(3257��VDSEF���BDLUVBPDLQBD�

7<3(�322/6�LFRQ�,1&/8'(�6$3%&���BDLUVB$�

*&-----------------------------------------------------------------*

*& Include SAPBC401_AIRS_A *

*&-----------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

&/$66�OFOBDLUSODQH�'(),1,7,21�

��38%/,&�6(&7,21����������������������������������������&2167$176��SRVB��7<3(�L�9$/8(����

����0(7+2'6��VHWBDWWULEXWHV�,03257,1*�������������������LPBQDPH������7<3(�VWULQJ�������������������LPBSODQHW\SH�7<3(�VDSODQH�SODQHW\SH��������������GLVSOD\BDWWULEXWHV�

����&/$66�0(7+2'6��GLVSOD\BQBRBDLUSODQHV�

��35,9$7(�6(&7,21������������������������������������������'$7$��QDPH������7<3(�VWULQJ�����������SODQHW\SH�7<3(�VDSODQH�SODQHW\SH�

����&/$66�'$7$��QBRBDLUSODQHV�7<3(�L�

Page 219: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-49

(1'&/$66�

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

&/$66�OFOBDLUSODQH�,03/(0(17$7,21�

��0(7+2'�VHWBDWWULEXWHV�����QDPH���������� �LPBQDPH�����SODQHW\SH����� �LPBSODQHW\SH�����QBRBDLUSODQHV� �QBRBDLUSODQHV�������(1'0(7+2'�

��0(7+2'�GLVSOD\BDWWULEXWHV�����:5,7(����LFRQBZVBSODQH�DV�LFRQ��������������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��������������$7�SRVB��QBRBDLUSODQHV�/()7�-867,),('������(1'0(7+2'�

(1'&/$66�

Page 220: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-50

6ROXWLRQV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

3URJUDPPLQJ7RSLF� ,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�������SODQHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBDLUSODQH�

START-OF-SELECTION.

*##############################

��&5($7(�2%-(&7�UBSODQH���$33(1'�UBSODQH�72�SODQHBOLVW�

��&5($7(�2%-(&7�UBSODQH���$33(1'�UBSODQH�72�SODQHBOLVW�

��&5($7(�2%-(&7�UBSODQH���$33(1'�UBSODQH�72�SODQHBOLVW�

Page 221: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-51

6ROXWLRQV

8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG3URJUDPPLQJ

7RSLF� 0HWKRG�&DOOV

*&---------------------------------------------------------------------*

*& Report SAPBC401_AIRS_MAIN_C *

*&---------------------------------------------------------------------*

*& call method set_Attributes to initialize objects *

*& and visualize content of objects via display_attributes *

*&---------------------------------------------------------------------*

REPORT sapbc401_airs_main_c.

TYPE-POOLS icon.

INCLUDE sapbc401_airs_c.

'$7$��UBSODQH�7<3(�5()�72�OFOBDLUSODQH�������SODQHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBDLUSODQH�������FRXQW�W\SH�L�

START-OF-SELECTION.

*##############################

��OFOBDLUSODQH !GLVSOD\BQBRBDLUSODQHV����

��&5($7(�2%-(&7�UBSODQH���$33(1'�UBSODQH�72�SODQHBOLVW���UBSODQH�!VHWBDWWULEXWHV��LPBQDPH� �/+�%HUOLQ���������������������������LPBSODQHW\SH� �����������

��&5($7(�2%-(&7�UBSODQH���$33(1'�UBSODQH�72�SODQHBOLVW���UBSODQH�!VHWBDWWULEXWHV��LPBQDPH� �$$�1HZ�<RUN���������������������������LPBSODQHW\SH� �����������

��/223�$7�SODQHBOLVW�,172�UBSODQH�����UBSODQH�!GLVSOD\BDWWULEXWHV������(1'/223�

Page 222: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-52

OFOBDLUSODQH !GLVSOD\BQBRBDLUSODQHV����

�FDOOLQJ�WKH�IXQFWLRQDO�PHWKRG�JHWBQBRBDLUSODQHV �ORQJ�V\QWD[�IRU�IXQFWLRQDO�FDOO� �OFOBDLUSODQH !JHWBQBRBDLUSODQHV��UHFHLYLQJ�UHBFRXQW� �FRXQW��� �VKRUW�V\QWD[�IRU�IXQFWLRQDO�FDOO�FRXQW� �OFOBDLUSODQH !JHWBQBRBDLUSODQHV����ZULWH����1XPEHU�RI�DLUSODQHV�����FRXQW�

*&---------------------------------------------------------------------*

*& Include SAPBC401_AIRS_C *

*& Develops functional static method get_n_o_airplanes *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

&/$66�OFOBDLUSODQH�'(),1,7,21�

��38%/,&�6(&7,21����������������������������������������&2167$176��SRVB��7<3(�L�9$/8(����

����0(7+2'6��VHWBDWWULEXWHV�,03257,1*�������������������LPBQDPH������7<3(�VWULQJ�������������������LPBSODQHW\SH�7<3(�VDSODQH�SODQHW\SH��������������GLVSOD\BDWWULEXWHV�

����&/$66�0(7+2'6��GLVSOD\BQBRBDLUSODQHV��������������������JHWBQBRBDLUSODQHV�UHWXUQLQJ�9$/8(�UHBFRXQW��W\SH�,�

��35,9$7(�6(&7,21������������������������������������������'$7$��QDPH������7<3(�VWULQJ�����������SODQHW\SH�7<3(�VDSODQH�SODQHW\SH�

����&/$66�'$7$��QBRBDLUSODQHV�7<3(�L�

(1'&/$66�

Page 223: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-53

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

&/$66�OFOBDLUSODQH�,03/(0(17$7,21�

��0(7+2'�VHWBDWWULEXWHV�����QDPH���������� �LPBQDPH�����SODQHW\SH����� �LPBSODQHW\SH�����QBRBDLUSODQHV� �QBRBDLUSODQHV�������(1'0(7+2'�

��0(7+2'�GLVSOD\BDWWULEXWHV�����:5,7(����LFRQBZVBSODQH�DV�LFRQ��������������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��������������$7�SRVB��QBRBDLUSODQHV�/()7�-867,),('������(1'0(7+2'�

��PHWKRG�JHWBQBRBDLUSODQHV�����UHBFRXQW� �QBRBDLUSODQHV���HQGPHWKRG�

(1'&/$66�

Page 224: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-54

6ROXWLRQV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

3URJUDPPLQJ7RSLF� &RQVWUXFWRU

*&---------------------------------------------------------------------*

*& Report SAPBC401_AIRS_MAIN_d *

*&---------------------------------------------------------------------*

*& call constructor of class lcl_airplane *

*&---------------------------------------------------------------------*

REPORT sapbc401_airs_main_d.

TYPE-POOLS icon.

INCLUDE sapbc401_airs_d.

'$7$��UBSODQH�7<3(�5()�72�OFOBDLUSODQH�������SODQHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBDLUSODQH�

START-OF-SELECTION.

*##############################

lcl_airplane=>display_n_o_airplanes( ).

��&5($7(�2%-(&7�UBSODQH�H[SRUWLQJ�LPBQDPH� �/+�%HUOLQ����������������������������������LPBSODQHW\SH� ��������� APPEND r_plane TO plane_list.

��&5($7(�2%-(&7�UBSODQH�H[SRUWLQJ�LPBQDPH� �$$�1HZ�<RUN����������������������������������LPBSODQHW\SH� ���������

LOOP AT plane_list INTO r_plane.

r_plane->display_attributes( ).

ENDLOOP.

lcl_airplane=>display_n_o_airplanes( ).

Page 225: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-55

*&---------------------------------------------------------------------*

*& Include SAPBC401_AIRS_d *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

"--------------------------------

CONSTANTS: pos_1 TYPE i VALUE 30.

����0(7+2'6��FRQVWUXFWRU�,03257,1*�������������������LPBQDPH������7<3(�VWULQJ�������������������LPBSODQHW\SH�7<3(�VDSODQH�SODQHW\SH��������������GLVSOD\BDWWULEXWHV�

CLASS-METHODS: display_n_o_airplanes.

PRIVATE SECTION.

"----------------------------------

DATA: name TYPE string,

planetype TYPE saplane-planetype.

CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS.

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

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.

Page 226: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-56

WRITE: /, / ’Number of airplanes: ’(ca1),

AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.

ENDMETHOD.

ENDCLASS.

Page 227: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-57

6ROXWLRQV8QLW� 3ULQFLSOHV�RI�2EMHFW�2ULHQWHG

3URJUDPPLQJ7RSLF� &DOOLQJ�D�3ULYDWH�0HWKRG

*&---------------------------------------------------------------------*

*& Report SAPBC401_AIRS_MAIN_e *

*& *

*&---------------------------------------------------------------------*

*& inside display_attributes a private method is called to get *

*& further details on technical aspects of the planetype *

*&---------------------------------------------------------------------*

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

Page 228: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-58

*&---------------------------------------------------------------------*

*& Include SAPBC401_AIRS_e *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

"--------------------------------

CONSTANTS: pos_1 TYPE i VALUE 30.

METHODS: constructor IMPORTING

im_name TYPE string

im_planetype TYPE saplane-planetype,

display_attributes.

CLASS-METHODS: display_n_o_airplanes.

PRIVATE SECTION.

"----------------------------------

����0(7+2'6��JHWBWHFKQLFDOBDWWULEXWHV�����������������,03257,1*�LPBW\SH�����W\SH�VDSODQH�SODQHW\SH�����������������(;3257,1*�H[BZHLJKW���7<3(�VBSODQBZHL���������������������������H[BWDQNFDS��7<3(�VBFDSDFLW\�

DATA: name TYPE string,

planetype TYPE saplane-planetype.

CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS. "lcl_airplane DEFINITION

Page 229: 003 - Bc401 - Abap Objects

© SAP AG BC401 7-59

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

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�����6(/(&7�6,1*/(�ZHLJKW�WDQNFDS�)520�VDSODQH��������������,172��H[BZHLJKW��H[BWDQNFDS���������������:+(5(�SODQHW\SH� �LPBW\SH�����,)�V\�VXEUF��!���������H[BZHLJKW� ��������������H[BWDQNFDS� �����������(1',)���(1'0(7+2'����������������������JHWBWHFKQLFDOBDWWULEXWHV

ENDCLASS. "lcl_airplane IMPLEMENTATION

Page 230: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-1

SAP AG 2002

l *HQHUDOL]DWLRQ���VSHFLDOL]DWLRQ�RI�FODVVHV&RQWHQWV�

,QKHULWDQFH

Page 231: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-2

SAP AG 2002

l 'HILQH�DQ�LQKHULWDQFH�UHODWLRQVKLS�EHWZHHQFODVVHV

l 5HGHILQH�PHWKRGV

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

,QKHULWDQFH��8QLW�2EMHFWLYHV

Page 232: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-3

SAP AG 2002

,QKHULWDQFH��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �� ��� ��� ����� � � � � ����� � � � � ! � � � ��� � ��" � � � � ��# # � � $&%�' ' ( )�*,+.-�/ * - %�021% )43 + % 1 1 ( ) *,5 %�6 %

798�: ;�<>= ;�? @BA ;�C�DE C�F D @ 8G C ? C E�HBI @ 8 C ;�JG C ? CLK FNM @�O ? 8 : ;G @ ? C : D P.Q9R�S�T�U2VXW U S W�Y UBZ

[.\�] ^�_ ] `4a bdcLe4f4gihkj b _l m g \�] b ^ l bn [ \ e�o \ p�q q ] ^ orNsut2v�wix yz s|{ vrNsut2v�wix yz s|{ v

Page 233: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-4

SAP AG 2002

OFOBWUXFN��PD[BFDUJR��QBRBWDQNV��QBRBZKHHOV������JHWBFDUJR

,QKHULWDQFH��80/�([DPSOH

OFOBYHKLFOH��PDNH��PRGHOO��VHUBQR��FRORU��QBRBYHKLFOHV�FRQVWUXFWRU�GLVSOD\BDWWULEXWHV�JHWBFRXQW

OFOBFDU��FDUBW\SH���PD[BVHDWV��DFFHOHUWLRQ������JHWBW\SH

OFOBEXV��PD[BSDVVHQJHUV��ODYDWRU\��WHOHYLVLRQ������JHWBSDVVHQJHUV

�LV�D�UHODWLRQVKLS

n Inheritance is a relationship, in which one class (the subclass) inherits all the main characteristics ofanother class (the superclass). The subclass can also add new components (attributes, methods, andso on) and replace inherited methods with its own implementations.

n Inheritance is an implementation relationship that emphasizes similarities between classes. In theexample above, the similarities between the car, bus, and truck classes are extracted to the vehiclesuperclass. This means that common components are only defined/implemented in the superclass andare automatically present in the subclasses.

n The inheritance relationship is often described as an "is a" relationship: A truck LV�D vehicle.

Page 234: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-5

SAP AG 2002

0XOWLSOH�,QKHULWDQFH"

Spe

cial

izat

ion

Gen

eral

izat

ion

}�~���u����� ���N���N�u�u���4�������u�>�

OFOBVXSHU� OFOBVXSHU�

OFOBVXE� OFOBVXE� OFOBVXE�

n Inheritance should be used to implement generalization and specialization relationships. A superclassis a JHQHUDOL]DWLRQ�of its subclasses. The subclass in turn is a VSHFLDOL]DWLRQ of its superclasses.

n The situation in which a class, for example lcl_sub2, inherits from two classes (lcl_super1and lcl_super2) simultaneously, is known as multiple inheritance. However, this is QRWimplemented in ABAP Objects. ABAP Objects only has single inheritance.You can, however, simulate multiple inheritance in ABAP Objects using interfaces (see the sectionon interfaces).

n Single inheritance does not mean that the inheritance tree only has one level. On the contrary, thedirect superclass of one class can in turn be the subclass of a further superclass. In other words: Theinheritance 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.

n Inheritance is a "one-sided relationship": Subclasses know their direct superclasses, but(super)classes do not know their subclasses.

Page 235: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-6

SAP AG 2002

5HODWLRQVKLSV�%HWZHHQ�6XSHUFODVVHV�DQG�6XEFODVVHV

l &RPPRQ�FRPSRQHQWV�RQO\�H[LVW�RQFH�LQ�WKHVXSHUFODVV

� 1HZ�FRPSRQHQWV�LQ�WKH�VXSHUFODVV�DUHDXWRPDWLFDOO\�DYDLODEOH�LQ�VXEFODVVHV

� $PRXQW�RI�QHZ�FRGLQJ�LV�UHGXFHG��SURJUDPPLQJ�E\�GLIIHUHQFH��

l 6XEFODVVHV�DUH�H[WUHPHO\�GHSHQGHQW�RQVXSHUFODVVHV

� �:KLWH�%R[�5HXVH��6XEFODVV�PXVW�SRVVHVV�GHWDLOHG�NQRZOHGJH�RIWKH�LPSOHPHQWDWLRQ�RI�WKH�VXSHUFODVV

OFOBVXE

OFOBVXSHU

,QKHULWHG�FRPSRQHQWV

1HZ�FRPSRQHQWV

n If inheritance is used properly, it provides a significantly better structure, as FRPPRQ�FRPSRQHQWVRQO\�QHHG�WR�EH�VWRUHG�RQFH�FHQWUDOO\ (in the superclass) and are then automatically available tosubclasses. Subclasses also benefit from modifications (however, they can also be invalidated as aresult).

n Inheritance provides very strong links between the superclass and the subclass. The subclass mustpossess detailed knowledge of the implementation of the superclass, particularly for redefinition, butalso in order to use inherited components. Even if the superclass does not technically know itssubclasses, the subclass often makes additional requirements of the superclass, for example, becausea subclass needs certain protected components or because implementation details in the superclassneed to be changed in the subclass in order to redefine methods. The basic reason is that thedeveloper of a (super)class cannot normally predict all the requirements that subclasses will laterneed to make of the superclass.

Page 236: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-7

SAP AG 2002

,QKHULWDQFH��6\QWD[

&/$66�OFOBYHKLFOH�'(),1,7,21��38%/,&�6(&7,21����0(7+2'6��HVWLPDWHBIXHO�����������������������,03257,1*�LPBGLVWDQFH����7<3(�VBGLVWDQFH�����������������������5(7851,1*�9$/8(�UHBIXHO��7<3(�W\BIXHO��35,9$7(�6(&7,21�����'$7$��PDNH��7<3(�VWULQJ� ...

(1'&/$66�&/$66�OFOBWUXFN�'(),1,7,21�,1+(5,7,1*�)520�OFOBYHKLFOH���38%/,&�6(&7,21�����0(7+2'6��JHWBFDUJR�5(7851,1*�9$/8(�UHBFDUJR��7<3(�W\BFDUJR���35,9$7(�6(&7,21�����'$7$��PD[BFDUJR�7<3(�W\BFDUJR�(1'&/$66�

n Normally the only other entry required for subclasses is what has changed in relation to the directsuperclass. Only DGGLWLRQV are permitted in ABAP Objects, that is, in a subclass you can "never takesomething away from a superclass". All components from the superclass are automatically present inthe subclass.

n The attributes of the superclass lcl_vehicle exist in the subclass lcl_truck; the methodestimate_fuel is also available in the subclass.

n The subclass defines a method get_cargo. It is not visible in the superclass.

Page 237: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-8

SAP AG 2002

&/$66�OFOBWUXFN�,03/(0(17$7,21���0(7+2'�HVWLPDWHBIXHO��������������VXSHU�!HVWLPDWHBIXHO�������(1'0(7+2'�(1'&/$66�

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

1HZLPSOHPHQWDWLRQ�RIPHWKRG$OZD\V�SRLQWV�WR�WKH

VXSHUFODVV�FRPSRQHQW

n The REDEFINITION statement for the inherited method must be in the same SECTION as thedefinition of the original method. (It can therefore not be in the PRIVATE SECTION, since a class’sprivate methods are not visible and therefore cannot be redefined in subclasses).

n If you redefine a method, you do not need to enter its interface again in the subclass, but only thename of the method. The reason for this is that ABAP Objects does not support overloading.

n In the case of redefined methods, changing the interface (RYHUORDGLQJ) is not permitted; exception:Overloading is possible with the constructor.

n Within the redefined method, you can access components of the direct superclass using the SUPERreference.

n To implement a redefined method in a subclass, you often need to call the method of the same namein the LPPHGLDWH superclass. In ABAP Objects you can call the method from the superclass using thepseudo-reference super:The pseudo-reference super can only be used in redefined methods.

Page 238: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-9

SAP AG 2002

,QKHULWDQFH�DQG�5HGHILQLQJ�WKH�&RQVWUXFWRU

&/$66�OFOBYHKLFOH�'(),1,7,21��38%/,&�6(&7,21���0(7+2'6��FRQVWUXFWRU�,03257,1*�������������LPBPDNH�7<3(�VWULQJ�(1'&/$66�

&/$66�OFOBWUXFN�'(),1,7,21�,1+(5,7,1*�)520�OFOBYHKLFOH���38%/,&�6(&7,21�����0(7+2'6��FRQVWUXFWRU�,03257,1*�LPBPDNH��7<3(�VWULQJ�����������������������������������LPBFDUJR�7<3(�W\BFDUJR���35,9$7(�6(&7,21�����'$7$��PD[BFDUJR�7<3(�W\BFDUJR�(1'&/$66�

&/$66�OFOBYHKLFOH�,03/(0(17$7,21���0(7+2'�&216758&725�����PDNH� �LPBPDNH���(1'0(7+2'�(1'&/$66�

&/$66�OFOBWUXFN�,03/(0(17$7,21���0(7+2'�FRQVWUXFWRU�����&$//�0(7+2'�VXSHU�!FRQVWUXFWRU��LPBPDNH� �LPBPDNH�������PD[BFDUJR� �LPBFDUJR���(1'0(7+2'�(1'&/$66�

&DQ�FKDQJH�WKHLQWHUIDFH

&DOO�FRQVWUXFWRU�RI�LPPHGLDWHVXSHUFODVV

n The constructor of the superclass PXVW be called within the constructor of the subclass. The reasonfor this is the special function of the constructor: To ensure that objects are initialized correctly. Onlythe class itself, however, can initialize its own (private) components correctly; this task cannot becarried out by the subclass. Therefore it is essential that all (instance) constructors are called in aninheritance hierarchy (in the correct sequence).

n For static constructors, unlike instance constructors, the static constructor in the superclass is calledautomatically, that is the runtime system automatically ensures that the static constructors of all itssuperclasses have already been executed before the static constructor in a particular class is executed.

Page 239: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-10

SAP AG 2002

'$7$��UB��7<3(�5()�72�OFOB���������UB��7<3(�5()�72�OFOB��&5($7(�2%-(&7�UB��(;3257,1*�LPBD�� �����&5($7(�2%-(&7�UB��(;3257,1*�LPBD�� ��������������������������������LPBD�� ������

5XOHV�IRU�&DOOLQJ�WKH�&RQVWUXFWRU

l� �����������������&ODVV�RI�LQVWDQFH�WR�EH�FUHDWHG������ �FRQVWUXFWRU������⇒�)LOO�LWV�SDUDPHWHUVl� �����������������&ODVV�RI�LQVWDQFH�WR�EH�FUHDWHG������ ��¡  �FRQVWUXFWRU������⇒�6HDUFK�IRU�WKH�QH[W�VXSHUFODVV�ZLWK�D�FRQVWUXFWRU

LQ�WKH�LQKHULWDQFH�WUHH������⇒�)LOO�LWV�SDUDPHWHUV

OFOB�a1constructor(im_a1:i)

OFOB�

OFOB�a2constructor(im_a1, im_a2)

n The model described for instance constructors must also be taken into account for CREATEOBJECT.

n There are two main methods of creating an instance of a class using CREATE OBJECT:

• 1. The class has a defined (and implemented) instance constructor

In this case, when you are using CREATE OBJECT, the parameters have to be filled accordingto the constructor interface, that is, optional parameters may, and non-optional parameters mustbe filled with actual parameters. If the constructor does not have any (formal) parameters, noparameters may or can be filled.

• 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 whichthe instance constructor has been defined and implemented. Then, when you are using CREATEOBJECT, 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 befilled.

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

Page 240: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-11

SAP AG 2002

&/$66�OFOBYHKLFOH�'(),1,7,21��38%/,&�6(&7,21���...�3527(&7('�6(&7,21����'$7$�WDQN�7<3(�5()�72�OFOBWDQN��35,9$7(�6(&7,21�����'$7$�PDNH�7<3(�VWULQJ�(1'&/$66�

,QKHULWDQFH�DQG�9LVLELOLW\

l 3XEOLF�FRPSRQHQWV¢ 9LVLEOH�WR�DOO¢ 'LUHFW�DFFHVV

l 3URWHFWHG�FRPSRQHQWV¢ 2QO\�YLVLEOH�ZLWKLQ�WKH�FODVV

DQG�LWV�VXEFODVVHVl 3ULYDWH�FRPSRQHQWV

¢ 2QO\�YLVLEOH�ZLWKLQ�WKH�FODVV¢ 1R�DFFHVV�IURP�RXWVLGH�WKH

FODVV�QRW�HYHQ�IURP�WKH�VXEFODVV

OFOBYHKLFOH��PDNH��PRGHOO��WDQN�...

�FRQVWUXFWRU�GLVSOD\BDWWULEXWHV�JHWBFRXQW

£ ����¤u�N�N�¥ ��� ~ �¦�,�§���>¨© �ª���N«|�¬���

n Inheritance provides an extension of the visibility concept: There are protected components. Thevisibility of these components lies between that of the public components (visible to all users, allsubclasses, and the class itself), and private (visible only to the class itself). Protected componentsare visible to and can be used by all subclasses and the class itself.

n Subclasses cannot access the private components (particularly attributes) of the superclass. Privatecomponents are genuinely private. This is particularly important if a (super)class needs to make localenhancements to handle errors: It can use private components to do this without knowing orinvalidating subclasses.

n In ABAP Objects, you must keep to the section sequence PUBLIC, PROTECTED, PRIVATE.

Page 241: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-12

SAP AG 2002

��� OFOBEXVPrivatePublic

Protected

&DQ�EH�DGGUHVVHG�IURP�RXWVLGH��IRUDOO�FOLHQWV

&DQ�RQO\�EHDGGUHVVHG�ZLWKLQWKH�FODVV

,QKHULWHG

WDQN

JHWBPDNHVHWBPDNHJHWBFRXQWGLVSOD\BDWWULEXWHVHVWLPDWHBIXHO

,QKHULWHG

9LVLELOLW\�RI�3URWHFWHG�&RPSRQHQWV

n In this example, lcl_bus, a subclass of lcl_vehicle, can directly access the protected attributetank. If the attribute was private, the subclasses would only be able to access tank using non-private methods.

Page 242: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-13

SAP AG 2002

5XOHV�IRU�5HGHILQLQJ�0HWKRGV

l ,QKHULWHG�PHWKRGV�FDQ�EH�UHGHILQHG�LQ�VXEFODVVHV¢ 5HGHILQHG�PHWKRGV�PXVW�EH�UH�LPSOHPHQWHG�LQ�VXEFODVVHV¢ 7KH�VLJQDWXUH�RI�UHGHILQHG�PHWKRGV�FDQQRW�EH�FKDQJHG¢ 6WDWLF�PHWKRGV�FDQQRW�EH�UHGHILQHG¢ ,Q�LQKHULWDQFH��VWDWLF�FRPSRQHQWV�DUH��VKDUHG��

$�FODVV�VKDUHV�LWV�QRQ�SULYDWH�VWDWLF�DWWULEXWHV�ZLWK�DOO�LWV�VXEFODVVHV

OFOBYHKLFOHV

OFOBFDU OFOBWUXFN OFOBEXV

HVWLPDWHBIXHO

­ �u�®�����¯� ~ ¨��N°±�N�²���¯�,�³�>�����,¨��N�´¨u�Nµ�µd�,���|���>¶·�,¸,°¹»º�¼¾½4¿»À�¼�¹�ÁLÂ&ÃX¹iÄ ¹»º¾¼¾½�¿�À�¼9¹�Á2Â&Ãi¹�Ä ¹»º¾¼¾½�¿»À�¼.¹4ÁLÂ.Ãi¹�Ä

n In ABAP Objects, you can not only add new components, but also provide inherited methods withnew 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.

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

n You should not confuse redefinition with "overloading". The latter describes the ability of a class tohave methods with the same name but a different signature. This is not available in ABAP Objects.

n There is only one static event per roll area. In this way, a class that defines a public or protectedstatic attribute shares this attribute with all its subclasses. The significant point here is that subclassesdo not each receive their own copy of the static attribute.

Page 243: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-14

SAP AG 2002

5HGHILQLQJ�0HWKRGV��([DPSOH

0(7+2'�HVWLPDWHBIXHO���'$7$��WRWDOBZHLJKW����* just an example!��WRWDOBZHLJKW� �PD[BFDUJR������������������������ZHLJKW�����UHBIXHO� �WRWDOBZHLJKW� �������������LPBGLVWDQFH� �IDFWRU�(1'0(7+2'�

0(7+2'�HVWLPDWHBIXHO��'$7$��WRWDOBZHLJKW����* just an example!�WRWDOBZHLJKW� �PD[BSDVVHQJHUV� ���������DYHUDJHBZHLJKW���ZHLJKW��UHBIXHO� �WRWDOBZHLJKW� ������������LPBGLVWDQFH� �IDFWRU��(1'0(7+2'�

OFOBEXV��PD[BSDVVHQJHUV��...

��FRQVWUXFWRU��HVWLPDWHBIXHO

OFOBWUXFN��PD[BFDUJR�...

��FRQVWUXFWRU��HVWLPDWHBIXHO

n In the above example, both redefined methods calculate the return code in different ways. Theimportant point is that the semantics stay the same.

Page 244: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-15

SAP AG 2002

l 'HILQH�DQ�LQKHULWDQFH�UHODWLRQVKLS�EHWZHHQFODVVHV

l 5HGHILQH�PHWKRGV

<RX�DUH�QRZ�DEOH�WR�

,QKHULWDQFH��8QLW�6XPPDU\

Page 245: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-16

�,QKHULWDQFH�([HUFLVHV8QLW��,QKHULWDQFH7RSLF��&UHDWLQJ�&ODVV�+LHUDUFKLHV

At the conclusion of these exercises, you will be able to:

• Define subclasses

• Redefine superclass methods in subclasses

0RGHO�VROXWLRQ� 6$3%&���B,1+6B0$,1B$6$3%&���B,1+6B$�include program

<RXU�SURJUDP� =%&���B��B0$,1B$,53/$1(=%&���B��B$,53/$1(�include program

1-1 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, createthis subclass in your include program.

1-2-1 The class is to have a private instance attribute PD[BVHDWV with the sametype as table field VIOLJKW�VHDWVPD[.

1-2-2 A public constructor is to be defined and implemented in the class. Thisconstructor 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 instanceattributes.

1-3 Create the subclass OFOBFDUJRBSODQH for the class OFOBDLUSODQH. Also, create thissubclass in your include program.

1-3-1 The class is to have a private instance attribute PD[BFDUJR with thesame type as the table field VFSODQH�FDUJRPD[.

1-3-2 A public constructor is to be defined and implemented in the class. Thisconstructor 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 instanceattributes.

Page 246: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-17

1-4 Switch to your main program.

1-4-1 Use the DATA statement to create a reference for each subclass(OFOBSDVVHQJHUBSODQH, OFOBFDUJRBSODQH).

1-4-2 Call the static method GLVSOD\BQBRBDLUSODQHV (before instantiating anyobjects).

1-4-3 Use the two references to create one instance each of the subclassesOFOBSDVVHQJHUBSODQH and OFOBFDUJRBSODQH. Decide for yourself how tofill the attributes.

1-4-4 Call the GLVSOD\BDWWULEXWHV�method for both instances.

1-4-5 Call the static method GLVSOD\BQBRBDLUSODQHV a second time.

1-5 Follow the program flow in the Debugger, paying special attention to the call ofGLVSOD\BDWWULEXWHV.

1-6 Can the method JHWBWHFKQLFDOBDWWULEXWHV�be called directly from the redefinedmethod�GLVSOD\BDWULEXWHV of the subclass?

1-7 Is it necessary for the subclasses to directly access the attributes QDPH�andSODQHW\SH of the superclass to initialize them?

Or, to formulate it differently:If these attributes remained in the private visibility area of the superclass, howwould the subclasses have to access the attributes?

Page 247: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-18

�,QKHULWDQFH�6ROXWLRQV8QLW� ,QKHULWDQFH7RSLF� &UHDWLQJ�&ODVV�+LHUDUFKLHV

*&---------------------------------------------------------------------*

*& Report SAPBC401_INHS_MAIN_a *

*&---------------------------------------------------------------------*

*& the classes lcl_passenger_plane and lcl_cargo_plane are *

*& instantiated. Inheritance is shown *

*&---------------------------------------------------------------------*

REPORT sapbc401_inhs_main_a.

INCLUDE <icon>.

INCLUDE sapbc401_inhs_a.

'$7$��UBSODQH�7<3(�5()�72�OFOBDLUSODQH�������UBFDUJR�W\SH�UHI�WR�OFOBFDUJRBSODQH�������UBSDVVHQJHU�W\SH�UHI�WR�OFOBSDVVHQJHUBSODQH�������SODQHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBDLUSODQH�

START-OF-SELECTION.

*##############################

lcl_airplane=>display_n_o_airplanes( ).

��&5($7(�2%-(&7�UBSDVVHQJHU�(;3257,1*�������������������������LPBQDPH� �/+�%(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( ).

Page 248: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-19

*&---------------------------------------------------------------------*

*& Include SAPBC401_INHS_a *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

*...

*---------------------------------------------------------------------*

* CLASS lcl_cargo_plane DEFINITION

*---------------------------------------------------------------------*

*

*---------------------------------------------------------------------*

&/$66�OFOBFDUJRBSODQH�'(),1,7,21�,1+(5,7,1*�)520�OFOBDLUSODQH�

��38%/,&�6(&7,21��������������������������������0(7+2'6��FRQVWUXFWRU�,03257,1*�LPBQDPH�7<3(�VWULQJ�����������������������������������LPBSODQHW\SH�7<3(�VDSODQH�SODQHW\SH�����������������������������������LPBFDUJR�7<3(�VFSODQH�FDUJRPD[�����0(7+2'6��GLVSOD\BDWWULEXWHV�5('(),1,7,21�

��35,9$7(�6(&7,21��������������������������������'$7$��PD[BFDUJR�7<3(�VFSODQH�FDUJRPD[�

(1'&/$66����������������������OFOBFDUJRBSODQH�'(),1,7,21

Page 249: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-20

*---------------------------------------------------------------------*

* CLASS lcl_cargo_plane IMPLEMENTATION

*---------------------------------------------------------------------*

*

*---------------------------------------------------------------------*

&/$66�OFOBFDUJRBSODQH�,03/(0(17$7,21�

��0(7+2'�FRQVWUXFWRU�����&$//�0(7+2'�VXSHU�!FRQVWUXFWRU��LPBQDPH������ �LPBQDPH������������������������������������LPBSODQHW\SH� �LPBSODQHW\SH�������PD[BFDUJR� �LPBFDUJR���(1'0(7+2'����������������������FRQVWUXFWRU

��0(7+2'�GLVSOD\BDWWULEXWHV�����VXSHU�!GLVSOD\BDWWULEXWHV��������:5,7(����0D[�&DUJR� ���PD[BFDUJR�����8/,1(���(1'0(7+2'����������������������GLVSOD\BDWWULEXWHV(1'&/$66����������������������OFOBFDUJRBSODQH�,03/(0(17$7,21

*---------------------------------------------------------------------*

* CLASS lcl_passenger_plane DEFINITION

*---------------------------------------------------------------------*

*

*---------------------------------------------------------------------*

&/$66�OFOBSDVVHQJHUBSODQH�'(),1,7,21�,1+(5,7,1*�)520�OFOBDLUSODQH��

��38%/,&�6(&7,21�����0(7+2'6��FRQVWUXFWRU�,03257,1*�LPBQDPH�7<3(�VWULQJ�����������������������������������LPBSODQHW\SH�7<3(�VDSODQH�SODQHW\SH�����������������������������������LPBVHDWV�7<3(�VIOLJKW�VHDWVPD[�����0(7+2'6��GLVSOD\BDWWULEXWHV�5('(),1,7,21�

��35,9$7(�6(&7,21�����'$7$��PD[BVHDWV�7<3(�VIOLJKW�VHDWVPD[�(1'&/$66����������������������OFOBSDVVHQJHUBSODQH�'(),1,7,21

Page 250: 003 - Bc401 - Abap Objects

© SAP AG BC401 8-21

*---------------------------------------------------------------------*

* CLASS lcl_passenger_plane IMPLEMENTATION

*---------------------------------------------------------------------*

*

*---------------------------------------------------------------------*

&/$66�OFOBSDVVHQJHUBSODQH�,03/(0(17$7,21�

��0(7+2'�FRQVWUXFWRU�����&$//�0(7+2'�VXSHU�!FRQVWUXFWRU��LPBQDPH������ �LPBQDPH������������������������������������LPBSODQHW\SH� �LPBSODQHW\SH�������PD[BVHDWV� �LPBVHDWV���(1'0(7+2'����������������������FRQVWUXFWRU

��0(7+2'�GLVSOD\BDWWULEXWHV�����VXSHU�!GLVSOD\BDWWULEXWHV��������:5,7(����0D[�6HDWV� ���PD[BVHDWV�����8/,1(���(1'0(7+2'����������������������GLVSOD\BDWWULEXWHVENDCLASS. "lcl_passenger_plane IMPLEMENTATION

Page 251: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-1

SAP AG 2002

l &DVWl 3RO\PRUSKLVP

&RQWHQWV�

&DVWLQJ

Page 252: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-2

SAP AG 2002

l 8VH�FDVWVl 'HYHORS�JHQHULF�SURJUDPV�XVLQJ�SRO\PRUSKLVP

ZLWK�LQKHULWDQFH

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

&DVWLQJ��8QLW�2EMHFWLYHV

Page 253: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-3

SAP AG 2002

&DVWLQJ��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �

� ��� ��� ����� � � � � ����� � � � � ! � � � ��� � �#" � � � � ��$ $ � � % &�' ' ( )+*-,/.�0 * . &�132& )54 , & 2 2 ( ) *-6 &�7 &

8�9�: ;+<#= ;+> ?�@ ;�A�BC A�D B ? 9E A > A C�F�G ? 9 A ;�HE A > AJI DLK ?�M > 9 : ;E ? > A : B N�O�P�Q R�SUTWV S Q V�X SY

Z�[�\ ]�^ \ _+` ab3c5d5egfih a ^�j k e [�\ a ] j a�l Z [ c�m [ n�o o \ ] mp ]+q a [ \ j n ] ^ arts�uUv/wLx3yr s�u v w x y

Page 254: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-4

SAP AG 2002

&DVWLQJ����

1DUURZLQJ�FDVW1DUURZLQJ�FDVW

:LGHQLQJ�FDVW�:LGHQLQJ�FDVW�

,QKHULWDQFH�DQG�SRO\PRUSKLVP�,QKHULWDQFH�DQG�SRO\PRUSKLVP�

Page 255: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-5

SAP AG 2002

'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�������UBWUXFN���7<3(�5()�72�OFOBWUXFN���

&5($7(�2%-(&7�UBWUXFN�

�1DUURZLQJ�&DVWUBYHKLFOH� �UBWUXFN

3ULQFLSOH�RI�1DUURZLQJ�&DVW

z|{~}�z5�����

z�{��U�����L�#�L�

z�{��U�����L�#�L�

��� OFOBWUXFNPublic

*HHUEWJHWBPDNHJHWBFRXQW

GLVSOD\BDWWULEXWHVVHWBDWWULEXWHVHVWLPDWHBIXHO

5HGHILQLHUW

JHWBFDUJR���

n After the narrowing cast, you can use the r_vehicle reference to access the components of thetruck instance that were inherited from lcl_vehicle - obviously, in some cases with thelimitations entailed by their visibility. You can no longer access the truck-specific part of theinstance (get_cargo in the above example) using the r_vehicle reference.

Page 256: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-6

SAP AG 2002

'$7$��UBYHKLFOH������7<3(�5()�72�OFOBYHKLFOH�������UBWUXFN��������7<3(�5()�72�OFOBWUXFN�������OHYHO����������7<3(��������W\BOHYHO�

�JHQHULF�DFFHVV�WR�VXEFODVV�VHUYLFHVOHYHO� �UBWUXFN�!HVWLPDWHBIXHO��������� �1DUURZLQJ�&DVWUBYHKLFOH� �UBWUXFN� �8VH�RI�WKH�VXEFODVV�LQVWDQFH�LQ�WKH�VXSHUFODVV�FRQWH[WOHYHO� �UBYHKLFOH�!HVWLPDWHBIXHO���������

&RPSDWLELOLW\�DQG�1DUURZLQJ�&DVW

l ,QVWDQFHV�IURP�VXEFODVVHV�FDQ�EH�XVHG�LQ�DQ\�FRQWH[W��LQ�ZKLFK�WKHLQVWDQFHV�RI�WKH�VXSHUFODVV�DSSHDU

l 7KH�FRPSRQHQWV�LQKHULWHG�IURP�WKH�VXSHUFODVV�FDQ�EH�DGGUHVVHGl *HQHULF�DFFHVV�E\�WKH�FOLHQW�LV�SRVVLEOH

n One of the significant principles of inheritance is that an instance from a subclass can be used in anycontext, in which an instance from the superclass appears. This is possible because the subclass hasinherited 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.

n Variables that point to a superclass instance can also refer to subclass instances at runtime.

n The assignment of a subclass instance to a reference variable of the type "reference to superclass" isdescribed as a narrowing cast, because you are switching from a more detailed view to a one withless detail.

n The description "up-cast" is also used.

n What is a narrowing cast used for? A user who is not interested in the finer points of cars, trucks, andbusses (but only, for example, in the fuel consumption and tank gauge) does not need to know aboutthem. 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 anarrowing cast.

Page 257: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-7

SAP AG 2002

� � ��� �i� ��|� �

OFOBUHQWDO OFOBYHKLFOHV1..*1

� � �+� �i� �i� � ���i�

��� OFOBUHQWDOPrivatePublicLWDE

7KH�FOLHQW

OFOBWUXFN OFOBEXV

� � �+� �i� ��� �i�

*HQHULF�$FFHVV"

n A possible client (car rental company for example) wants to access the list of vehicles withidentically-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.

Page 258: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-8

SAP AG 2002

��� �i� ����L���� OFOBUHQWDOPrivatePublic

DGGBYHKLFOH � � �i� ��� � ���i�

��� �i� ��� ���

� � ¡�¢�£g¤��¦¥J¤ ¢�§�¨

...

0(7+2'6��DGGBYHKLFOH�,03257,1*�LPBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�...

7KH�FOLHQW

3UHSDUDWLRQV�IRU�*HQHULF�$FFHVV����

n Objects from different classes (lcl_bus, lcl_truck, and lcl_car in the above example ) canbe stored in an internal table consisting of references to the superclass (lcl_vehicle in the aboveexample),and then processed in a uniform manner using the same access technique.

n A client, the car rental company in this example, can then generically access the identically-namedservice of the different classes. (This will be discussed in more detail.)

Page 259: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-9

SAP AG 2002

&/$66�OFOBUHQWDO�'(),1,7,21��38%/,&�6(&7,21���0(7+2'6�DGGBYHKLFOH�,03257,1*�LPBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH���0(7+2'6�FDOFBHVWLPDWHGBIXHO�5(7851,1*�9$/8(�UHBIXHO��...�3527(&7('�6(&7,21���'$7$��YHKLFOHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBYHKLFOH�(1'&/$66�

&/$66�OFOBUHQWDO�,03/(0(17$7,21����0(7+2'�DGGBYHKLFOH����$33(1'�LPBYHKLFOH�72�YHKLFOHBOLVW��(1'0(7+2'��0(7+2'�FDOFBHVWLPDWHGBIXHO���'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH����/223�$7�YHKLFOHBOLVW�,172�UBYHKLFOH������UHBIXHO� �UHBIXHO���UBYHKLFOH�!HVWLPDWHBIXHO��LPBGLVWDQFH��.���(1'/223��(1'0(7+2'�(1'&/$66�

3UHSDUDWLRQV�IRU�*HQHULF�$FFHVV����

� When objects from different classes react differently to the same method call, this is known asSRO\PRUSKLVP. To do this, the classes implement the same method in different ways. This can bedone using inheritance, by redefining a method from the superclass in subclasses and implementingit differently. (,QWHUIDFHV will be discussed later; they too can enable polymorphic behavior.)

n When an instance receives a message to execute a particular method, then that method is executed ifit has been implemented by the class the instance belongs to. If the class has not implemented thatmethod, but only inherited and not redefined it, then a search up through the inheritance hierarchy iscarried out until an implementation of that method is found.

n The dynamic type, not the static type of the reference variable is used to search for theimplementation of a method (will be discussed later). r_vehicle->estimate_fuel abovetherefore uses the class of the instance that r_vehicle actually refers to to search for theimplementation of estimate_fuel. The static type for r_vehicle, which is always REF TOlcl_vehicle is not used.

n Polymorphism is one of the main strengths of inheritance: The user can work in the same way withdifferent classes, regardless of their implementation. The search for the right implementation of amethod is carried out by the runtime system, not the user.

Page 260: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-10

SAP AG 2002

0(7+2'�FDOFBHVWLPDWHGBIXHO��'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH��/223�$7�YHKLFOHBOLVW�LQWR�UBYHKLFOH���UHBIXHO� �UHBIXHO���UBYHKLFOH�!HVWLPDWHBIXHO��LPBGLVWDQFH����(1'/223�(1'0(7+2'�

3RO\PRUSKLVP���*HQHULF�$FFHVV

© ª�«-¬ ­�¬ ®�¯�°i±/­ ²Public³i´i´iµ·¶+¸�¹µ�´iº�´i»�¼¾½�¼L´Lµ�¸¿�ÀiÁ�Â�ÃÄ�ÁLÅ·Æ�ÇLÈ�Å|É

© Ê�«�¬ ­�¬ ®�Ë ±�ÌPublic³i´i´iµ·¶+¸-¹µ�´iº�´i»�¼¾½¼L´Lµ�¸¿�ÀiÁ|Â�ÃÄ�ÁLÅ·Æ�ÇLÈ�Å|É

0(7+2'�HVWLPDWHBIXHO���...� WRWDOBZHLJKW� �PD[BSDVVHQJHUV� ���������DYHUDJHBZHLJKW���ZHLJKW���UHBIXHO� �WRWDOBZHLJKW� ������������LPBGLVWDQFH� �IDFWRU�(1'0(7+2'�

0(7+2'�HVWLPDWHBIXHO��...���WRWDOBZHLJKW� �PD[BFDUJR���ZHLJKW�����UHBIXHO� �WRWDOBZHLJKW� �������������LPBGLVWDQFH� �IDFWRU�(1'0(7+2'�

� � ¡�¢�£g¤��¦¥J¤ ¢�§�¨

n Which coding is actually executed when estimate_fuel is called depends on the dynamic typeof the reference variable r_vehicle, that is it depends on which object from which (sub)classr_vehicle refers to.

n You can use polymorphism to write programs that are generic to a high degree and that do not evenneed to be changed if use cases are added. In the simple example above, this means that, should afurther subclass be added, for example for motorbikes, the above coding would not need to bechanged.A redefined method will be created in the server class lcl_motorbike.

Page 261: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-11

SAP AG 2002

6WDWLF�DQG�'\QDPLF�7\SHV�RI�5HIHUHQFHV

l 7KH�VWDWLF�W\SH�RI�D�UHIHUHQFH�YDULDEOH� ,V�GHILQHG�XVLQJ�7<3(�5()�72� 5HPDLQV�FRQVWDQW�WKURXJKRXW�WKH�SURJUDP�IORZ� 'HILQHV�ZKLFK�DWWULEXWHV�DQG�PHWKRGV�FDQ�EH�DGGUHVVHG

l 7KH�G\QDPLF�W\SH�RI�D�UHIHUHQFH�YDULDEOH� ,V�GHILQHG�E\�DVVLJQPHQW� &DQ�FKDQJH�GXULQJ�WKH�SURJUDP�UXQ� 'HILQHV�ZKLFK�FRGH�LV�WR�EH�H[HFXWHG�IRU

UHGHILQHG�PHWKRGV

'$7$��UBYHKLFOH����7<3(�5()�72�OFOBYHKLFOH�

Í Æ|Î�ÅLÏ�ÂLÐ�ÉLÅ

Ñ Ò ÓÔ� �L� �L� � ���i�Õ�½iÖ|´iµ|¼�¸¾´iº×¹µ�´iºi´�»�¼¾½�´iº¿�À�ÁiÂ�Ã�Ä�Á Å ÆÇ È|Å|É ¿�ÀiÁ�Â�ÃÄ�ÁLÅ·Æ�ÇLÈ�Å|É

Ñ � ÓÔ� �i� ��|� �Õ�½iÖ|´iµ|¼�¸¾´iº×¹µ�´iºi´�»�¼¾½�´iº¿�À�ÁiÂ�Ã�Ä�Á žÆÇ È|Å|É ¿�ÀiÁ|Â�ÃÄ�ÁLÅ·Æ�ÇLÈ�Å|É

n A reference variable always has two types, static and dynamic:

é The static type of a reference variable is determined by variable definition using TYPE REF TO.It cannot and does not change. It specifies which attributes and methods can be addressed

é The dynamic type of a reference variable is the type of the instance currently being referred to, it istherefore determined by assignment and can change during the program run. It defines what codeis to be executed for redefined methods.

n In the example, the static type of the r_vehicle variable is always REF TO lcl_vehicle, butits dynamic type after the cast is REF TO lcl_bus or REF TO lcl_truck.

n In the Debugger, the reference me can be used to determine the dynamic type.

Page 262: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-12

SAP AG 2002

�3URFHGXUDO�UHDOLVDWLRQ�RI�WKH�H[DPSOH�ZLWKRXW�SRO\PRUSKLVP

'$7$��YHKLFOHBOLVW���7<3(�7$%/(�2)�YHKLFOHBOLVWBW\SH�������YHKLFOH��������7<3(�YHKLFOHBOLVWBW\SH��������/223�$7�YHKLFOHBOLVW�,172�YHKLFOH���&$6(�YHKLFOH�FDWHJRU\�����:+(1�758&.�������3(5)250�HVWLPDWHBIXHOBWUXFN�86,1*��������������������������������������&+$1*,1*�IXHO�����:+(1�%86�������3(5)250�HVWLPDWHBIXHOBEXV�86,1*������������������������������������&+$1*,1*�IXHO���(1'&$6(���$''�IXHO�72�QHHGHGBIXHO�(1'/223���

name �FDWHJRU\���� ...YHKLFOHBOLVW

&RPSDULVRQ�WR�3URFHGXUDO�3URJUDPPLQJ

l ,Q�REMHFW�RULHQWHG�SURJUDPPLQJ��\RX�GR�QRW�QHHG�WR�FKDQJH�WKH�FRGLQJ�LI\RX�DGG�XVH�FDVHV

l ,Q�D�SURFHGXUDO�SURJUDP��\RX�ZRXOG�KDYH�WR�DGMXVW�&$6(�FRQVWUXFWLRQV�IRU�H[DPSOH�

truck1bus1bus2truck2

truck3

n Using polymorphism makes generic programming easier. Instead of implementing a CASE or IFstatement, you can have one access or call, which improves readability and does not need to bechanged if you extend the program by adding further subclasses.

Page 263: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-13

SAP AG 2002

1DUURZLQJ�FDVW1DUURZLQJ�FDVW

:LGHQLQJ�FDVW�:LGHQLQJ�FDVW�

,QKHULWDQFH�DQG�SRO\PRUSKLVP�,QKHULWDQFH�DQG�SRO\PRUSKLVP�

&DVWLQJ����

Page 264: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-14

SAP AG 2002

��� �i� ���|�L�

��� OFOBUHQWDOPrivatePublic

JHWBPD[BFDUJR

� � �i� �L� � �|�i�

� � �i� ��� ����g��¡�¢�£W¤��5¥U¤�¢�§W¨

7KH�FOLHQW

� � �i� �i� � ���i�ØÔÙ�Ú ¥3£ ÙWÛ�ÜgÝÞàß�á

ØâÙ�Ú ¥J£ ÙWÛ�ÜWÝÞäã�á

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'�

7UXFN�RU�RWKHUYHKLFOH"

8VH�RI�:LGHQLQJ�&DVW

n The client, the car rental company in the above example, wants to execute a function for specificvehicles form the list (vehicle_list). For example, the client wants to ascertain the truck withthe largest cargo capacity.However, not all vehicles are in the trucks list, it also includes references to cars and busses.

Page 265: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-15

SAP AG 2002

Ñ � Óâ� �i� ���|�L�

� OFOBUHQWDO � � �J� �i� ��� � �|�i�

� � �3� �i� ��� ���

å�æ¦çiè5é/ê�ëè�ì�ê+í�å�îÎÅLÏ�ÂLÐ|É Å·Æ�ÉiÂiÀiÁ

� ï �3� � � ��� � ���i�ØÔÙ�Ú ¥3£ ÙWÛ�ÜgÝÞàß�á

ØâÙ�Ú ¥J£ ÙWÛ�ÜWÝÞäã�á

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'�

ð�ñ|òôó õÔó ö-÷�øJù�úiû3ó

:LGHQLQJ�&DVW��([DPSOH

n The type of case described above is known as a widening cast (or "down cast") because it changesfrom a less detailed view to one with more detail. The target reference (r_truck in the aboveexample) must correspond to the object reference (r_vehicle in the above example), that is theinstance must have the details implied by the reference.

n The widening cast logically represents the opposite of the narrowing cast. The widening cast cannotbe checked statically, only at runtime. The &DVW�2SHUDWRU ?=�(or the equivalent MOVE ... ?TO… ) must be used to make this visible.

n With this kind of cast, a check is carried out at runtime to ensure that the current content of thesource variable corresponds to the type requirements of the target variables. In this example, itchecks that the dynamic type of the source reference r_vehicle is compatible with the static typeof the target reference r_truck. If it is, the assignment is carried out. Otherwise, an exception thatcan be handled is raised, and the original value of the target variable remains the same. Thisexception of the error class CX_SY_MOVE_CAST_ERROR can be caught using TRY-ENDTRY andthe CATCH statement. (This will be discussed in more detail later.)

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

Page 266: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-16

SAP AG 2002

1DUURZLQJ�FDVW1DUURZLQJ�FDVW

:LGHQLQJ�FDVW�:LGHQLQJ�FDVW�

,QKHULWDQFH�DQG�SRO\PRUSKLVP�,QKHULWDQFH�DQG�SRO\PRUSKLVP�

&DVWLQJ����

Page 267: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-17

SAP AG 2002

6HPDQWLFV�DQG�8VH�ZLWK�,QKHULWDQFH

l ,QKHULWHG�FRPSRQHQWV�PXVW�EHKDYH�LQ�VXEFODVVHVH[DFWO\�DV�WKH\�GR�LQ�VXSHUFODVVHV�IRU�DOO�XVHUV

l :KHQ�\RX�UHGHILQH�D�PHWKRG�LWV�VHPDQWLFV�PXVW�UHPDLQWKH�VDPH��\RX�FDQQRW�FKDQJH�WKH�VLJQDWXUH

l 8VLQJ�LQKHULWDQFH�l $OORZV�\RX�WR�HQKDQFH�FODVVHV�XVLQJ

JHQHUDOL]DWLRQ�VSHFLDOL]DWLRQ�DQG�KHQFH�DFKLHYHEHWWHU�VRIWZDUH�VWUXFWXUH

l 3URYLGHV�SRVVLELOLW\�RI�SRO\PRUSKLF�EHKDYLRU��JHQHULF�SURJUDPPLQJ���&$6(�FRQVWUXFWLRQV�QRORQJHU�QHHGHG

ü 0XVW�QRW�EH�XVHG�VROHO\�IRU��FRGH�LQKHULWDQFH�

5XOHV�

n 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 ora superclass instance. The user works only with references to the superclass and must rely on theinherited components behaving in the subclass instances exactly as they do in the superclassinstances, otherwise the program will not work.

n On the other hand, this ensures useful restrictions on the implementation of the subclasses: Inheritedcomponents must keep their inherited semantics. You cannot use inherited attributes or events in anyway other than intended in the superclass, and you cannot change method semantics by redefinition.

n You must avoid "code inheritance": It is not correct for one class to inherit from another simplybecause SDUW of the functionality required is already implemented there.

Page 268: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-18

SAP AG 2002

,QFRUUHFW�8VH�RI�,QKHULWDQFH

l ,QKHULWDQFH�LV�RIWHQ�XVHG�LQFRUUHFWO\�ü 7R�VLPSO\�UHXVH�FRGHü ,QVWHDG�RI�DGGLWLRQDO�DWWULEXWHV�DJJUHJDWLRQ�UROH

FRQFHSWV

ü 7KH�XVH�RI�LQKHULWDQFH�GRHV�QRW�DOZD\V�FRUUHVSRQGWR�H[SHFWDWLRQV�LQ�WKH�UHDO�ZRUOG

5XOHV�

FDU

FDUBUHG FDUBEOXH

6XSHUFODVVýÿþ����������������

FKDQJHBZLGWK

FKDQJHBKHLJKW6XEFODVV �� ����+þ

GHVLJQ�"

GHVLJQ�"

n Using inheritance instead of attributes, or a misunderstanding of inheritance as an is-one relationshipoften leads to the following kind of design: the superclass FDU has the subclasses UHG�FDU, JUHHQ�FDU,and so on. These subclasses all have an identical structure and identical behavior.

n Because an instance cannot change its class, in circumstances like the following, you should not useinheritance directly, but use a so-called role design pattern instead:The class HPSOR\HH has the subclasses IXOO�WLPH�HPSOR\HH and SDUW�WLPH�HPSOR\HH. What happenswhen a part-time employee becomes a full-time employee? A new full-time employee object wouldhave 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 apart-time employee object that logically does not exist anymore.

n The use of inheritance does not always correspond to expectations in the real world: For example, ifthe class VTXDUH inherits from the class UHFWDQJOH, the latter will have two separate methods forchanging length or width, although the sides of the square actually need to be changed by the samemeasurement.

Page 269: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-19

SAP AG 2002

l 8VH�FDVWVl 'HYHORS�JHQHULF�SURJUDPV�XVLQJ�SRO\PRUSKLVP

ZLWK�LQKHULWDQFH

<RX�DUH�QRZ�DEOH�WR�

&DVWLQJ��8QLW�6XPPDU\

Page 270: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-20

�&DVWLQJ�([HUFLVHV8QLW��&DVWLQJ7RSLF��3RO\PRUSKLVP

At the conclusion of these exercises, you will be able to:

• Describe polymorphism and inheritance

• Use generic programming for inheritance relationships and implementpolymorphic method calls

0RGHO�VROXWLRQ� 6$3%&���B&$66B0$,1B$6$3%&���B,1+6B$�include program

1-1 Extend your existing program =%&���B��B0$,1B$,53/$1( or start with themodel solution from the last chapter’s exercises.

1-2 In your main program, define an internal table for buffering airplane objects. Thetype 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 theGLVSOD\BDWWULEXWHV method for every plane in a LOOP.Read the internal table in the LOOP using the auxiliary reference variableUBDLUSODQH (type 5()�72�OFOBDLUSODQH).

1-3-1 Was this successful?

1-3-2 Check the internal table and the execution of the GLVSOD\BDWWULEXWHV methodin the Debugger

1-3-3 Which source code is executed when the GLVSOD\BDWWULEXWHV�method iscalled, the original method from the superclass or the relevant redefinedmethods from the subclasses? What would happen if one of these methodshad not been redefined in the subclass?

0RGHO�VROXWLRQ� 6$3%&���B&$66B0$,1B%6$3%&���B&$66B%�include program

7HPSODWH� 6$3%&���B&$67B%

Page 271: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-21

2-1 Copy the definition of the class OFOBFDUULHU from the template6$3%&���B&$67B% to your own include program =%&���B��B$,53/$1((hence adding this class to your include program).

2-2 Add the following public instance methods to the class OFOBFDUULHU: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 areference 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 thelist DLUSODQHBOLVW. The GLVSOD\BDWWULEXWHV method from class OFOBDLUSODQHshould 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 LFRQBIOLJKWas an icon before the name).

2-3 Go back to your main program.

2-3-1 At the DATA statement, create a reference to the class OFOBFDUULHU.

2-3-2 Comment out all statements affecting the internal table (or delete them).

2-3-3 Create an airline and fill the transfer parameters with data of your ownchoice.

2-3-4 Add the planes already created in the last exercise (passenger and cargoplanes) to the list of planes DLUSODQHBOLVW. To do this, call the methodDGGBDLUSODQH of the class OFOBFDUULHU.Create more planes and add them to the airplane list.

2-3-5 Display the airline attributes by calling the GLVSOD\BDWWULEXWHV method of theclass OFOBFDUULHU.

2-3-6 Question: Which code is executed when you execute GLVSOD\BDWWULEXWHVwithin GLVSOD\BDLUSODQHV.

Page 272: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-22

�&DVWLQJ�6ROXWLRQV8QLW� &DVWLQJ7RSLF� 3RO\PRUSKLVP

*&---------------------------------------------------------------------*

*& Report SAPBC401_CASS_MAIN_a *

*& *

*&---------------------------------------------------------------------*

*& show casting operations ... *

*&---------------------------------------------------------------------*

REPORT sapbc401_cass_main_a.

INCLUDE <icon>.

INCLUDE sapbc401_inhs_a.

DATA: r_plane TYPE REF TO lcl_airplane,

r_cargo TYPE REF TO lcl_cargo_plane,

r_passenger TYPE REF TO lcl_passenger_plane,

������SODQHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBDLUSODQH�

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’

Page 273: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-23

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 *

*& *

*&---------------------------------------------------------------------*

*& carrier adds airplanes to its airplane_list --> polymorphism *

*&---------------------------------------------------------------------*

REPORT sapbc401_cass_main_b.

INCLUDE <icon>.

INCLUDE sapbc401_cass_b.

DATA: r_plane TYPE REF TO lcl_airplane,

r_cargo TYPE REF TO lcl_cargo_plane,

r_passenger TYPE REF TO lcl_passenger_plane,

������UBFDUULHU�W\SH�UHI�WR�OFOBFDUULHU�

START-OF-SELECTION.

*##############################

***** Create Carrier ********************************************

��FUHDWH�REMHFW�UBFDUULHU�H[SRUWLQJ�LPBQDPH� �6PLOH)O\�7UDYHO�

***** Passenger Plane ********************************************

CREATE OBJECT r_passenger EXPORTING

im_name = ’LH BERLIN’

im_planetype = ’747-400’

im_seats = 345.

***** cargo Plane ************************************************

Page 274: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-24

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���***** show all airplanes inside carrier ***************************

��UBFDUULHU�!GLVSOD\BDWWULEXWHV����

*---------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION

*---------------------------------------------------------------------*

* Class definitions of ... lcl_airplane, cargo- and passenger_plane

* ...

* ...

*---------------------------------------------------------------------*

* CLASS lcl_carrier DEFINITION

*---------------------------------------------------------------------*

&/$66�OFOBFDUULHU�'(),1,7,21�

��38%/,&�6(&7,21��������������������������������������������������0(7+2'6��FRQVWUXFWRU�,03257,1*�LPBQDPH�7<3(�VWULQJ��������������JHWBQDPH�5(7851,1*�YDOXH�H[BQDPH��7<3(�VWULQJ��������������DGGBDLUSODQH�,03257,1*���������������������LPBSODQH�7<3(�5()�72�OFOBDLUSODQH��������������GLVSOD\BDLUSODQHV��������������GLVSOD\BDWWULEXWHV���35,9$7(�6(&7,21���������������������������������������������'$7$��QDPH��������������7<3(�VWULQJ�����������DLUSODQHBOLVW�W\SH�7$%/(�2)�5()�72�OFOBDLUSODQH�(1'&/$66�

Page 275: 003 - Bc401 - Abap Objects

© SAP AG BC401 9-25

*---------------------------------------------------------------------*

* CLASS lcl_carrier IMPLEMENTATION

*---------------------------------------------------------------------*

&/$66�OFOBFDUULHU�,03/(0(17$7,21�

��0(7+2'�DGGBDLUSODQH�����$33(1'�LPBSODQH�72�DLUSODQHBOLVW���(1'0(7+2'�

��0(7+2'�GLVSOD\BDWWULEXWHV�����:5,7(��LFRQBIOLJKW�$6�,&21��QDPH���XOLQH��XOLQH�����GLVSOD\BDLUSODQHV������(1'0(7+2'�

��0(7+2'�GLVSOD\BDLUSODQHV�����GDWD��UBSODQH�W\SH�UHI�WR�OFOBDLUSODQH�������ORRS�DW�DLUSODQHBOLVW�LQWR�UBSODQH����������UBSODQH�!GLVSOD\BDWWULEXWHV����������HQGORRS���(1'0(7+2'�

��0(7+2'�FRQVWUXFWRU�����QDPH� �LPBQDPH���(1'0(7+2'�

��0(7+2'�JHWBQDPH�����H[BQDPH� �QDPH���(1'0(7+2'�

(1'&/$66�

Page 276: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-1

SAP AG 2002

l ,QWHUIDFHVl &RPSRXQG�LQWHUIDFHVl 3RO\PRUSKLVP

&RQWHQWV�

,QWHUIDFHV

Page 277: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-2

SAP AG 2002

l 'HILQH�DQG�LPSOHPHQW�LQWHUIDFHVl 'HYHORS�JHQHULF�SURJUDPV�XVLQJ�SRO\PRUSKLVP

ZLWK�LQWHUIDFHV

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

,QWHUIDFHV��8QLW�2EMHFWLYHV

Page 278: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-3

SAP AG 2002

,QWHUIDFHV��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �

� ��� ��� ����� � � � � ����� � � � � ! � � � � � � ��" � � � � ��# # � � $ %�& & ' (�)�*,+�- ) + %�.0/% (21 * % / / ' ( )�3 %�4 %5�6�7 8�9;: 8�< =?> 8�@�AB @�C A = 6D @ < @ B�E?F = 6 @ 8�G

D @ < @IH CKJ =�L < 6 7 8D = < @ 7 A MON�P�Q R�SUTWV S Q V?X S?Y

Z,[�\ ]�^ \ _�` ab0c2d2e�f g a ^?h i e [�\ a ] h a?j Z [ c�k [ l�m m \ ] kn ]2o a [p\ h l ] ^ aq l b h \ ] k

rtsIuv�w�x?yUz vU{rtsIuv w�x yUz v {

Page 279: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-4

SAP AG 2002

,QWHUIDFHV��3ULQFLSOHV�,QWHUIDFHV��3ULQFLSOHV�

:RUNLQJ�ZLWK�LQWHUIDFHV:RUNLQJ�ZLWK�LQWHUIDFHV

3UHYLHZ��&RPSRXQG�LQWHUIDFHV3UHYLHZ��&RPSRXQG�LQWHUIDFHV

,QWHUIDFHV����

Page 280: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-5

SAP AG 2002

OFOBDLUSODQH OFOBYHKLFOHV

OFOBUHQWDO

|~}������������������������

��� ��� ��� ���

,QWHUIDFHV,QWHUIDFHV

OFOBFDUULHU OFOBKRWHO

6HUYLFH��6HUYLFH��6HUYLFH��

���"

,QWHUIDFHV��8VH

n In ABAP Objects, interfaces are implemented in addition to and independently of classes. Interfacesonly describe the external point of contact of a class (protocols), they do not contain anyimplementation.

n Interfaces are usually defined by a user. The user describes in the interface which services (technicaland semantic) it needs in order to carry out a task. The user never actually knows the providers ofthese services, but communicates with them through the interface. In this way the user is protectedfrom actual implementations and can work in the same way with different classes/objects, as long asthey provide the services required. This is known as SRO\PRUSKLVP�with interfaces.

Page 281: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-6

SAP AG 2002

OFOBDLUSODQH OFOBYHKLFOHV

OFOBUHQWDO

���K�K�����I�����U�

|~}������������������������

GLVSOD\BSDUWQHUFKHFNBDYDLODELOLW\

���

��� ��� ��� ���

,QWHUIDFH,QWHUIDFH

OFOBFDUULHU OFOBKRWHO

,QWHUIDFHV��6HUYLFHV

n 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 differentimplementations of the services in the server classes (polymorphism).

n The following are examples of shared services of the server classes in the above case:display_partner, check_availability, booking, or cancel_booking.

Page 282: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-7

SAP AG 2002

,PSOHPHQWDWLRQPHKUHUHU�,QWHUIDFHVP|JOLFK��

,W�LV�SRVVLEOH�WRLPSOHPHQW�VHYHUDOLQWHUIDFHV

OFOBDLUSODQH OFOBYHKLFOHV

OFOBUHQWDOOFOBFDUULHU

��� ��� ��� ���

OFOBKRWHO

OFOBWUDYHOBDJHQF\

OLIBSDUWQHUVLPSOHPHQWV

XVHV&OLHQW&OLHQW

OLIBOLFHQVH���

��,QWHUIDFH!!��,QWHUIDFH!! ��,QWHUIDFH!!��,QWHUIDFH!!

,QWHUIDFH��80/�([DPSOH

n In UML, interfaces can represented in the same way as classes. However, they always have thestereotype «interface» above their name and can therefore be distinguished from classes.

n The use of an interface is represented by a dotted line with a two-sided arrow from the user to theinterface, the stereotype «uses» is optional. The fact that a class implements an interface isrepresented by a dotted line with a three-sided arrow from the class to the interface. The similarity tothe representation of inheritance is intentional, because the interface can be seen as a generalizationof the class implemented or the class can be seen as a specialization of the interface.

n In ABAP Objects, the same components can be defined in interfaces and in classes. This allows youto shift part of the public point of contact of a class into an interface, even though the class is alreadyin use; users will not notice the difference as long as you use alias names (see appendix) for thecomponents that are now in the interface.

n A class can implement any number of interfaces and an interface can be implemented by any numberof classes. By implementing several interfaces, a class can simulate multiple inheritance. In theexample, lcl_rental implements the interfaces lif_partners and lif_license.

Page 283: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-8

SAP AG 2002

'HILQLQJ�DQG�,PSOHPHQWLQJ�DQ�,QWHUIDFH

,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�

l 'HFODULQJ�WKHLQWHUIDFH

l 7KH�VHUYHU�FODVVHV�PDNH�WKH�LQWHUIDFHSXEOLF�

l ,QWHUIDFHV�DUHLPSOHPHQWHG�LQ�FODVVHV

l ,QWHUIDFHV�GR�QRW�KDYHYLVLELOLW\�VHFWLRQV

&/$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�

,QWHUIDFHUHVROXWLRQRSHUDWRU

n In ABAP Objects, the same components (attributes, methods, constants, types, alias names) can bedefined in an interface in largely the same way as in classes. However, interfaces do not havecomponent visibility levels.

n Interfaces are implemented in classes.

• The interface name is listed in the definition part of the class. Interfaces can only beimplemented 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 sameinterface privately.

• The operations defined in the interface are implemented as methods of a class. A check is carriedout to ensure that all the methods defined in the interfaces are actually present in theimplementation part of the class (for global interfaces, a missing or superfluous implementationof an interface method results in a warning).

• The attributes, events, constants, and types defined in the interface are automatically available tothe class carrying out the implementation.

n Interface components are addressed in the class carrying out the implementation byprefixing the interface name, followed by a tilde (the ,QWHUIDFH�5HVROXWLRQ�2SHUDWRU):<interfacename>~<componentname>.

n Changes to an interface usually invalidate all the classes implementing it.

Page 284: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-9

SAP AG 2002

�W�����0��O�����U 

l 6HSDUDWLRQ�RI�H[WHUQDO�SRLQW�RI�FRQWDFW�LQWHUIDFH��DQG�LPSOHPHQWDWLRQ��FODVV�

¡ 7KH�FOLHQW�GHILQHV�WKH�SURWRFRO��WKH�VHUYHU�LPSOHPHQWV�LW¡ �%ODFN�%R[�SULQFLSOH���&OLHQW�RQO\�NQRZV�WKH�LQWHUIDFH��QRW

WKH�LPSOHPHQWDWLRQ¡ /RRVHU�OLQNDJH�EHWZHHQ�FOLHQW�DQG�VHUYHU

l 3RO\PRUSKLVP¡ *HQHULF�KDQGOLQJ�RI�REMHFWV�RI�GLIIHUHQW�FODVVHV

l $EVWUDFWLRQ¡ ,QWHUIDFH�DV�D�JHQHUDOL]DWLRQ�RI

WKH�LPSOHPHQWLQJ�FODVV

l 6LPXODWLRQ�RI�PXOWLSOH�LQKHULWDQFH¢¤£�¥§¦�¨ ©

¢W£Wª¤© ¢�£OªO«

¬p­�® ¦W¨ ¯K°

,QWHUIDFHV��)HDWXUHV

�W�2���²±�W�����0³�,´ µ�¶�· ¸¹»º�¶�¸�¼ ½p¾���¼

n Interfaces are the means of choice for describing external points of contact or protocols, withoutlinking them to a type of implementation. An extra layer is introduced between the client and theserver to protect the client explicitly from the server, thereby making the client independent.

n Interfaces enable you to work uniformly with different classes (providers/servers). In particular, theyalways ensure polymorphic behavior as they do not have their own implementation, but insteadallow the providers to carry it out.

n The definition of an interface is always an abstraction: The user wants to handle various providers inthe same way and must therefore abstract concrete implementations to a description of the servicesrequired to fulfill the task.

n You can also use interfaces to achieve multiple inheritance by defining the functionality to beinherited by a second class as an interface that the inheriting class then has to implement.

Page 285: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-10

SAP AG 2002

,QWHUIDFHV��3ULQFLSOHV�,QWHUIDFHV��3ULQFLSOHV�

:RUNLQJ�ZLWK�LQWHUIDFHV:RUNLQJ�ZLWK�LQWHUIDFHV

3UHYLHZ��&RPSRXQG�LQWHUIDFHV3UHYLHZ��&RPSRXQG�LQWHUIDFHV

,QWHUIDFHV����

Page 286: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-11

SAP AG 2002

:RUNLQJ�ZLWK�,QWHUIDFH�&RPSRQHQWV

&/$66�OFOBUHQWDO�,03/(0(17$7,21��������0(7+2'�OLIBSDUWQHUVaGLVSOD\BSDUWQHU� �VSHFLDO�FRGLQJ�WR�GLVSOD\�SDUWQHUV�RU �MXVW�FDOOLQJ�H[LVWLQJ�PHWKRG�WKDW�ILWV�����GLVSOD\BDWWULEXWHV������(1'0(7+2'�(1'&/$66�

'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�...

&5($7(�2%-(&7�UBYHKLFOH� �V\QWDFWLFDOO\�SRVVLEOH�EXW�QRW�XVHIXO�UBYHKLFOH�!OLIBSDUWQHUVaGLVSOD\BSDUWQHU����

OFOBUHQWDO��QDPH��YHKLFOHBOLVW��DGGBYHKLFOH��GLVSOD\BDWWULEXWHV����

OLIBSDUWQHUVGLVSOD\BSDUWQHUFKHFNBDYDLODELOLW\���

LPSOHPHQWV

,QWHUIDFH�UHVROXWLRQ�RSHUDWRU

n You can access interface components using an object reference, whose class implements theinterface. Syntactically this is done with the interface resolution operator, just as with the methoddefinitions in the implementation part of the class.

n This allows you to differentiate between components defined in the interface and components of thesame name that are defined in the class itself. The reason for this is the shared namespace.

n 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~method1An alias defined in this way can be directly addressed using r_ref->a1.

Page 287: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-12

SAP AG 2002

,QWHUIDFH�5HIHUHQFHV��1DUURZLQJ�&DVW

'$7$��UBUHQWDO�7<3(�5()�72�OFOBUHQWDO�������UBOLI����7<3(�5()�72�OLIBSDUWQHUV���

&5($7(�2%-(&7�UBUHQWDO�

�1DUURZLQJ�&DVWUBOLI� �UBUHQWDO

¿ÁÀ�¿ �;���ÃÂ;�

¿ÁÀ �t�KÄ

¿ÁÀ �K�KÄ

����OFOBUHQWDOPublic

GLVSOD\BSDUWQHUFKHFNBDYDLODELOLW\���

,QWHUIDFHFRPSRQHQWV

Private

n Interfaces are addressed using interface references. Interface references always refer to instances inthe classes carrying out the implementation. Interface references always have both static anddynamic types.

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

n With an interface reference, you can no longer address all components in the class carrying out theimplementation, but only the components defined in the interface. These components are nowaddressed using the interface reference exclusively with their own short name.

n When an object reference is assigned to an interface reference, you must be able to convert thedynamic type of the interface reference to the static type of the object reference - that is, the classthat was used to define the object reference must have implemented the interface of the interface-reference.

Page 288: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-13

SAP AG 2002

Å§Æ Ç Æ ÈÉ Ê Ë ÌÍÆ��� OFOBWUDYHOBDJHQF\PrivatePublic

DGGBSDUWQHUÎ�Ï�Ð�Ñ�ÒWÓ¤Ð�ÔIÕ�Ö�×WÑ

...

0(7+2'6��DGGBSDUWQHU�,03257,1*�LPBSDUWQHU�7<3(�5()�72�OLIBSDUWQHUV�...

ÅOÆ Ç Æ ÈØ ÌÍÙtË ÚÍÆ

Å§Æ Ç Æ È»ÇtÚ Ø Ø Û ÌÍØ

8VLQJ�WKH�,QWHUIDFH

n The travel agent is a user (client) of the created interface. The travel agency keeps references to itsbusiness 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.

n When it is called, the client’s public method add_partner is transferred the business partnerreferences. In the method, the references are inserted into the table partner_list. Therefore, theinterface parameter of the add_partner method has the type REF TO lif_partners.

n The aim here is polymorphism: It needs to be possible to generically access the services of theinterface later.

Page 289: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-14

SAP AG 2002

0(7+2'�GLVSOD\BDJHQF\BSDUWQHUV��'$7$��UBSDUWQHU�7<3(�5()�72�OLIBSDUWQHUV����/223�$7�SDUWQHUBOLVW�,172�UBSDUWQHU�����UBSDUWQHU�!GLVSOD\BSDUWQHU������(1'/223�(1'0(7+2'�

0(7+2'����aGLVSOD\BSDUWQHU���� GLVSOD\BKRWHOBDWWULEXWHV�����...� (1'0(7+2'�

0(7+2'����aGLVSOD\BSDUWQHU�����GLVSOD\BUHQWDOBGDWD�����...(1'0(7+2'�

Î�ÏWÐ�Ñ�ÒWÓWÐ2ÔIÕ�Ö�×OÑÜ Ý Þ�Æ ÇKÆ ÈØ ÌÍÙßË ÚÍÆ

Ü à Þ²Æ Ç Æ ÈÉÁÊtË Ì Æ

0(7+2'����aGLVSOD\BSDUWQHU���GLVSOD\BDWWULEXWHV�����...(1'0(7+2'�

Ü á Þ�Æ Ç Æ È»ÇtÚ Ø Ø Û ÌÍØ

3RO\PRUSKLVP�DQG�,QWHUIDFHV

n Polymorphism can also be used for interfaces: you can use interface references to call methods thatcan have a GLIIHUHQW�LPSOHPHQWDWLRQ depending on the object behind the reference.

n The dynamic type, not the static type of the reference variable is used to search for theimplementation 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 theimplementation of display. The static type for r_partner, which is always REF TOlif_partners, is not used.

Page 290: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-15

SAP AG 2002

� OFOBUHQWDOâ�ã�ã�ä»å�æ�ç�è�é�ê�ë

ì?í îÍïtðÍñÍîpò�ó ô õ ï

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���������������&$7&+�F[BV\BPRYHBFDVWBHUURU� ����UHDFW�RQ�WKDW�FDVW�HUURU�����(1'75<���(1'/223�(1'0(7+2'�

öø÷ ù²÷ ú²ûüÍýWþ²ÿ0÷ úIý��0ÿ��0ù��Å§Æ Ç Æ È»ÉÍÊtË ÌÍÆ

Å§Æ Ç Æ ÈØ Ì ÙßË ÚÍÆ

Å§Æ Ç Æ È»ÇtÚ Ø Ø Û ÌÍØ

,QWHUIDFH�5HIHUHQFHV��:LGHQLQJ�&DVW

n The widening cast is, as with inheritance, the opposite of the narrowing cast: Here it is used toretrieve a class reference from an interface reference. Obviously it cannot be statically checked, sincean interface can be implemented by more than one class.

n An object reference cannot be assigned to an interface reference if it has itself not implemented thecorresponding interface. It cannot be assigned even if a subclass has implemented the interface andthe interface reference points to an object in this class.

n Assignments between interface references whose interfaces are not related to each other cannot bechecked statically and must therefore be formulated using the cast operator ?=.

n For this type of assignment, a check must be carried out at runtime to see whether the class of theinstance that the source reference points to also supports the interface that the target reference refersto. If this is the case, the cast is carried out, otherwise the exception that can be handled of the classCX_SY_MOVE_CAST_ERROR is raised.

Page 291: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-16

SAP AG 2002

,QWHUIDFHV��3ULQFLSOHV�,QWHUIDFHV��3ULQFLSOHV�

:RUNLQJ�ZLWK�LQWHUIDFHV:RUNLQJ�ZLWK�LQWHUIDFHV

3UHYLHZ��&RPSRXQG�LQWHUIDFHV3UHYLHZ��&RPSRXQG�LQWHUIDFHV

,QWHUIDFHV����

Page 292: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-17

SAP AG 2002

&RPSRXQG�,QWHUIDFHV

OFOBUHQWDOOFOBFDUULHU

,QWHUIDFH,QWHUIDFH

OFOBKRWHO

OLIBSDUWQHUV �������� ������

3UREOHP�([WHQGLQJLQWHUIDFHV

OFOBKRWHO

,QWHUIDFH,QWHUIDFHOLIBURRPBERRNLQJ

,QWHUIDFH,QWHUIDFH OLIBSDUWQHUV

OFOBFDUULHU OFOBUHQWDO

6ROXWLRQ�&RPSRXQGLQWHUIDFHV

"

n ABAP Objects contains a composition model for interfaces. A compound interface contains otherinterfaces as components (component interfaces) and summarizes the extension of these componentinterfaces. An elementary interface does not itself contain other interfaces.

n One interface can be used as a component interface in several compound interfaces.

n UML only deals with the specialization and generalization of interfaces. This relationship isrepresented by a dotted line with a three-sided arrow from the specialized to the generalizedinterface.

n Compound interfaces in ABAP Objects can always be seen as specializations of their componentinterfaces and represented as such in UML.

Page 293: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-18

SAP AG 2002

&RPSRXQG�,QWHUIDFHV��([DPSOH

,17(5)$&(�OLIBSDUWQHUV���0(7+2'6�GLVSOD\BSDUWQHU�(1',17(5)$&(�,17(5)$&(�OLIBURRPBERRNLQJ���,17(5)$&(6�OLIBSDUWQHUV���0(7+2'6�ERRNBURRP�(1',17(5)$&(�

&/$66�OFOBKRWHO�'(),1,7,21���38%/,&�6(&7,21�����,17(5)$&(6�OLIBURRPBERRNLQJ�(1'&/$66�&/$66�OFOBKRWHO�,03/(0(17$7,21���0(7+2'�OLIBSDUWQHUVaGLVSOD\BSDUWQHU���(1'0(7+2'���0(7+2'�OLIBURRPBERRNLQJaERRNBURRP���(1'0(7+2'�(1'&/$66�

'$7$��LBSDUWQHU����7<3(�5()�72�OLIBSDUWQHUV�������LBURRPBERRN��7<3(�5()�72�OLIBURRPBERRNLQJ�...

LBSDUWQHU� �LBURRPBERRN��������³1DUURZLQJ�&DVWLBURRPBERRN�!OLIBSDUWQHUVaGLVSOD\BSDUWQHU���� �LBSDUWQHU�!GLVSOD\BSDUWQHU����DOVR�SRVVLEOH�

LBURRPBERRN�" �LBSDUWQHU�������³:LGHQLQJ�&DVW

n In a compound interface, the components of the component interface keep their original names, thatis <component-interfacename>~<componentname>; no more prefixes are added. Inother words, all components in a compound interface are on the same level, and componentsinherited from component interfaces are marked with the usual interface prefix.

n This equality principle for compound interfaces also affects how they are implemented. Theprocedure is as follows: First, implement the elementary interfaces, then the additional methods fromthe compound interfaces. For multiple compound interfaces, the process is simply repeated. In theclass carrying out the implementation, all components of all interfaces implemented are again on thesame level.

n 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 theclasses that implement them.

Page 294: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-19

SAP AG 2002

l 'HILQH�DQG�LPSOHPHQW�LQWHUIDFHVl 'HYHORS�JHQHULF�SURJUDPV�XVLQJ�SRO\PRUSKLVP

ZLWK�LQWHUIDFHV

<RX�DUH�QRZ�DEOH�WR�

,QWHUIDFHV��8QLW�6XPPDU\

Page 295: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-20

�,QWHUIDFHV�([HUFLVHV8QLW��,QWHUIDFHV7RSLF��'HILQLQJ�,QWHUIDFHV

At the conclusion of these exercises, you will be able to:

• Define and implement interfaces

0RGHO�VROXWLRQ� 6$3%&���B,176B0$,1B$6$3%&���B9(+'B)�include program6$3%&���B,176B$�include program

1 The classes OFOBUHQWDO��OFOBYHKLFOH, and their subclasses are to be added to the existing UMLdiagram.The classes OFOBUHQWDO� OFOBFDUULHU, and later OFOBKRWHO will all store separate services andprovide a possible client (to be defined later) using an interface OLIBSDUWQHUV.

1-1 Add the interface OLIBSDUWQHUV to your UML diagram. GLVSOD\BSDUWQHU is to be theonly interface method. What are the roles of the classes OFOBUHQWDO� OFOBFDUULHU, andOFOBKRWHO?�Depict this in the UML diagram by adding text and arrows.Fill in the UML diagram on the next page.

1-2 Copy the include program 6$3%&���B9(+'B)�to your own include program=%&���B��B9(+,&/(. As well as the classes OFOBUHQWDO��OFOBYHKLFOH��OFOBWUXFN�OFOBEXV, the program also contains the interface OLIBSDUWQHUV.

1-3 Incorporate this include program into your main program.Add the types W\BIXHO and W\BFDUJR (both TYPE P DECIMALS 2) to the mainprogram.Pay attention to the position of the interface definition; you may have to change theorder of the include programs or move the interface into the other include program.Familiarize yourself with the interface definition.

Page 296: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-21

1-4 Now implement the�interface of your class OFOBFDUULHU.

1-5 In the main program, create some instances for OFOBUHQWDO, OFOBYHKLFOH, and so on bycopying the relevant code from the template 6$3%&���B9(+7B0$,1B$. Call themethod GLVSOD\BDWWULEXWHV of the class OFOBUHQWDO.Start your program. You should see an airline with your planes and a car rentalcompany with various vehicles.(You will use the interface in the next exercise.)

UML-Template for Exercise 1-1

Page 297: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-22

([HUFLVHV8QLW��,QWHUIDFHV7RSLF��8VLQJ�,QWHUIDFHV

At the conclusion of these exercises, you will be able to:

• Define and implement interfaces

• Use polymorphism with interfaces

0RGHO�VROXWLRQ� 6$3%&���B,176B0$,1B%6$3%&���B9(+'B*�include program6$3%&���B,176B$�include program

2 Now the class OFOBWUDYHOBDJHQF\ (travel agency) is to be added to the existing UMLdiagram. It will use the interface to access the classes OFOBUHQWDO� OFOBFDUULHU��OFOBKRWHO�(notyet implemented).

2-1 Add the class OFOBWUDYHOBDJHQF\ to your UML diagram. What is the role of thisclass?�Depict this in the UML diagram by adding text and arrows.Fill in the UML diagram on the next page.

2-2 Copy the definition of the class OFOBWUDYHOBDJHQF\�from the template6$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 businesspartners (that is car rental companies and airlines) to the internal tableSDUWQHUBOLVW.

2-2-2 What are the types of the internal table and the interface parameter ofDGGBSDUWQHU?

2-2-3 The travel agency will use the method GLVSOD\BDJHQF\BSDUWQHUV to displaythe data of its business partners.

Page 298: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-23

2-3 Go back to your main program.

2-4 Create an instance of the class OFOBWUDYHOBDJHQF\.

2-4-1 Use the DGGBSDUWQHU method to add the travel agency’s business partners tothe internal table.

2-4-2 Use the GLVSOD\BDJHQF\BSDUWQHUV method to display the travel agency’sbusiness partners.

2-4-3 At which point does polymorphism take place?Follow this in the Debugger.

UML-Template for Exercise 2-1

Page 299: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-24

�,QWHUIDFHV�6ROXWLRQV8QLW� ,QWHUIDFHV7RSLF� 'HILQLQJ�,QWHUIDFHV

*&---------------------------------------------------------------------*

*& Report SAPBC401_INTS_MAIN_A *

*& *

*&---------------------------------------------------------------------*

*& include interface lif_partners and include file with lcl_vehicle.. *

*&---------------------------------------------------------------------*

REPORT sapbc401_ints_main_a.

types ty_fuel type p decimals 2.

types ty_cargo type p decimals 2.

INCLUDE <icon>.

INCLUDE sapbc401_vehd_f.

include sapbc401_ints_a.

DATA: r_plane TYPE REF TO lcl_airplane,

r_cargo TYPE REF TO lcl_cargo_plane,

r_passenger TYPE REF TO lcl_passenger_plane,

������UBFDUULHU�W\SH�UHI�WR�OFOBFDUULHU�UBDJHQF\�W\SH�UHI�WR�OFOBWUDYHOBDJHQF\�

������UBUHQWDO�W\SH�UHI�WR�OFOBUHQWDO�������UBWUXFN�W\SH�UHI�WR�OFOBWUXFN�������UBEXV�W\SH�UHI�WR�OFOBEXV�

START-OF-SELECTION.

*##############################

***** Create Carrier ********************************************

��FUHDWH�REMHFW�UBFDUULHU�H[SRUWLQJ�LPBQDPH� �6PLOH)O\�7UDYHO�

***** Passenger Plane ********************************************

CREATE OBJECT r_passenger EXPORTING

Page 300: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-25

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

***** show all airplanes inside carrier ***************************

��UBFDUULHU�!GLVSOD\BDWWULEXWHV����

******* create RENTAL *****************************************

��&5($7(�2%-(&7�UBUHQWDO�(;3257,1*�LPBQDPH� �+$33<�&$5�5(17$/�

******* create truck *****************************************

��&5($7(�2%-(&7�UBWUXFN�(;3257,1*�LPBPDNH� �0$1����������������������������������LPBFDUJR� ������UBUHQWDO�!DGGBYHKLFOH��UBWUXFN���******* create truck *****************************************

��&5($7(�2%-(&7�UBEXV�(;3257,1*�LPBPDNH� �0HUFHGHV��������������������������������LPBSDVVHQJHUV� ������UBUHQWDO�!DGGBYHKLFOH��UBEXV���******* create truck *****************************************

��&5($7(�2%-(&7�UBWUXFN�(;3257,1*�LPBPDNH� �92/92����������������������������������LPBFDUJR� ������UBUHQWDO�!DGGBYHKLFOH��UBWUXFN���

Page 301: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-26

*&---------------------------------------------------------------------*

*& Include SAPBC401_INTS_a *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

...

*---------------------------------------------------------------------*

* CLASS lcl_carrier DEFINITION

*---------------------------------------------------------------------*

*---------------------------------------------------------------------*

&/$66�OFOBFDUULHU�'(),1,7,21�

��38%/,&�6(&7,21��������������������������������������������������,17(5)$&(6�OLIBSDUWQHUV�����0(7+2'6��FRQVWUXFWRU�,03257,1*�LPBQDPH�7<3(�VWULQJ��������������JHWBQDPH�5(7851,1*�YDOXH�H[BQDPH��7<3(�VWULQJ��������������DGGBDLUSODQH�,03257,1*���������������������LPBSODQH�7<3(�5()�72�OFOBDLUSODQH��������������GLVSOD\BDLUSODQHV��������������GLVSOD\BDWWULEXWHV���35,9$7(�6(&7,21���������������������������������������������'$7$��QDPH��������������7<3(�VWULQJ�����������DLUSODQHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBDLUSODQH�(1'&/$66����������������������OFOBFDUULHU�'(),1,7,21

*---------------------------------------------------------------------*

* CLASS lcl_carrier IMPLEMENTATION

*---------------------------------------------------------------------*

&/$66�OFOBFDUULHU�,03/(0(17$7,21���0(7+2'�OLIBSDUWQHUVaGLVSOD\BSDUWQHU�����GLVSOD\BDLUSODQHV������(1'0(7+2'����������������������OLIBSDUWQHUVaGLVSOD\BSDUWQHU

��0(7+2'�DGGBDLUSODQH�����$33(1'�LPBSODQH�72�DLUSODQHBOLVW���(1'0(7+2'����������������������DGGBDLUSODQH

��0(7+2'�GLVSOD\BDWWULEXWHV�����:5,7(��LFRQBIOLJKW�$6�,&21��QDPH���8/,1(��8/,1(�

Page 302: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-27

����GLVSOD\BDLUSODQHV������(1'0(7+2'����������������������GLVSOD\BDWWULEXWHV

��0(7+2'�GLVSOD\BDLUSODQHV�����'$7$��UBSODQH�7<3(�5()�72�OFOBDLUSODQH�����/223�$7�DLUSODQHBOLVW�,172�UBSODQH�������UBSODQH�!GLVSOD\BDWWULEXWHV��������(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� �QDPH���(1'0(7+2'����������������������JHWBQDPH

(1'&/$66����������������������OFOBFDUULHU�,03/(0(17$7,21

Page 303: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-28

6ROXWLRQV8QLW� ,QWHUIDFHV7RSLF� 8VLQJ�,QWHUIDFHV

*&---------------------------------------------------------------------*

*& Report SAPBC401_INTS_MAIN_B *

*& *

*&---------------------------------------------------------------------*

*& include interface lif_partners and include file with lcl_vehicle.. *

*& the client (travel_agency) uses the interface... *

*&---------------------------------------------------------------------*

REPORT sapbc401_ints_main_b.

types ty_fuel type p decimals 2.

types ty_cargo type p decimals 2.

INCLUDE <icon>.

INCLUDE sapbc401_vehd_g.

include sapbc401_ints_a.

DATA: r_plane TYPE REF TO lcl_airplane,

r_cargo TYPE REF TO lcl_cargo_plane,

r_passenger TYPE REF TO lcl_passenger_plane,

r_carrier type ref to lcl_carrier,

������UBDJHQF\�W\SH�UHI�WR�OFOBWUDYHOBDJHQF\�������UBUHQWDO�W\SH�UHI�WR�OFOBUHQWDO�������UBWUXFN�W\SH�UHI�WR�OFOBWUXFN�������UBEXV�W\SH�UHI�WR�OFOBEXV�

START-OF-SELECTION.

*##############################

***** Create TRAVEL_AGENCY **************************************

����&5($7(�2%-(&7�UBDJHQF\�(;3257,1*�LPBQDPH� �)O\6PLOH�7UDYHO�

Page 304: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-29

***** Create CARRIER ********************************************

create object r_carrier exporting im_name = ’Smile&Fly-Travel’.

***** Passenger Plane ********************************************

CREATE OBJECT r_passenger EXPORTING

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

r_carrier->add_airplane( r_passenger ).

r_carrier->add_airplane( r_cargo ).

***** insert business-parnter of agency into partner_list***********

��UBDJHQF\�!DGGBSDUWQHU��UBFDUULHU���

******* create RENTAL *****************************************

��&5($7(�2%-(&7�UBUHQWDO�(;3257,1*�LPBQDPH� �+$33<�&$5�5(17$/�

******* create truck *****************************************

��&5($7(�2%-(&7�UBWUXFN�(;3257,1*�LPBPDNH� �0$1����������������������������������LPBFDUJR� ������UBUHQWDO�!DGGBYHKLFOH��UBWUXFN���******* create truck *****************************************

��&5($7(�2%-(&7�UBEXV�(;3257,1*�LPBPDNH� �0HUFHGHV��������������������������������LPBSDVVHQJHUV� ������UBUHQWDO�!DGGBYHKLFOH��UBEXV���******* create truck *****************************************

��&5($7(�2%-(&7�UBWUXFN�(;3257,1*�LPBPDNH� �92/92����������������������������������LPBFDUJR� ������UBUHQWDO�!DGGBYHKLFOH��UBWUXFN���

***** insert business-partner of agency into partner_list***********

��UBDJHQF\�!DGGBSDUWQHU��UBUHQWDO���

******* show attributes of all partners of travel_agency ******

��UBDJHQF\�!GLVSOD\BDJHQF\BSDUWQHUV����

Page 305: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-30

*&---------------------------------------------------------------------*

*& 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

*---------------------------------------------------------------------*

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 constructor IMPORTING im_make TYPE string.

METHODS display_attributes.

METHODS set_make IMPORTING im_make TYPE string.

METHODS get_make EXPORTING ex_make TYPE string.

CLASS-METHODS get_count EXPORTING re_count TYPE i.

PRIVATE SECTION.

"-------------------

DATA: make TYPE string.

METHODS init_make.

CLASS-DATA: n_o_vehicles TYPE i.

ENDCLASS. "lcl_vehicle DEFINITION

Page 306: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-31

*---------------------------------------------------------------------*

* CLASS lcl_vehicle IMPLEMENTATION

*---------------------------------------------------------------------*

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( ). " me->init_make( ). also possible

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

ENDCLASS. "lcl_vehicle IMPLEMENTATION

Page 307: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-32

*---------------------------------------------------------------------*

* CLASS lcl_truck DEFINITION

*---------------------------------------------------------------------*

CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle.

PUBLIC SECTION.

"-------------------

METHODS: constructor IMPORTING im_make TYPE string

im_cargo TYPE ty_cargo.

METHODS display_attributes REDEFINITION.

METHODS get_cargo RETURNING value(re_cargo) TYPE ty_cargo.

PRIVATE SECTION.

"-------------------

DATA: max_cargo TYPE ty_cargo.

ENDCLASS. "lcl_vehicle DEFINITION

*---------------------------------------------------------------------*

* CLASS lcl_truck IMPLEMENTATION

*---------------------------------------------------------------------*

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

ENDCLASS. "lcl_vehicle DEFINITION

Page 308: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-33

*---------------------------------------------------------------------*

* CLASS lcl_bus DEFINITION

*---------------------------------------------------------------------*

CLASS lcl_bus DEFINITION INHERITING FROM lcl_vehicle.

PUBLIC SECTION.

"-------------------

METHODS: constructor IMPORTING im_make TYPE string

im_passengers TYPE i.

METHODS display_attributes REDEFINITION.

PRIVATE SECTION.

"-------------------

DATA: max_passengers TYPE i.

ENDCLASS. "lcl_vehicle DEFINITION

*---------------------------------------------------------------------*

* CLASS lcl_bus IMPLEMENTATION

*---------------------------------------------------------------------*

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

ENDCLASS. "lcl_vehicle DEFINITION

Page 309: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-34

*---------------------------------------------------------------------*

* CLASS lcl_rental DEFINITION

*---------------------------------------------------------------------*

&/$66�OFOBUHQWDO�'(),1,7,21�

��38%/,&�6(&7,21�����������������������������0(7+2'6�������FRQVWUXFWRU�,03257,1*�LPBQDPH�7<3(�VWULQJ�����0(7+2'6�������DGGBYHKLFOH�,03257,1*�LPBYHKLFOH����������������������������������7<3(�5()�72�OFOBYHKLFOH�����0(7+2'6�������GLVSOD\BDWWULEXWHV�����,17(5)$&(6��OLIBSDUWQHUV���35,9$7(�6(&7,21�����������������������������'$7$��QDPH�7<3(�VWULQJ�����������YHKLFOHBOLVW�7<3(�7$%/(�2)�5()�72�OFOBYHKLFOH�(1'&/$66����������������������OFOBUHQWDO�'(),1,7,21

*---------------------------------------------------------------------*

* CLASS lcl_rental IMPLEMENTATION

*---------------------------------------------------------------------*

&/$66�OFOBUHQWDO�,03/(0(17$7,21���0(7+2'�OLIBSDUWQHUVaGLVSOD\BSDUWQHU�����GLVSOD\BDWWULEXWHV������(1'0(7+2'����������������������OLIBSDUWQHUVaGLVSOD\BSDUWQHU

��0(7+2'��FRQVWUXFWRU�����QDPH� �LPBQDPH���(1'0(7+2'����������������������FRQVWUXFWRU

��0(7+2'��DGGBYHKLFOH�����$33(1'�LPBYHKLFOH�72�YHKLFOHBOLVW���(1'0(7+2'����������������������DGGBYHKLFOH

��0(7+2'��GLVSOD\BDWWULEXWHV�����'$7$��UBYHKLFOH�7<3(�5()�72�OFOBYHKLFOH�����:5,7(�����LFRQBWUDQVSRUWBSURSRVDO�$6�,&21��QDPH�����:5,7(����+HUH�FRPHV�WKH�YHKLFOH�OLVW����8/,1(��8/,1(�����/223�$7�YHKLFOHBOLVW�,172�UBYHKLFOH�������UBYHKLFOH�!GLVSOD\BDWWULEXWHV��������(1'/223���(1'0(7+2'����������������������GLVSOD\BDWWULEXWHV(1'&/$66����������������������OFOBUHQWDO�,03/(0(17$7,21

Page 310: 003 - Bc401 - Abap Objects

© SAP AG BC401 10-35

*---------------------------------------------------------------------*

* CLASS lcl_travel_agency DEFINITION

*---------------------------------------------------------------------*

&/$66�OFOBWUDYHOBDJHQF\�'(),1,7,21�

��38%/,&�6(&7,21�����������������������������0(7+2'6�������FRQVWUXFWRU�,03257,1*�LPBQDPH�7<3(�VWULQJ�����0(7+2'6�������DGGBSDUWQHU�,03257,1*�LPBSDUWQHU����������������������������������7<3(�5()�72�OLIBSDUWQHUV�����0(7+2'6�������GLVSOD\BDJHQF\BSDUWQHUV���35,9$7(�6(&7,21�����������������������������'$7$��QDPH�7<3(�VWULQJ�����������SDUWQHUBOLVW�7<3(�7$%/(�2)�5()�72�OLIBSDUWQHUV�(1'&/$66����������������������OFOBWUDYHOBDJHQF\�'(),1,7,21

*---------------------------------------------------------------------*

* CLASS lcl_travel_agency IMPLEMENTATION

*---------------------------------------------------------------------*

&/$66�OFOBWUDYHOBDJHQF\�,03/(0(17$7,21���0(7+2'�GLVSOD\BDJHQF\BSDUWQHUV�����'$7$��UBSDUWQHU�7<3(�5()�72�OLIBSDUWQHUV�����:5,7(��LFRQBGHSHQGHQWV�$6�,&21��QDPH�����:5,7(����+HUH�DUH�WKH�SDUWQHUV�RI�WKH�WUDYHO�DJHQF\���8/,1(�8/,1(�����/223�$7�SDUWQHUBOLVW�,172�UBSDUWQHU�������UBSDUWQHU�!GLVSOD\BSDUWQHU��������(1'/223�(1'0(7+2'����������������������GLVSOD\BDJHQF\BSDUWQHUV

0(7+2'��FRQVWUXFWRU���QDPH� �LPBQDPH�(1'0(7+2'����������������������FRQVWUXFWRU

0(7+2'��DGGBSDUWQHU���$33(1'�LPBSDUWQHU�72�SDUWQHUBOLVW�(1'0(7+2'����������������������DGGBSDUWQHU(1'&/$66����������������������OFOBWUDYHOBDJHQF\�,03/(0(17$7,21

Page 311: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-1

SAP AG 2002

l 'HILQLQJ�DQG�WULJJHULQJ�HYHQWVl 5HJLVWHULQJ�DQG�KDQGOLQJ�HYHQWV

&RQWHQWV�

(YHQWV

Page 312: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-2

SAP AG 2002

l 'HILQH�DQG�WULJJHU�HYHQWVl +DQGOH�HYHQWVl 5HJLVWHU�DQG�GHUHJLVWHU�HYHQWVl 5HFHLYH�D�UHIHUHQFH�IURP�WKH�VHQGHUl ([SODLQ�WKH�FRQFHSWXDO�GLIIHUHQFHV�EHWZHHQ

PHWKRGV�DQG�HYHQWV

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

(YHQWV��8QLW�2EMHFWLYHV

Page 313: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-3

SAP AG 2002

(YHQWV��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �� ��� ��� ����� � � � � ����� � � � � ! � � � ��� � ��" � � � � ��# # � � $ %�& & ' (�)�*,+.- ) + %�/10

% (32 * % 0 0 ' ( )�4 %�5 %6�7�8 9�:<; 9�= >@? 9�A�BC A�D B > 7E A = A C�F@G > 7 A 9�HE A = AJI DLK >�M = 7 8 9E > = A 8 B N,O�P�Q.R@SUTWV S Q V�X S@Y

Z,[.\ ]�^ \ _�` ab1c3d3e�f g a ^@h i e [.\ a ] h a@j Z [ c�k [ l�m m \ ] kn ]�o a [ \ h l ] ^ ap l b h \ ] kn ] h a [ d l ^@a b

qsrUtvu1wxqsr t u1wx

Page 314: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-4

SAP AG 2002

(YHQWV��2YHUYLHZ

&DU 9HKLFOH�UHJLVWUDWLRQ�RIILFH

&DU�UHQWDO�FRPSDQ\

�PDLQ�SURJUDP&5($7(�2%-(&7�UBFDU�(;3257,1*�...

y{z}|�~�z��

� z���z����}z���������|�~��@z��

���������<�����������<���3�<�3�<���<���������¡ �¢

n By triggering an event, an object or class announces a change of state, or that a certain state has beenachieved.

n 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 ofcompletion; the car is registered at the vehicle registration office.

n Note:The events discussed here are QRW�the events of the ABAP runtime system (such asINITIALIZATION,START-OF-SELECTION, and so on) and QRW�the events of background processing or workflow.

Page 315: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-5

SAP AG 2002

)HDWXUHV

l�/RRVHU�OLQNDJH�WKDQ�IRU�D�PHWKRG�FDOO£ ��7ULJJHULQJ�REMHFW�PDNHV�VWDWXV�FKDQJH�NQRZQ£ ��+DQGOHU�REMHFWV�FDQ�VKRZ�LQWHUHVW�DQG�UHDFW

l�'LIIHUHQW�FRPPXQLFDWLRQ�PRGHO¤ ��7KH�WULJJHU�GRHV�QRW�NQRZ�WKH�XVHU�����XVHUV�UHDFWLRQ�LV�RI�QR�LQWHUHVW�WR�WULJJHU

l�$UHDV�RI�XVH£ �*8,�LPSOHPHQWDWLRQV¥ �&RQIRUPLW\�ZLWK�RWKHU�REMHFW�PRGHOV��&20��$FWLYH;�FRQWUROV��2SHQ'RF

n Events link objects or classes more loosely than direct method calls do. Method calls establishprecisely when and in which statement sequence the method is called. However, with events, thereaction of the object to the event is triggered by the event itself.

n Events are most often used in GUI implementations.

n Other external object models, such as COM, ActiveX controls, and so on, also provide events.

Page 316: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-6

SAP AG 2002

7ULJJHULQJ�DQG�+DQGOLQJ�(YHQWV��2YHUYLHZ

l��7ULJJHULQJ�HYHQWV��&ODVV�GHILQHV�HYHQW���(9(176��&/$66�(9(176���2EMHFW�RU�FODVV�WULJJHUV�HYHQW���5$,6(�(9(17�

l��+DQGOLQJ�HYHQWV¦ �(YHQW�KDQGOHU�FODVV�GHILQHV�DQG�LPSOHPHQWV�HYHQWKDQGOHU�PHWKRG���>&/$66�@0(7+2'6����)25�(9(17�����2)�����¦ �(YHQW�KDQGOHU�REMHFW�RU�KDQGOHU�FODVV�UHJLVWHUV�LWVHOI�WRVSHFLILF�HYHQWV�DW�UXQWLPH���6(7�+$1'/(5�

5XOHV�§

¨

©

ª

n At the moment of implementation, a class defines its:

• Instance events (using the EVENTS statement)

• Static events (using the CLASS-EVENTS statement)

n Classes or their instances that receive a message when an event is triggered at runtime and want toreact to this event define event handler methods.Statement:[CLASS-]METHODS <handler_method> FOR EVENT <event> OF <classname>.

n 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)

n A class or instance can trigger an event at runtime using the RAISE EVENT statement.

Page 317: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-7

SAP AG 2002

'HILQLQJ�DQG�7ULJJHULQJ�(YHQWV��6\QWD[

&/$66��FODVVQDPH!�'(),1,7,21���(9(176���HYHQW!�(;3257,1*�9$/8(��H[BSDU!��7<3(��W\SH!�

&/$66�OFOBYHKLFOH�'(),1,7,21���38%/,&�6(&7,21�����0(7+2'6�FRQVWUXFWRU�,03257,1*�...������(9(176�YHKLFOHBFUHDWHG���...(1'&/$66�

&/$66��FODVVQDPH!�,03/(0(17$7,21���0(7+2'���P!����5$,6(�(9(17��HYHQW!�(;3257,1*��H[BSDU!� ��DFWBSDU!�

&/$66�OFOBYHKLFOH�,03/(0(17$7,21���0(7+2'�FRQVWUXFWRU�����...������5$,6(�(9(17�YHKLFOHBFUHDWHG���(1'0(7+2'�(1'&/$66�

FDU

§

¨«­¬�®W¯J°L±�²³®­´�±Wµ�®�¶�·¸®�¹sº

n Both instance and static events can be triggered in instance methods.

n Only static events can be triggered in static methods.

n Events can only have EXPORTING parameters which must be passed by value.

n Triggering an event using the statement RAISE EVENT has the following effect:

• The program flow is interrupted at that point

• The event handler methods registered to this event are called and processed

• Once all event handler methods have been executed, the program flow continues

n If an event handler method in turn triggers an event, then the program flow is again interrupted andall event handler methods are executed (nesting).

Page 318: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-8

SAP AG 2002

+DQGOLQJ�DQG�5HJLVWHULQJ�(YHQWV

UHJLVWUDWLRQRIILFH

UHQWDO»½¼J¾�¿

»�¼J¾¸À

Á ¾@Âû1ÄFDUULHU

ÅLÆ�ÇÉÈUÊ3È�Ë�Ç<ÌÉÆÍÈ�Î�ÈUÆ�Ç@Ï

y{z�|�~�z�� ��|�~��@z��

���¡�<���3�<�3�<���<�1�<������ �¢

n Events are registered using the SET HANDLER statement. Registration is only active at programruntime. Events cannot be persistent.

n You want to register an object to an event belonging to another object. The SET HANDLERstatement enters the registration in that object’s list. All handlers for one event are entered in this list.

n When the event is triggered, the list shows which event handler methods need to be called.

Page 319: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-9

SAP AG 2002

(YHQW�+DQGOHU�0HWKRGV

&/$66��FODVVBKDQGOH!�'(),1,7,21���0(7+2'6���RQBHYHQW!�)25�(9(17��HYHQW!����������������������2)��FODVVQDPH!�_��LQWHUIDFH!����������������������,03257,1*��H[BSDU�!������H[BSDU1!�>VHQGHU@�

&/$66�OFOBUHQWDO�'(),1,7,21��������35,9$7(�6(&7,21�����0(7+2'6��DGGBYHKLFOH�)25�(9(17�YHKLFOHBFUHDWHG�2)�OFOBYHKLFOH�����������������������������,03257,1*�VHQGHU�(1'&/$66�

FDU� UHQWDODGGBYHKLFOH

©

Ñ Î�È<Ò¡ÌLÓ�ÔLÈWÕvÓ<Ê3È�Ö×dzÈUØ�Ù

n Event handler methods are triggered by events (RAISE EVENT), although they can also be calledlike normal methods (CALL METHOD).

n The interface of the event handler method consists solely of IMPORTING parameters. You can onlyuse 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 declarationof the event. The parameters are typed in the event definition and the typing is passed to the eventhandler method, that is, the interface parameters of the event handler method cannot be typed in thedefinition of the event handler method.In addition to the explicitly defined event interface parameters, the implicit parameter sender canalso be listed as an IMPORTING parameter for instance events. This passes on a reference to theobject that triggered the event.

Page 320: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-10

SAP AG 2002

&/$66�OFOBUHQWDO�,03/(0(17$7,21��0(7+2'�FRQVWUXFWRU����...���6(7�+$1'/(5�DGGBYHKLFOH�)25�$//�,167$1&(6����(1'0(7+2'�(1'&/$66�

5HJLVWHULQJ�IRU�DQ�(YHQW��6\QWD[

6(7�+$1'/(5��UHIBKDQGOH!�!�RQBHYHQW!���������������������������)25��UHIBVHQGHU!�_�)25�$//�,167$1&(6��������������������������>$&7,9$7,21��YDU!@�

&/$66�OFOBUHQWDO�'(),1,7,21�����...��35,9$7(�6(&7,21�����0(7+2'6��DGGBYHKLFOH�)25�(9(17�YHKLFOHBFUHDWHG�2)�...(1'&/$66�

FDU� UHQWDOVHW�KDQGOHU�

ª

«­¬�®�¯J°É±½²³®­´�±Wµ�®�¶1·¸®�¹sº

n When an event is triggered, only those event handler methods are executed that have, by this point,registered themselves using SET HANDLER.

n You can register an event using ACTIVATION ’X’, and deregister it using ACTIVATIONspace. If you do not specify ACTIVATION, then the event registers (default behavior).

n You can register several methods in one SET HANDLER statement:SET HANDLER <ref_handle1>-><handler_method1> ... <ref_handleN>-><handler_methodN> FOR <ref_sender> | FOR ALL INSTANCES.

Page 321: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-11

SAP AG 2002

5HJLVWUDWLRQ�'HUHJLVWUDWLRQ��+DQGOHU�7DEOHV

ÚÛ�¡�� Ü�3�<���¸�¡Ý��­�ßÞ�à¡�Íá��<�×��¨��

UHJBYHKLFOH

YHKLFOHBFUHDWHG��HYHQW�5HJLVWHUHGREMHFW

+DQGOHUPHWKRG

FDU�

ÊâÕvÓUÖ�Ê

Úã���� ä�­�U���¸�¡Ý��3�åÞ�àÜ�æá��<�×��§�á

DGGBYHKLFOH

YHKLFOHBFUHDWHG��HYHQW�5HJLVWHUHGREMHFW

+DQGOHUPHWKRG

FDU�

ʸÕvÓUÖ�Ê

UHJBYHKLFOH

UHJLVWUDWLRQRIILFH

UHQWDO

UHJLVWUDWLRQRIILFH

n Every object that has defined events has an internal table, the handler table. All objects that haveregistered for events are entered in this table together with their event handler methods.

n Objects that have registered themselves for an event that is still "active" also remain "active". Themethods of these objects are called when the event is triggered, even if they can no longer be reachedusing main memory references.

Page 322: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-12

SAP AG 2002

(YHQW�+DQGOLQJ��)HDWXUHV

l (YHQW�KDQGOLQJ�LV�VHTXHQWLDOl 6HTXHQFH�LQ�ZKLFK�HYHQW�KDQGOHU�PHWKRGV�DUH�FDOOHG�LV�QRW�GHILQHGl :LWK�UHJDUG�WR�WKH�*DUEDJH�&ROOHFWRU��UHJLVWUDWLRQ�KDV�WKH�VDPH�HIIHFW

DV�D�UHIHUHQFH�WR�WKH�UHJLVWHUHG�REMHFW¤ 5HJLVWHUHG�REMHFWV�DUH�QHYHU�GHOHWHG

l 7KH�YLVLELOLW\�RI�DQ�HYHQW�GHILQHV�DXWKRUL]DWLRQ�IRU�HYHQW�KDQGOLQJl 7KH�YLVLELOLW\�LQ�DQ�HYHQW�KDQGOHU�PHWKRG�GHILQHV�DXWKRUL]DWLRQ�IRU

XVLQJ�6(7�+$1'/(5�VWDWHPHQWV¤ (YHQW�KDQGOHU�PHWKRGV��KRZHYHU��FDQ�RQO\�KDYH�WKH�VDPH�YLVLELOLW\�RU�PRUH

UHVWULFWHG�YLVLELOLW\�WKDQ�WKH�HYHQWV�WKH\�UHIHU�WR

n If several objects have registered for an event, then the sequence in which the event handler methodsare called is not defined, that is, there is no guaranteed sequence in which the event handler methodsare called.

n If a new event handler is registered in an event handler method for an event that has just beentriggered, then this event handler is added to the end of the sequence and is then also executed whenits turn comes.If an existing event handler is deregistered in an event handler method, then this handler is deletedfrom the event handler method sequence.

n Events are also subject to the visibility concept and can therefore be either public, protected, orprivate. Visibility specifies who can handle an event:

• PUBLIC: All users

• PROTECTED: Only users within that class or its subclasses

• PRIVATE: Only users within that class

n Event handler methods also have visibility attributes. Event handler methods, however, can onlyhave the same visibility or more restricted visibility than the events they refer to. The visibility ofevent handler methods establishes authorization for SET HANDLER statements; SET HANDLERstatements can be used: Everywhere, in the class and its subclasses, or only within the class.

Page 323: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-13

SAP AG 2002

l 'HILQH�DQG�WULJJHU�HYHQWVl +DQGOH�HYHQWVl 5HJLVWHU�DQG�GHUHJLVWHU�HYHQWVl ([SODLQ�WKH�FRQFHSWXDO�GLIIHUHQFHV�EHWZHHQ

PHWKRGV�DQG�HYHQWV

<RX�DUH�QRZ�DEOH�WR�

(YHQWV��8QLW�6XPPDU\

Page 324: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-14

�(YHQWV�([HUFLVHV8QLW��(YHQWV7RSLF��7ULJJHULQJ�DQG�+DQGOLQJ�(YHQWV

At the conclusion of these exercises, you will be able to:

• Define and trigger events

• Handle events

• Register event handler methods

As soon as a new airplane is created, this event must be made known tothe airline. .

0RGHO�VROXWLRQ� 6$3%&���B(9(6B0$,1B$6$3%&���B9(+'B+�include program6$3%&���B(9(6B$�include program

1 The DGGBDLUSODQH method of the class OFOBFDUULHU will no longer be explicitly called inthe PDLQ�SURJUDP, but be triggered automatically from the class OFOBDLUSODQH.Triggering an event when a plane is created (CREATE OBJECT) will result in theautomatic execution of the airline method DGGBDLUSODQH.

1-1 In the UML diagram decide what steps are needed where for the triggering andhandling of the event DLUSODQHBFUHDWHG.See next page for UML diagram.

1-2 Trigger the event DLUSODQHBFUHDWHG in a suitable method within the classOFOBDLUSODQH.

1-3 Handle the event within the class OFOBFDUULHU using the handler methodDGGBDLUSODQH. This method requires a new interface, its implementation mustalso be changed slightly.

1-4 In the main program, comment out the callsr_carrier->add_airplane.

1-5 In the debugger, check whether the event is triggered and handled by the eventhandler method when the planes are created.If this does not happen, check whether one of the four important steps forimplementing events was perhaps left out.

Page 325: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-15

��� �2SWLRQDO�Implement the event YHKLFOHBFUHDWHG for the car rental company and itscorresponding vehicles.

� 2SWLRQDO��DGYDQFHG��In the UML, there is the possibility of using events when creating the business partnersof the travel agency.If airlines, car rental companies, or hotels are created, these business partners shouldautomatically be made known to the travel agency.

2-1 You could solve this using the events FDUULHUBFUHDWHG, UHQWDOBFUHDWHG, andKRWHOBFUHDWHG. Would this be problematic?What would be the best solution?

2-2 Implement your solution for OFOBFDUULHU and OFOBUHQWDO.

Page 326: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-16

6ROXWLRQV8QLW� (YHQWV7RSLF� 7ULJJHULQJ�DQG�+DQGOLQJ�(YHQWV

*&---------------------------------------------------------------------*

*& Report SAPBC401_EVES_MAIN_A *

*& *

*&---------------------------------------------------------------------*

*& Implement Events in lcl_vehicle and lcl_airplane *

*&---------------------------------------------------------------------*

REPORT sapbc401_eves_main_a.

TYPES: ty_fuel TYPE p DECIMALS 2,

ty_cargo TYPE p DECIMALS 2.

INCLUDE <icon>.

include sapbc401_vehd_h.

INCLUDE sapbc401_eves_a.

DATA: r_vehicle TYPE REF TO lcl_vehicle,

r_truck TYPE REF TO lcl_truck,

r_bus TYPE REF TO lcl_bus,

r_passenger type ref to lcl_passenger_plane,

r_cargo type ref to lcl_cargo_plane,

r_carrier type ref to lcl_carrier,

r_rental type ref to lcl_rental,

r_agency type ref to lcl_travel_agency.

START-OF-SELECTION.

*########################

***** Create TRAVEL_AGENCY **************************************

CREATE OBJECT r_agency EXPORTING im_name = ’Fly&Smile Travel’.

***** Create CARRIER ********************************************

Page 327: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-17

create object r_carrier exporting im_name = ’Smile&Fly-Travel’.

***** Passenger Plane ********************************************

CREATE OBJECT r_passenger EXPORTING

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

***** insert business-parnter of agency into partner_list***********

r_agency->add_partner( r_carrier ).

******* create RENTAL *****************************************

CREATE OBJECT r_rental EXPORTING im_name = ’HAPPY CAR RENTAL’.

******* create truck *****************************************

CREATE OBJECT r_truck EXPORTING im_make = ’MAN’

im_cargo = 45.

��UBUHQWDO�!DGGBYHKLFOH��UBWUXFN���******* create truck *****************************************

CREATE OBJECT r_bus EXPORTING im_make = ’Mercedes’

im_passengers = 80.

��UBUHQWDO�!DGGBYHKLFOH��UBEXV���******* create truck *****************************************

CREATE OBJECT r_truck EXPORTING im_make = ’VOLVO’

im_cargo = 48.

��UBUHQWDO�!DGGBYHKLFOH��UBWUXFN���

***** insert business-parnter of agency into partner_list***********

r_agency->add_partner( r_rental ).

******* show attributes of all partners of travel_agency ******

r_agency->display_agency_partners( ).

Page 328: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-18

*&---------------------------------------------------------------------*

*& Include SAPBC401_EVES_A *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

"---------------------------------------------

CONSTANTS: pos_1 TYPE i VALUE 30.

METHODS: constructor IMPORTING

im_name TYPE string

im_planetype TYPE saplane-planetype,

display_attributes.

CLASS-METHODS: display_n_o_airplanes.

����HYHQWV�DLUSODQHBFUHDWHG�

PRIVATE SECTION.

"----------------------------------------------

METHODS: get_technical_attributes

IMPORTING im_type TYPE saplane-planetype

EXPORTING ex_weight TYPE s_plan_wei

ex_tankcap TYPE s_capacity.

DATA: name TYPE string,

planetype TYPE saplane-planetype.

CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS. "lcl_airplane DEFINITION

Page 329: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-19

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

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

ENDCLASS. "lcl_airplane IMPLEMENTATION

Page 330: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-20

*---------------------------------------------------------------------*

* CLASS lcl_cargo_plane DEFINITION

*---------------------------------------------------------------------*

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

*---------------------------------------------------------------------*

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

ENDCLASS. "lcl_cargo_plane IMPLEMENTATION

Page 331: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-21

*---------------------------------------------------------------------*

* CLASS lcl_passenger_plane DEFINITION

*---------------------------------------------------------------------*

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

*---------------------------------------------------------------------*

*

*---------------------------------------------------------------------*

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

ENDCLASS. "lcl_passenger_plane IMPLEMENTATION

Page 332: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-22

*---------------------------------------------------------------------*

* CLASS lcl_carrier DEFINITION

*---------------------------------------------------------------------*

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,

airplane_list TYPE TABLE OF REF TO lcl_airplane.

ENDCLASS. "lcl_carrier DEFINITION

*---------------------------------------------------------------------*

* CLASS lcl_carrier IMPLEMENTATION

*---------------------------------------------------------------------*

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

Page 333: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-23

METHOD constructor.

name = im_name.

����VHW�KDQGOHU�DGGBDLUSODQH�IRU�DOO�LQVWDQFHV�ENDMETHOD. "constructor

METHOD get_name.

ex_name = name.

ENDMETHOD. "get_name

ENDCLASS. "lcl_carrier IMPLEMENTATION

Page 334: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-24

6ROXWLRQV��RSWLRQDO�8QLW� (YHQWV7RSLF� 7ULJJHULQJ�DQG�+DQGOLQJ�(YHQWV

*&---------------------------------------------------------------------*

*& Report SAPBC401_EVES_MAIN_B *

*& *

*&---------------------------------------------------------------------*

*& Implement Events in LCL_vehicle and lcl_airplane *

*& Implement Events in LCL_carrier and lcl_rental *

*&---------------------------------------------------------------------*

REPORT sapbc401_eves_main_b.

TYPES: ty_fuel TYPE p DECIMALS 2,

ty_cargo TYPE p DECIMALS 2.

INCLUDE <icon>.

include sapbc401_vehd_i.

INCLUDE sapbc401_eves_b.

DATA: r_vehicle TYPE REF TO lcl_vehicle,

r_truck TYPE REF TO lcl_truck,

r_bus TYPE REF TO lcl_bus,

r_passenger type ref to lcl_passenger_plane,

r_cargo type ref to lcl_cargo_plane,

r_carrier type ref to lcl_carrier,

r_rental type ref to lcl_rental,

r_agency type ref to lcl_travel_agency.

START-OF-SELECTION.

*########################

******* create travel_agency *****************************************

CREATE OBJECT r_agency EXPORTING im_name = ’Fly&Smile Travel’.

******* create rental *****************************************

CREATE OBJECT r_rental EXPORTING im_name = ’HAPPY CAR RENTAL’.

Page 335: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-25

******* create truck *****************************************

CREATE OBJECT r_truck EXPORTING im_make = ’MAN’

im_cargo = 45.

******* create truck *****************************************

CREATE OBJECT r_bus EXPORTING im_make = ’Mercedes’

im_passengers = 80.

******* create truck *****************************************

CREATE OBJECT r_truck EXPORTING im_make = ’VOLVO’

im_cargo = 48.

***** Create CARRIER ********************************************

create object r_carrier exporting im_name = ’Smile&Fly-Travel’.

***** Passenger Plane ********************************************

CREATE OBJECT r_passenger EXPORTING

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.

******* show attributes of all partners of travel_agency ******

r_agency->display_agency_partners( ).

Page 336: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-26

*&---------------------------------------------------------------------*

*& Include SAPBC401_EVES_B *

*&---------------------------------------------------------------------*

*------------------------------------------------------------------*

* events in: lcl_airplane and lcl_carrier !

*

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

"---------------------------------------------

CONSTANTS: pos_1 TYPE i VALUE 30.

METHODS: constructor IMPORTING

im_name TYPE string

im_planetype TYPE saplane-planetype,

display_attributes.

CLASS-METHODS: display_n_o_airplanes.

EVENTS:airplane_created.

PRIVATE SECTION.

"----------------------------------------------

METHODS: get_technical_attributes

IMPORTING im_type TYPE saplane-planetype

EXPORTING ex_weight TYPE s_plan_wei

ex_tankcap TYPE s_capacity.

DATA: name TYPE string,

planetype TYPE saplane-planetype.

CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS. "lcl_airplane DEFINITION

Page 337: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-27

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

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. ex_weight = 100000. ex_tankcap = 10000. ENDIF.

ENDMETHOD. "get_technical_attributes

ENDCLASS. "lcl_airplane IMPLEMENTATION

...

Page 338: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-28

*---------------------------------------------------------------------*

* CLASS lcl_carrier DEFINITION

*---------------------------------------------------------------------*

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,

airplane_list TYPE TABLE OF REF TO lcl_airplane.

ENDCLASS. "lcl_carrier DEFINITION

*---------------------------------------------------------------------*

* CLASS lcl_carrier IMPLEMENTATION

*---------------------------------------------------------------------*

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

Page 339: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-29

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� �HYHQW�GHILQHG�LQVLGH�WKH�LQWHUIDFH���� ��(9(176��SDUWQHUBFUHDWHG�(1',17(5)$&(����������������������OLIBSDUWQHUV

*---------------------------------------------------------------------*

* CLASS lcl_vehicle DEFINITION

*---------------------------------------------------------------------*

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 constructor IMPORTING im_make TYPE string.

METHODS display_attributes.

METHODS set_make IMPORTING im_make TYPE string.

METHODS get_make EXPORTING ex_make TYPE string.

CLASS-METHODS get_count EXPORTING re_count TYPE i.

EVENTS: vehicle_created.

PRIVATE SECTION.

"-------------------

DATA: make TYPE string.

METHODS init_make.

Page 340: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-30

CLASS-DATA: n_o_vehicles TYPE i.

ENDCLASS. "lcl_vehicle DEFINITION

*---------------------------------------------------------------------*

* CLASS lcl_vehicle IMPLEMENTATION

*---------------------------------------------------------------------*

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( ). " me->init_make( ). also possible

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

ENDCLASS. "lcl_vehicle IMPLEMENTATION

Page 341: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-31

*---------------------------------------------------------------------*

* CLASS lcl_rental DEFINITION

*---------------------------------------------------------------------*

CLASS lcl_rental DEFINITION.

PUBLIC SECTION.

"-------------------

METHODS: constructor IMPORTING im_name TYPE string.

����0(7+2'6�������DGGBYHKLFOH�IRU�HYHQW�YHKLFOHBFUHDWHG�RI�OFOBYHKLFOH�������������������������LPSRUWLQJ�VHQGHU� METHODS display_attributes.

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

*---------------------------------------------------------------------*

CLASS lcl_rental IMPLEMENTATION.

METHOD lif_partners~display_partner.

display_attributes( ).

ENDMETHOD. "lif_partners~display_partner

METHOD constructor.

name = im_name.

set handler add_vehicle for all instances.

����UDLVH�HYHQW�OLIBSDUWQHUVaSDUWQHUBFUHDWHG� ENDMETHOD. "constructor

METHOD add_vehicle.

APPEND sender TO vehicle_list.

ENDMETHOD. "add_vehicle

Page 342: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-32

METHOD display_attributes.

DATA: r_vehicle TYPE REF TO lcl_vehicle.

skip 2.

WRITE: / icon_transport_proposal AS ICON, name.

WRITE: ’ Here comes the vehicle list: ’. ULINE. ULINE.

LOOP AT vehicle_list INTO r_vehicle.

r_vehicle->display_attributes( ).

ENDLOOP.

ENDMETHOD. "display_attributes

ENDCLASS. "lcl_rental IMPLEMENTATION

*---------------------------------------------------------------------*

* CLASS lcl_travel_agency DEFINITION

*---------------------------------------------------------------------*

CLASS lcl_travel_agency DEFINITION.

PUBLIC SECTION.

"-------------------

METHODS: constructor IMPORTING im_name TYPE string.

����0(7+2'6����DGGBSDUWQHU�IRU�HYHQW�SDUWQHUBFUHDWHG�RI�OLIBSDUWQHUV�������������������������������,03257,1*�VHQGHU� 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

*---------------------------------------------------------------------*

CLASS lcl_travel_agency IMPLEMENTATION.

METHOD display_agency_partners.

DATA: r_partner TYPE REF TO lif_partners.

WRITE: icon_dependents AS ICON, name.

WRITE: ’ Here are the partners of the travel agency: ’.ULINE.ULINE.

LOOP AT partner_list INTO r_partner.

r_partner->display_partner( ).

ENDLOOP.

ENDMETHOD. "display_agency_partners

Page 343: 003 - Bc401 - Abap Objects

© SAP AG BC401 11-33

METHOD constructor.

name = im_name.

��VHW�KDQGOHU�DGGBSDUWQHU�IRU�DOO�LQVWDQFHV�ENDMETHOD. "constructor

METHOD add_partner.

��$33(1'�VHQGHU�72�SDUWQHUBOLVW�ENDMETHOD. "add_partner

ENDCLASS. "lcl_travel_agency IMPLEMENTATION

Page 344: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-1

SAP AG 2002

l /RFDO�YHUVXV�JOREDO�FODVVHV�DQG�LQWHUIDFHVl &ODVV�%XLOGHU

&RQWHQWV�

*OREDO�&ODVVHV�DQG�,QWHUIDFHV

Page 345: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-2

SAP AG 2002

l 'HVFULEH�WKH�GLIIHUHQFH�EHWZHHQ�ORFDO�DQG�JOREDOFODVVHV�DQG�LQWHUIDFHV

l &UHDWH�JOREDO�FODVVHV�DQG�LQWHUIDFHV�XVLQJ�WKH&ODVV�%XLOGHU

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

*OREDO�&ODVVHV�DQG�,QWHUIDFHV��8QLW�2EMHFWLYHV

Page 346: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-3

SAP AG 2002

*OREDO�&ODVVHV�DQG�,QWHUIDFHV��,QWHJUDWLRQ�LQ&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �� ��� ��� ����� � � � � ������� � � � � � � � � � ��!

� � � � �" " � � #%$�& & ' (�)�*,+.- ) + $�/10$ (32 * $ 0 0 ' ( )�4 $�5 $687�9 :�;=< :�> ?@ :�A�BC A�D B ? 7E A > A C�FG ? 7 A :IHE A > AKJ D�L ?�M > 7 9 :E ? > A 9 B N%O�PIQ R�SUTWV S Q V�X SY

Z,[.\ ]I^ \ _3` acbKd3e3fhgji a ^ck l f [.\ a ] k am Z�[ dIn[ o�p p \ ] nq ]3r a [ \ k o ]�^ a

s o b k \ ] nq ] k a [ e o ^ a bt�u a ] k bvxw y{z1| w�} w |U~ ~U� ~ |��1��� �U� ����� |��=� ~v w y{z1| w�} w | ~ ~U� ~ | �1� � �U� � ��� | � � ~

Page 347: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-4

SAP AG 2002

&ODVV�%XLOGHU&ODVV�%XLOGHU

:RUNLQJ�ZLWK�JOREDO�FODVVHV�DQG�LQWHUIDFHV:RUNLQJ�ZLWK�JOREDO�FODVVHV�DQG�LQWHUIDFHV

$SSOLHG�H[DPSOH$SSOLHG�H[DPSOH

*OREDO�&ODVVHV�DQG�,QWHUIDFHV����

Page 348: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-5

SAP AG 2002

5HYLHZ��/RFDO�&ODVVHV�DQG�,QWHUIDFHV

5(3257�SURJ��&/$66�OFOBDLUSODQH�'(),1,7,21����...(1'&/$66�...

5(3257�SURJ��'$7$��UBDLUSODQH�������7<3(�5()�72�OFOBDLUSODQH�...

l /RFDO�SURJUDP�FODVVHV� /RFDO�FODVVHV�DUH�RQO\�YLVLEOH�LQ�WKH�SURJUDP�WKH\�ZHUH�GHILQHG�LQ� 1R�JOREDO�DFFHVV�SRVVLEOH� 1RW�VWRUHG�LQ�WKH�5HSRVLWRU\��QR�ZKHUH�XVHG�OLVW��DQG�VR�RQ

3URJUDPP�� 3URJUDPP��

n Local classes and interfaces are only known within the program in which they are defined andimplemented.

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

Page 349: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-6

SAP AG 2002

*OREDO�&ODVVHV�DQG�,QWHUIDFHV

l $%$3�:RUNEHQFK�GHYHORSPHQW�WRRO�� ���������������j�����=�l 7RRO�IRU�FUHDWLQJ��WHVWLQJ��DQG�PDQDJLQJ�JOREDO�FODVVHV�DQG

LQWHUIDFHV� 6WRUHG�LQ�5HSRVLWRU\� *HQHUDWHV�WKH�IUDPHZRUN�FRGLQJ�

IRU�H[DPSOH�&/$66��QDPH!�'(),1,7,21� 0DQDJHV�WKH�LQFOXGH�SURJUDPV�LQ�ZKLFK�WKH�FRGLQJ�LV�VWRUHG

l $FFHVV�IURP�DOO�SURJUDPV�XVLQJ�7<3(�5()�72l &XVWRPHU�QDPHVSDFH�IRU�JOREDO�FODVVHV�DQG�LQWHUIDFHV����< ���RU

= l :KHUH�XVHG�OLVW�DYDLODEOH

n Unlike local program classes and interfaces, global classes and interfaces can be created andimplemented using the ABAP Workbench tool &ODVV�%XLOGHU. These classes and interfaces are thenavailable active throughout the whole system.

n Global class and interface names share the same namespace.

n Individual methods of global classes and interfaces can be transported separately.

Page 350: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-7

SAP AG 2002

�������=�¡ £¢¥¤K¦{§��¡¨¥���=§©¥§3ª�«�¬=¨­�®�¯¬�¨�ª�«¥¬U¨

°�±�²�³´±�µI¶·%¸%¹»º�¼I½�¾�¼¼

¿»¶�À�Á� ÃMIME RepositoryRepository BrowserRepository Information SystemTag LibraryTransport Organizer

Object Name Description

SULYDWHSURWHFWHG¦{«¥©­Ä���¬DEVWUDFW

&UHDWLQJ�*OREDO�&ODVVHV�LQ�WKH��2EMHFW�1DYLJDWRU

,QVWDQFH�FUHDWLRQ

1RUPDO�$%$3�FODVV([FHSWLRQ�FODVV3HUVLVWHQW�FODVV

)LQDO�FODVV2QO\�PRGHOHG

·,¸»¹%º�¼I½�¾�¼�¼¹hÅ ±cÆcÆ=Ç�È ÉI ±I ʹ�Å ±ÆÆ�¶cÆ

&UHDWH�D�FODVV�LQ�WKH�2EMHFW1DYLJDWRU

Workbench Edit Goto Utilities Environment System Help

n You can also display the Class Builder’s global classes in the Object Navigator.

n Being able to view classes through the navigation window on the left is especially useful whendeveloping code.

n Here, you can also create a new class. Proceed as you would when creating a program: Use thecontext menu for a package node or directly for a class node within a package.

n Alternatively, you can also create a new class by entering a class name and then choosing 'LVSOD\. Inthis case, you must specify a package.

n 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)

Page 351: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-8

SAP AG 2002

&ODVV�%XLOGHU��$WWULEXWHV

Class Edit Goto Utilities Environment System Help

���������Ë�Ì�­�´���{�U�KÍÎ��Ï{��Ð�ÑÒ�����������ÔÓÕ��Ö{×�ØÚÙ´ÛÝÜÞÖ{ØÚßÚàá×ãâ�â

ä¥å8æèç%éhê»ë

ì=æ�æ�í�î ïWð,æñåò�ó%ôõå

ö%å3÷»å»ø ù�î ë�î ï,ú û å%ú ü�ýhþ î ÿ�� ì=ë8ë%é��,ú ü»ýhþ å � å8ë���í�î þ æjî é�ÿä�éWêhú

��÷»å»ÿ,æñë ��ÿ8æñåWí�ÿ%ó»ø ü�ýhþ å3ë ì¥ø î ó�ë»å�ëì�æ�æèí�î ïWð�æ¯å3ë�hí�î å»ÿ»ê»ë��ÿ8æñåWíñó���å8ë��í é þ å»í�æjî å8ë

� ö8ì ò��� ü�� ���ò� ��� %ì�� û ��ö�ì­ò����

ü�ýhþ å � ü�û �.ò�� ò�ó%ôõå�é��ó»î í þ ø óWÿ,åü�ýhþ å � ì�� ö�ì�ò�� ì­î í þ ø ó�ÿ%å�æ ýhþ åü�ýhþ å � òUð»ô�ï,åWíhé��ó»î í þ ø óWÿ,å8ë

� ø ó8ë�ë��.ÿ�æ åWíñó���å � � ö� ,ì�� û � ö�ì­ò�� �! ! � ô þ ø å»ô�åWÿ8æñå%ê�" ì#��æjî ÷»å� ø ó,ë8ë � éhÿ,ë8æèí�ð ��æ¯é�í

�.ÿ8ë,æñó�ÿ ��íjî ÷�.ÿ8ë,æñó�ÿ ��íjî ÷� æ ó8æèî � ��íjî ÷

�hî ø æñå�í

'HILQH�FODVVDWWULEXWHV

n In the Class Builder, choosing the $WWULEXWHV tab displays the class attributes and their properties(instance attribute/static attribute and the visibility area)

Page 352: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-9

SAP AG 2002

&ODVV�%XLOGHU��0HWKRGV

Class Edit Goto Utilities Environment System Help

���������Ë�Ì�­�´���{�U�KÍÎ��Ï{��Ð�ÑÒ�����������ÔÓÕ��Ö{×�ØÚÙ�ÛÚÜÞÖÎØÚß àÞ×{â�â

ä¥å8æjç,éhê»ë

� �­ò$� ü û�% � ü � ûì¥î í þ ø ó�ÿ%å�ó3æñæjíèî ïhð�æñå8ëò=ð%ô{ï,åWíWé� þ ø ó�ÿ,å�ë

�hóWí.ó»ô�å8æ¯å»í�ë ��&���å þ æèî é ÿ,ë� �­ò�� ü�û�% � ü � û

ö%åI÷�å�ø ù=î ëWî ïWî ø î æ ý ä�å8æ¯ú � å8ë���í�î þ æèî é�ÿ

'�(*)*+�, - . +/,0)*12+*3 465 7 8*)/-:9�4 ; 3/-/)/-9�,,<1=; >�?�,0)@�1=; ) +*A -. +/,0)�1B<3DC*)E-F�1<G�8�)�1,=; )E-

��� ��� ö8ì � ì ühü�û � H % ü ������ ��� ö8ì � �ò� ��� »ì�� û ��ö8ì ò����

� ø ó8ë�ë�� ÿ8æñåWíñó���å � � ö� %ìI� û ��ö�ì­ò��� �! ! � ô þ ø å�ô�åWÿ�æ å%ê�" ì#��æèî ÷»å

ä¥å8æjç,éhê»ë� ÿ8ë,æ ó�ÿ���å ä¥å8æèç%éhêJ� ðhï»ø î �� ÿ8ë,æ ó�ÿ���å ä¥å8æèç%éhêJ� ðhï»ø î ���æ¯ó�æjî ��ä�å8æèç,éhê � ðhï»ø î �

'HILQH�VLJQDWXUH�RI�VHOHFWHGPHWKRG�

KHUH��&216758&725

� é�ÿ3ë%æjí�ð���æ é�í

'HILQH�FODVVPHWKRGV

ä�éWê�ú

n Under the 0HWKRGV�tab, you can create methods.

n This task is simplified by the &RQVWUXFWRU�function, choosing which automatically creates theconstructor method. You still have to specify the interface and the source code for the method.

n Choose 3DUDPHWHU�to define the signature for the method the cursor is currently on.

Page 353: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-10

SAP AG 2002

Class Edit Goto Utilities Environment System Help

���������Ë�Ì�­�´���{�U�KÍÎ��Ï{��Ð�ÑÒ�����������ÔÓÕ��Ö{×�ØÚÙ�ÛÚÜÞÖÎØÚß àÞ×{â�â

ä¥å8æjç,éhê»ë

� ��ò�� ü û�% � ü � ûì­î í þ ø ó�ÿ%å�ó�æ�æèíjî ïhð3æ¯å8ëòUð»ô�ï,åWíhé� þ ø ó�ÿ%å3ë

�hóWí.ó»ô�å8æ¯å»í�ë ��&���å þ æèî é ÿ,ë� �­ò�� ü�û�% � ü � û

ö%åI÷�å�ø ù�î ë»î ï�î ø î æ ý ä�å8æ¯ú � å8ë���í�î þ æèî é�ÿ

'�(*)*+�, - . +/,0)*12+*3 465 7 8*)/-:9�4 ; 3/-/)/-9�,,212; >�?/,0)@�1=; ) +*A -. +/,0)�1B<3DC*)E-F�1<G�8�)�1,=; )E-

��� ��� ö8ì � ì ühü�û � H % ü ������ ��� ö8ì � �ò� ��� »ì�� û ��ö8ì ò����

� ø ó8ë�ë�� ÿ8æñåWíñó���å � � ö� %ìI� û ��ö�ì­ò��� �! ! � ô þ ø å�ô�åWÿ�æ å%ê�" ì#��æèî ÷»å

ä¥å8æjç,éhê»ë � ÿ8ë,æ ó�ÿ���å ä¥å8æèç%éhêJ� ðhï»ø î �� ÿ8ë,æ ó�ÿ���å ä¥å8æèç%éhêJ� ðhï»ø î ���æ¯ó�æjî ��ä�å8æèç,éhê � ðhï»ø î �

� � �� K�hö ��L � ü ì û�ü ��ÿ8æñåWíñó���åõä¥å8æèç%éhê � ðhïWø î � � í å þ ó�íñó�æèî é�ÿ,ëM éhíWë,æñó�í�æ

���0HWKRG�LPSOHPHQWDWLRQ�RID�JOREDO�LQWHUIDFH

&ODVV�%XLOGHU��,QWHUIDFH�0HWKRGV

ä¥éWê�ú

���$QQRXQFH�D�JOREDOLQWHUIDFH�LQ�D�JOREDO�FODVV

n A global interface that was created in the Class Builder can be announced in a server class bychoosing the ,QWHUIDFHV tab.

n All interface methods are then automatically listed under the 0HWKRGV tab.

n All interface methods must then be implemented in this server class. In the example, the interfacezif_fly consists of a single method start.

Page 354: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-11

SAP AG 2002

&ODVV�%XLOGHU��0HWKRG�,QWHUIDFH

Class Edit Goto Utilities Environment System Help

���������Ô�Ì�¥���3���=�KÍÎ��Ï{��Ð{ÑÒ�����������ËÓÕ��Ö{×�ØÚÙ�ÛÚÜÞÖ{ØÚßÝàÞ×ãâ�â

ä­å8æjç,éhê%ë '�(�) +/,- . +/,0)*12+*3 465 7 8*)/- 9�4 ; 3D-�)D-9�,,012; >�?/,0)E-@ 12; ) +*A -. +/,0)*1B<3/C/)D-F�1G�8 )�1,=; )E-

ä¥å8æèç%éhê%ë ��&��3å þ æèî é�ÿ,ë� ä# �òhì äN�

� � ü�ýhþ î ÿ�� ì�ë�ë»é��%ú ü%ý�þ å � å ñó�ðhø æWùKó»ø ð�åO��å�ë��,í�î þ æèî é�ÿ� äI P� ö8ì ò�� ü�� �K�

ü�ýWþ å � ü û � ò��ü�ýWþ å � ì���ö3ì¥ò��PQñú ú ú � ø ó»ÿ%å�æ ýWþ åü�ýWþ å �ü�ýWþ å �

ä­å,æèç%éhêR�hó%í¯ó8ô å3æñåWí.ë � �¥ò�� ü�û�% � ü � û

� ø ó8ë�ë�� ÿ8æñåWíñó���å � � ö� 8ì�� û ��ö3ì¥ò��� �!*! � ô þ ø å»ô�åhÿ�æ å%ê�" ìS�8æèî ÷»å� é�ÿ8ë,æèí�ð��3æ é�í

�WóWí�ó»ô�å8æñå�í

6LJQDWXUH�SDUDPHWHUV�RIPHWKRG�

KHUH��&216758&725

n Selecting a method and choosing 3DUDPHWHUV displays a list of all the method’s signature parameters.

Page 355: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-12

SAP AG 2002

&ODVV�%XLOGHU��0HWKRG�,PSOHPHQWDWLRQ

���������������j�3�{�=� Í{��Ï{��Ð�ÑÕ���£�´������ÓÕ��ÖÎ×�Ø ÙjÛÚÜÞÖ{ØÚßÝàÞ×ãâ�â

äN� ü�T ���U��é�ÿ8ë,æèí�ð���æ¯é�í úôõó�V�åMWÎî ôX �ÿ�ó�ô�å»úþ ø óWÿ8å/ »æ ý�þ å�W�î ô� þ ø ó�ÿ%å3æ ý�þ å»úì����ZY ü � ÿ/ hé[ �óWî í þ ø ó»ÿ%å�ë�ú� ò�� ä\� ü�T �[� ú

�Uî ��ÿ%ó3æ�ð�í�å

ä¥å8æjç,éhê � �¥ò$� ü�û�% � ü � û ó ��æ�î ÷»å

ü�ý�þ å� ä# ò�ì äM� ü�� ����� ü û � ò$�� ä# K� ö�ì­ò�� ü�� ��� ü�� ������ìX� ö�ì�ò���Q=� ö8ì ò�� ü�� ���]��ø ó�ÿ%å�æ ýWþ å

ü�ýhþ å¥ë þ å �%ú � å3ë��»íèî þ æèî é�ÿ�WóWí�ó»ô�å8æñå�í�ë

'LVSOD\�WKH�LQWHUIDFH�WRKHOS�ZLWK�LPSOHPHQWLQJWKH�PHWKRG

'LVSOD\�WKH�LQWHUIDFH�WR�KHOSZLWK�LPSOHPHQWLQJ�WKH�PHWKRG

Method Edit Goto Utilities Environment System Help

n When implementing methods, you can also display the signature of the method to aid your work.

n In this example (constructor), the signature consists of the import parameters im_make andim_planetype.

Page 356: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-13

SAP AG 2002

&ODVV�%XLOGHU��,QKHULWDQFH

Class Edit Goto Utilities Environment System Help

���������Ë�Ì�­�´���{�U�KÍÎ��Ï{��Ð�ÑÒ����������� �áÖ�× ^ àZ_ Ù���Ö{à

ä¥å8æèç%éhê»ë ��÷»å»ÿ,æñë ��ÿ8æñåWí�ÿ%ó»ø ü�ýhþ å3ë ì¥ø î ó�ë»å�ëì�æñæèí�î ïWð8æ å3ë�hí�î å»ÿ»ê»ë��ÿ8æñåWíñó���å8ë��í é þ å»í�æjî å8ë� ø ó8ë�ë��.ÿ�æ åWíñó���å � � ö� � ì û �S�� K� ö8ì ò��� �!�! � ô þ ø å»ô�åWÿ8æñå%ê�" ì#��æjî ÷»å

� ø ó,ë8ë � éhÿ,ë8æèí�ð ��æ¯é�í$QQRXQFH�VXSHUFODVV

6XSHUFODVV6XSHUFODVV

6XSHUFODVV =&/B$,53/$1(B��Description

Instantiation

������

Final���

n In the Class Builder, inheritance relationships are defined by choosing the 3URSHUWLHV�tab.

n Here, for example, is the 6XSHUFODVV�function, which you can use to specify the superclass for thecurrent class.

n In this example, the superclass ZCL_AIRPLANE_00 is specified for the subclassZCL_CARGO_PLANE_00.

Page 357: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-14

SAP AG 2002

` a�Æà ±6aI²¶

&UHDWH�DQ�LQVWDQFH�ZLWKWKH�WHVW�HQYLURQPHQW

b�` c»°8Ç6d�e3¾�f�¾/g»¾DdK` ¿W°,ÇhdKf�i*c

jWÀ�À�¶� k Ç�ÁDl ¶�Â3¹»±cÆ�¶�dh²Ã.È m�¶·�¹�Ç�¾Ed�` ¿W°,ÇndPf�i�¾�¼�¼

dWà à ÂñÈ É/o�à ¶cÆp ¶cÃ<q�Á/rIÆs�¶cÆ�à g Éut ¶²Ã v wj»À�À�¶� k Ç�ÁEl ¶IÂI¹W±cÆ�¶�dh²cÃñÈ m�¶

·�¹hÇ�¾DdP` ¿W°,ÇndPf�i�¾�¼¼ ºExhy�¹WÇ´¾DdP` ¿�°,ÇndKf�i�¾3¼¼` a�à ¶� z ±²¶cÆdWà Ã.Â�È ÉDo�à ¶Æp�¶cà q�ÁEr3Æ

b�` c»°�Çnd�e3¾EdPs/s�¿�` ¸�j�s i/cb�` c»°�Çnd�e3¾*f�¾Dg�¾6d�` ¿�°,Çhd�f�i*c

&ODVV�%XLOGHU��7HVW�(QYLURQPHQW

Class Edit Goto Utilities{�|�}�~������}�}������P������M�����������#�P�{K|�}�~������}P}Z���S�K���\��N�I�������������Handler

Handler

n If the class is completed and activated, you can use the test tool to create an instance of the class andtest method calls of the class.

Page 358: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-15

SAP AG 2002

&ODVV�%XLOGHU&ODVV�%XLOGHU

:RUNLQJ�ZLWK�JOREDO�FODVVHV�DQG�LQWHUIDFHV:RUNLQJ�ZLWK�JOREDO�FODVVHV�DQG�LQWHUIDFHV

$SSOLHG�H[DPSOH$SSOLHG�H[DPSOH

*OREDO�&ODVVHV�DQG�,QWHUIDFHV����

Page 359: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-16

SAP AG 2002

���*�K���K�*���K�������������K�*�����������������������������������������������������������������������$�������P���������/ �¡�¢�� £�¤�¡�¥�¦��§P¨������D�K©�Dª�¨��������E��©«�����

�������=�¡ £¢¥¤K¦{§��¡¨¥���=§©¥§3ª�«�¬=¨­�®�¯¬�¨�ª�«¥¬U¨

¹�Å ±cÆÆnk ` a�à ¶I z ±�²¶·8¹hÇ�¾DdK` ¿»°,Çnd�f�i�¾3¼¼

¿%¶�ÀIÁ3Â.à ¬hr�¶E­KÁ d�²cÃñÈ m�¶MIME RepositoryRepository BrowserRepository Information SystemTag LibraryTransport Organizer

·�¹�Ç�¾EdP` ¿W°,ÇndPf�i�¾�¼¼Class Airplane edWà à Â�È É/o�à ¶cÆ® f�d$p�iName of airplane® f�¾/g�¾Ed�` ¿W°,ÇndPf�i*cNumber of planes® °,ÇndKf�iK¾/s6eh° iAirplane typep ¶cà q�Á/rIƯ ¹�g�f�c�s3¿�j»¹�s/g�¿ CONSTRUCTOR¯ b�` c»°�ÇEd�e3¾�d�s�s�¿�` ¸�j�s*i�cDisplays airplane attributes¯ b�` c»°�ÇEd�e3¾*f�¾/g»¾�dK` ¿W°,ÇndKf�i*cDisplays number of airplanes

Object Name Description

/HIW��'LVSOD\LQJ�D�FODVV�LQ�WKH¢¥ªS°=�²±¥ªN³.�²´¥¢�µx��¢X¶[´Xµ �DOORZV�\RXWR�DFFHVV�WKH�FODVV�FRPSRQHQWV

5LJKW��(GLW�DQ�$%$3�SURJUDPLQ�WKH� · ¶��6³2´�§

¸�¹º¸º» · ¶��E³0´�§P¼�½�� ¦¾´�§�³�¿K¶¥�SÀÁ´

*OREDO�&ODVVHV�LQ�WKH�2EMHFW�1DYLJDWRU

Workbench Edit Goto Utilities Environment System Help

Pattern

n You can also display the Class Builder’s global classes in the Object Navigator.

n Being able to view classes through the navigation window on the left is especially useful whenediting source code. You can drag & drop specific components into the right Editor window andautomatically create and insert source code (as a template).

Page 360: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-17

SAP AG 2002

¿»¶�À�Á3 à ¬ur3¶D­1Á dh²Ã�È m�¶

Â<ÂÂ0ÂÂ<ÂÂ0ÂÂ<ÂÂ0ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ2ÂÂ<ÂÂ<ÂÂ<ÂÂ2ÂÂ<ÂÂ<ÂÂ<ÂÂ2ÂÂ<ÂÂ<ÂÂÂ�à e�¾�¼¼è¾3±�È Â.À3Å ±Ea�¶Â<ÂÂ0ÂÂ<ÂÂ0ÂÂ<ÂÂ0ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ0ÂÂ0ÂÂ<ÂÂ<ÂÂ2ÂÂ<ÂÂ<ÂÂ<ÂÂ2ÂÂ<ÂÂ<ÂÂ<ÂÂ2ÂÂ<ÂÂ<ÂÂÂ�à e�¾�¼¼è¾3±�È Â.À3Å ±Ea�¶¹hÇ6dPc*c�Å ²IÅ ¾3±�È Â.À3Å ±Da�¶�b�i�ÄD` f�` s*` g�f�Åc�s6d�¿�s�v g�Ä/v c�i�Ç�i3¹�s*` g�f�ÅÆhÆ6ÆnÆnÆ6ÆnÆnÆhÆ6ÆnÆnÆ6ÆnÆnÆhÆ6ÆnÆnÆ6Æ

&5($7(�2%-(&7�UBDLUSODQH��������(;325,1*������������,03257,1*����

�������=�¡ £¢¥¤K¦{§��¡¨¥���=§©¥§3ª�«�¬=¨­�®�¯¬�¨�ª�«¥¬U¨

¹�Å ±cÆÆnk ` a�à ¶I z ±�²¶·8¹hÇ�¾DdK` ¿»°,Çnd�f�i�¾3¼¼

MIME Repository

Repository BrowserRepository Information SystemTag Library

Transport Organizer

·�¹�Ç�¾EdP` ¿W°,ÇndPf�i�¾�¼¼Class Airplane edWà à Â�È É/o�à ¶cÆ® f�d$p�iName of airplane® f�¾/g�¾Ed�` ¿W°,ÇndPf�i*cNumber of planes® °,ÇndKf�iK¾/s6eh° iAirplane typep ¶cà q�Á/rIƯ ¹�g�f�c�s3¿�j»¹�s/g�¿ CONSTRUCTOR¯ b�` c»°�ÇEd�e3¾�d�s�s�¿�` ¸�j�s*i�cDisplays airplane attributes¯ b�` c»°�ÇEd�e3¾*f�¾/g»¾�dK` ¿W°,ÇndKf�i*cDisplays number of airplanes

Object Name Description

6HOHFWLQJ�DQG�GUDJJLQJ�LQWRWKH�(GLWRU�FUHDWHV&5($7(�2%-(&7

¸º¹º¸U» · ¶¥�E³=´­§P¼�½���¦R´¥§�³�¿�¶¥�SÀÁ´

&ODVV�%XLOGHU��&5($7(�2%-(&7

Workbench Edit Goto Utilities Environment System Help

Pattern

n 'UDJ��GURS the class name: Instantiates an object.

n 6HOHFWLQJ�DQG�GUDJJLQJ a class name WR�WKH�ULJKW automatically creates a CREATE OBJECTstatement with the whole interface of the constructor. You still have to add the call’s actualparameters.

Page 361: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-18

SAP AG 2002

ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇEÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇEÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇǾÈ/ÉhÊ�ËDËÌÊ�Í/Î/ÏuÐ ÑDÍEÒ/ÓÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇEÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇEÇDÇ/ÇDÇ/ÇDÇDÇ/ÇDÇ/ÇDÇDÇ/ÇǾÈ/ÉhÊ�ËDËÌÊ�Í/Î/ÏuÐ ÑDÍEÒ/ÓÔ ÑEÍ*ÕDÕÖÑ Ô ÑÖÍ/ÎDÏuÐ�ÑEÍDÒ/ÓÖ×/Ó�ØDÎEÒ�Î/ÙDÎEÚDÒÛEÜÞÝ ß*Ü àuá/â�à6Ûnã/ä6ã*å/Ü*æhánç�èéDéDé/éDé/éDéDé/éDé/éDéEé/éDéDé/éDé/éDéDé/éDé/éDéDé/éDé/éDéDé/éEé

�������=�¡ £¢¥¤K¦{§��¡¨¥���=§©¥§3ª�«�¬=¨­�®�¯¬�¨�ª�«¥¬U¨

¹�Å ±cÆÆnk ` a�à ¶I z ±�²¶·8¹hÇ�¾DdK` ¿»°,Çnd�f�i�¾3¼¼

¿»¶�À�Á3 à ¬ur3¶D­1Á dh²Ã�È m�¶MIME Repository

Repository BrowserRepository Information SystemTag Library

Transport Organizer

·8¹hÇ�¾Ed�` ¿»°,Çnd�f�i�¾3¼¼ ¹�Å ±´Æ�Æ�d È Â�ÀIÅ ±EaI¶ ¶dhà Ã.Â�È É/o�à ¶cÆ® f�d�p$iName of airplane® f�¾/g»¾DdK` ¿»°,Çnd�f�i�cNumber of planes® °,Çnd�f�iU¾/sEeW° iAirplane typep ¶Ãq�Á/r�Ư ¹�g�f�c�s�¿�j»¹�s�g ¿CONSTRUCTOR¯ b�` c»°8Ç6d�e�¾�dPs/s�¿�` ¸�j�s i�cDisplays airplane attributes¯ b�` c»°8Ç6d�e�¾ f3¾�g»¾�dP` ¿W°,ÇndPf�i*cDisplays number of airplanes

Object Name Description

&$//�0(7+2'[[[�!GLVSOD\BDWWULEXWHV

6HOHFWLQJ�DQG�GUDJJLQJ�LQWRWKH�(GLWRU�FUHDWHV�&$//0(7+2'

¸�¹º¸º» · ¶��E³0´�§P¼�½�� ¦¾´�§�³�¿K¶¥�SÀÁ´

&ODVV�%XLOGHU��&$//�0(7+2'

Workbench Edit Goto Utilities Environment System Help

Pattern

n 'UDJ��GURS the method name: Calls a method

n 6HOHFWLQJ�DQG�GUDJJLQJ a method WR�WKH�ULJKW automatically creates a CALL METHOD statement withthe whole interface of the method. In this case you must also add the call’s actual parameters and setthe call’s reference variable.

Page 362: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-19

SAP AG 2002

&ODVV�%XLOGHU&ODVV�%XLOGHU

:RUNLQJ�ZLWK�JOREDO�FODVVHV�DQG�LQWHUIDFHV:RUNLQJ�ZLWK�JOREDO�FODVVHV�DQG�LQWHUIDFHV

$SSOLHG�H[DPSOH$SSOLHG�H[DPSOH

*OREDO�&ODVVHV�DQG�,QWHUIDFHV����

Page 363: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-20

SAP AG 2002

��

��

$$$$$$$$/+/+/+/+/+

����

���

���

������

���

������������������������������������������������������������������������������������������

86'86'86'86''(0'(0'(0'(0'(0

������

��������������

������������������

��������������

�������

$���

�������

$�������

$���$���

$�������$�������

$���

�������

7RROEDU

ê ´�§�³ ë¥��ÄE³¯�U§ » §���¢R³ ������¦{Ä�ª�ìí°�ª�§3��ªU¢¾³���M³ñª��¯Ä�� ë¥��¢X¶ î�´R³ñª�Ä �U´Xµx¢�IJ´­ª#¶

6$3�*ULG�&RQWURO

n An applied example for the use of ABAP Objects is the standard output tool 6$3�*ULG�&RQWURO. It isimplemented in ABAP Objects and provides the user with a wide range of functions (for examplesorting data, reorganizing/hiding columns, totalling).

n Principle of 6$3�&RQWURO�)UDPHZRUN:To use such a control the developer no longer has to re-implement functions in the control, butsimply specify the instances of the classes that contain these functions.

n The exact procedure for using the SAP Grid Control will be discussed in the following.

Page 364: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-21

SAP AG 2002

6FUHHQ$UHD

&XVWRP&RQWDLQHU&RQWURO

$/9�*ULG&RQWURO

ï � öE P� % � � % � ü �¥äS � �¥ò ü ì�� ò�� ûPrivatePublic

ð � ö/ �� % � »ì ö8ùP �� û �<�PrivatePublic

<RX�UHTXLUH�WZR�FODVVHV�IRUXVLQJ�D�6$3�*ULG�&RQWURO

,QFOXGLQJ�D�*ULG�&RQWURO�,QVWDQFH�LQ�D�'LDORJ�3URJUDP

n To implement a SAP Grid Control, you must implement:

é An instance of the container control

é An instance of the displaying SAP Grid Control

é The data retrieval

é The display of data using a method call

n 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 andprovides a physical area for visualization. Each control "lives" in a container. Because containers arethemselves controls, containers can be nested within one another. The container becomes the SDUHQWof its control.

n 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,' iscreated. These objects contain all necessary information for accessing the controls. For informationon the object types (classes) and the corresponding methods, refer to the online documentation.

Page 365: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-22

SAP AG 2002

...

'$7$��FRQWDLQHUBU�7<3(�5()�72�FOBJXLBFXVWRPBFRQWDLQHU�������JULGBU������7<3(�5()�72�FOBJXLBDOYBJULG��...

&5($7(�2%-(&7�FRQWDLQHUBU����(;3257,1*�FRQWDLQHUBQDPH� �&217$,1(5B�...

&5($7(�2%-(&7�JULGBU����(;3257,1*�LBSDUHQW� �FRQWDLQHUBU...

JULGBU�!VHWBWDEOHBIRUBILUVWBGLVSOD\�������������LBVWUXFWXUHBQDPH� �6$3/$1(������������LWBRXWWDE�������� �LWDEBVDSODQH�...

ñ ²IÅ ¾3µ*o�È ¾3²Eo�Æ�à Á ­�Å Å ÅPrivatePublic

ñ ²�Å ¾�µ*o�È ¾I±IÅ mc¾�µ8Â.È rPrivatePublic

ò óô õ

ö öö öö öö ö÷ ø÷ ø÷ ø÷ ø÷ ø

ò óò óõ ù ùõ ù úõ ù ùõ ù ùõ ù ú

ú ù ù ù û ù ò û ò óú ù ù ù û ù ú û ú ùú ù ù ù û ù ü û ò òú ù ù ù û ù ý û ò þú ù ù ù û ù ò û ò üú ù ù ù û ù ú û ú ôú ù ù ù û ù ü û ú òú ù ù ù û ù ü û ù õú ù ù ù û ù ý û ú ÿ

� � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

ý ò ü � ô þ

ò ú ü õ � ý ôò ú ü õ � ý ô

ý ò ü � ô þý ò ü � ô þü ô þ � ù ùò ú ü õ � ý ôò ú ü õ � ý ôò ú ü õ � ý ôö ü ú òó õ ó û õ ù ùö ü ò ù û ü ù ùö ü ò þö ü ò þö ü ò ù û ü ù ùö ü ò ù û ü ù ùö ü ú òó õ ó û õ ù ù

alv_grid_control

'LVSOD\LQJ�'DWD�ZLWK�D�*ULG�&RQWURO�,QVWDQFH

n The control is displayed on a screen, the call of which is not shown in this example.

n To create the object that communicates with the container control, it is sufficient to include the nameof the container area &217$,1(5B��on the screen, on which the container area is defined.

n To create the object that communicates with the SAP Grid Control, you must include the referencevariable that refers to the object for the custom container. This is because the object for the SAP GridControl contains a reference variable that points to the container object as a private attribute. Thisrelationship tells the object, which container it should be included in.

n To display data in a SAP Grid Control, it must be provided in an internal table. Then a method iscalled that receives the content of the internal table and its structure. The name of this method idVHWBWDEOHBIRUBILUVWBGLVSOD\. If, while the program runs, only the content of the internaltable to be displayed changes, it is sufficient to call the method UHIUHVKBWDEOHBGLVSOD\ withthe container area before the the screen is re-sent.

Page 366: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-23

SAP AG 2002

l 'HVFULEH�WKH�GLIIHUHQFH�EHWZHHQ�ORFDO�DQG�JOREDOFODVVHV�DQG�LQWHUIDFHV

l &UHDWH�JOREDO�FODVVHV�DQG�LQWHUIDFHV�XVLQJ�WKH&ODVV�%XLOGHU

<RX�DUH�QRZ�DEOH�WR�

*OREDO�&ODVVHV�DQG�,QWHUIDFHV��8QLW�6XPPDU\

Page 367: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-24

([HUFLVHV8QLW��*OREDO�&ODVVHV�DQG�,QWHUIDFHV7RSLF��&UHDWLQJ�*OREDO�&ODVVHV

At the conclusion of these exercises, you will be able to:

• Use the Class Builder to process global classes and interfaces

A travel agency communicates with its business partners. Various hotelsare now also to be business partners.

0RGHO�VROXWLRQ� &/B+27(/&/B+286(,)B3$571(56

1-1 Create the global class =&/B��B+27(/ in the Class Builder.

1-2 The class is to have the following�attributes:QDPH type STRING, private instance attributePD[BEHGV type I, private instance attributeQBRBKRWHO type I, private static attribute

1-3 The class is to have the following�methods:FRQVWUXFWRU with import parameters LPBQDPH and LPBEHGVGLVSOD\BDWWULEXWHV for displaying the instance attributes of the classGLVSOD\BQBRBKRWHOV static method for displaying instances created

1-4 Implement these methods.

1-5 Activate and test your class using the Class Builder test tool.

Page 368: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-25

2 Define the global interface =,)B��B3$571(56 with the interface methodGLVSOD\BSDUWQHU.Implement the interface in the class =&/B��B+27(/.

3 2SWLRQDO�Include the new class in the program with the airlines and car rental companies. Adjust thesections that refer to the local interface OLIBSDUWQHUV to suit the new global interface.

4 �RSWLRQDO�Define an inheritance relationship in the Class Builder. Your hotel is to inherit from thesuperclass =&/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 houseshould only have an attribute QDPH (protected) and the method GLVSOD\BDWWULEXWHV; delete allsuperfluous components.Finally, define the inheritance relationship between the house and the hotel.

OFOBFDUULHU

OFOBDLUSODQH OFOBYHKLFOHV

OFOBFDUBUHQWDO

��� ������ ���

OFOBKRWHO

]LIBSDUWQHUVLPSOHPHQWV

OFOBWUDYHOBDJHQF\XVHV

Page 369: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-26

6ROXWLRQV8QLW� *OREDO�&ODVVHV�DQG�,QWHUIDFHV7RSLF� &UHDWLQJ�*OREDO�&ODVVHV

*&---------------------------------------------------------------------*

*& Report SAPBC401_CLSS_MAIN_A *

*&---------------------------------------------------------------------*

*& Global Class lcl_hotel is also a business partner of the *

*& travel agency *

*&---------------------------------------------------------------------*

REPORT sapbc401_clss_main_a.

TYPES: ty_fuel TYPE p DECIMALS 2,

ty_cargo TYPE p DECIMALS 2.

INCLUDE <icon>.

include sapbc401_vehd_j.

INCLUDE sapbc401_clss_a.

DATA: r_vehicle TYPE REF TO lcl_vehicle,

r_truck TYPE REF TO lcl_truck,

r_bus TYPE REF TO lcl_bus,

r_passenger type ref to lcl_passenger_plane,

r_cargo type ref to lcl_cargo_plane,

r_carrier type ref to lcl_carrier,

r_rental type ref to lcl_rental,

r_agency type ref to lcl_travel_agency,

������UBKRWHO�W\SH�UHI�WR�FOBKRWHO�

START-OF-SELECTION.

*########################

******* create travel_agency *****************************************

CREATE OBJECT r_agency EXPORTING im_name = ’Fly&Smile Travel’.

******* create rental *****************************************

CREATE OBJECT r_rental EXPORTING im_name = ’HAPPY CAR RENTAL’.

Page 370: 003 - Bc401 - Abap Objects

© SAP AG BC401 12-27

******* create truck *****************************************

CREATE OBJECT r_truck EXPORTING im_make = ’MAN’

im_cargo = 45.

***** Create CARRIER ********************************************

create object r_carrier exporting im_name = ’Smile&Fly-Travel’.

***** Passenger Plane ********************************************

CREATE OBJECT r_passenger EXPORTING

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.

******* create hotel *****************************************

FUHDWH�REMHFW�UBKRWHO�H[SRUWLQJ�LPBQDPH� �+2/,'$<�,11��������������������������������LPBEHGV� �����

******* show attributes of all partners of travel_agency ******

r_agency->display_agency_partners( ).

Page 371: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-1

SAP AG 2002

l $EVWUDFW�DQG�ILQDO�FODVVHVl 9LVLELOLW\�RI�FRQVWUXFWRUVl )ULHQGVl 2EMHFW�6HUYLFHV

&RQWHQWV�

6SHFLDO�7HFKQLTXHV

Page 372: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-2

SAP AG 2002

l &UHDWH�DQG�GHVFULEH�DEVWUDFW�DQG�ILQDO�FODVVHVl ([SODLQ�WKH�YLVLELOLW\�FULWHULD�RI�FRQVWUXFWRUVl 'HVFULEH�WKH�IULHQG�FRQFHSWl 'HVFULEH�WKH�SHUVLVWHQFH�VHUYLFH�RI�2EMHFW

6HUYLFHV

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

6SHFLDO�7HFKQLTXHV��8QLW�2EMHFWLYHV

Page 373: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-3

SAP AG 2002

6SHFLDO�7HFKQLTXHV��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �� ��� ��� ����� � � � � ����� � � � � ! � � � � � � �#" � � � � ��$ $ � � % &�' ' ( )+*-,/.�0 * . &�132& )54 , & 2 2 ( ) *-6 &�7 &8�9�: ;+<#= ;+> ?�@ ;�A�BC A�D B ? 9E A > A C�F�G ? 9 A ;�HE A > AJI DLK ?�M > 9 : ;E ? > A : B N/O�P�Q�R�SUTWV S Q V�X S�Y

Z�[�\ ]�^ \ _+` ab3c5d5egfih a ^�j k e [�\ a ] j a�l Z [ c�m [ n�o o \ ] mp ]+q a [r\ j n ] ^ as n b j \ ] mp ] j a [ d n ^ a bt/u a ] j bv ` c f n ` s ` n b b a b n ] l p ] j a [ d n�^ abwyx{zU|~} ���#� z |~�y� } ��� z#�wyx{z | } ���#� z | �y� } ��� z#�

Page 374: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-4

SAP AG 2002

$EVWUDFW�DQG�ILQDO�FODVVHV$EVWUDFW�DQG�ILQDO�FODVVHV

9LVLELOLW\�RI�FRQVWUXFWRUV9LVLELOLW\�RI�FRQVWUXFWRUV

7KH�IULHQG�FRQFHSW�7KH�IULHQG�FRQFHSW�

3HUVLVWHQW�REMHFWV3HUVLVWHQW�REMHFWV

6SHFLDO�7HFKQLTXHV����

Page 375: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-5

SAP AG 2002

&/$66�OFOBYHKLFOH�'(),1,7,21�$%675$&7��38%/,&�6(&7,21���0(7+2'6�HVWLPDWHBIXHOBFRQVXPSWLRQ�$%675$&7�����������������������������,03257,1*����(1'&/$66�

l $EVWUDFW�FODVVHV�WKHPVHOYHV�FDQQRW�EH�LQVWDQWLDWHG��DOWKRXJKWKHLU�VXEFODVVHV�FDQ�

� 5HIHUHQFHV�WR�DEVWUDFW�FODVVHV�FDQ�UHIHU�WR�LQVWDQFHV�RI�VXEFODVVHVl $EVWUDFW��LQVWDQFH��PHWKRGV�DUH�GHILQHG�LQ�WKH�FODVV��EXW�QRW

LPSOHPHQWHG� 7KH\�PXVW�EH�UHGHILQHG�LQ�VXEFODVVHV

&ODVV�FDQQRWEH�LQVWDQWLDWHG

0HWKRG�QRWLPSOHPHQWHG�LQ�WKLVFODVV

$EVWUDFW�&ODVVHV

n It is not possible to instantiate objects of an abstract class However, this does not mean thatreferences to such a class are not useful. On the contrary, they are very useful, since they can (andmust) refer to instances in subclasses of the abstract class at runtime. The CREATE OBJECTstatement is extended in this context. You can specify the class of the instance to be createdexplicitly:CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>.

n Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract)subclasses, for example to define a uniform interface.

n Abstract instance methods are used to specify particular interfaces for subclasses, without having toimmediately provide implementation for them. Abstract methods need to be redefined and therebyimplemented in the subclass (here you also need to include the corresponding redefinition statementin the DEFINITION part of the subclass).

n Classes with at least one abstract method are themselves abstract.

n Static methods and constructors cannot be abstract (they cannot be redefined).

Page 376: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-6

SAP AG 2002

&/$66�OFOBWUXFN�'(),1,7,21�),1$/���,1+(5,7,1*�)520�OFOBYHKLFOH�

������(1'&/$66�

&/$66�OFOBEXV�'(),1,7,21�,1+(5,7,1*�)520�OFOBYHKLFOH���38%/,&�6(&7,21�����0(7+2'6�HVWLPDWHBQXPEHUBRIBIUHHBVHDWV�),1$/�(1'&/$66�

l )LQDO�PHWKRGV�FDQQRW�EH�UHGHILQHG�LQ�VXEFODVVHV

l )LQDO�FODVVHV�FDQQRW�KDYH�VXEFODVVHV

�����g� ��� �

�i�i� ��� � ���

... ......

)LQDO�&ODVVHV

n A final class cannot have subclasses, and can protect itself in this way against (uncontrolled)specialization.

n A final method in a class cannot be redefined in a subclass, and can protect itself in this way against(uncontrolled) redefinition.

n Some features:

• A final class implicitly only contains final methods. In this case, you cannot enter FINALexplicitly for these methods.

• Methods cannot be both final and abstract.

• Classes, on the other hand, that are both abstract and final can be useful: Only static componentscan be used.

Page 377: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-7

SAP AG 2002

$EVWUDFW�DQG�ILQDO�FODVVHV$EVWUDFW�DQG�ILQDO�FODVVHV

9LVLELOLW\�RI�FRQVWUXFWRUV9LVLELOLW\�RI�FRQVWUXFWRUV

7KH�IULHQG�FRQFHSW�7KH�IULHQG�FRQFHSW�

3HUVLVWHQW�REMHFWV3HUVLVWHQW�REMHFWV

6SHFLDO�7HFKQLTXHV����

Page 378: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-8

SAP AG 2002

l 7KH�XVDELOLW\�RI�FODVVHV�FDQ�EH�UHVWULFWHG�&/$66�FOBFODVV�'(),1,7,21����

� &5($7(�38%/,&(YHU\�XVHU��FOLHQW��FDQ�FUHDWH�LQVWDQFHV�RI�D�FODVV�GHIDXOW�VHWWLQJ�

� &5($7(�3527(&7('2QO\�WKH�FODVV�LWVHOI�DQG�DOO�LWV�VXEFODVVHV�FDQ�FUHDWH�LQVWDQFHV�RI�WKLVFODVV

� &5($7(�35,9$7(2QO\�WKH�FODVV�FDQ�FUHDWH�LQVWDQFHV�RI�LWVHOI

l 7KLV�LPSOLFLWO\�FRQWUROV�WKH�YLVLELOLW\�RI�WKHFODVV

l 3RVVLEOH�XVH��������¡ 5¢�£�¤{� �FRQFHSW

��� OFOBVLQJOHWRQPrivatePublic

¥§¦ ¨�© ªg«�¬­ � � ®� � ¯ ­ � � �L° ¯

±~²L³L´�µ{¶

:KR�&DQ�,QVWDQWLDWH�&ODVVHV"

n If you want to ensure that not every user (client) can instantiate a class, you can use the followingadditions to restrict the visibility area of the constructor and hence limit the use of the class. (Thefollowing additions must be preceded by CLASS ... DEFINITION.)

• The CREATE PUBLIC addition is implicitly available for every class definition, providedneither of the other two CREATE additions is used. It defines the default that HYHU\ user cancreate instances of a known class.

• 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 CREATEPROTECTED, only VXEFODVVHV of the class or the class itself and, in the case of CREATEPRIVATE, RQO\�WKH�FODVV�LWVHOI can create instances of the class.

• Therefore, the additions CREATE PROTECTED and CREATE PRIVATE allow you to controlinstance creation and, for example, are a prerequisite for the instance management of persistentobjects, for which the uniqueness of objects must be ensured. (Persistent objects will bediscussed in more detail later.)

n If it is to be impossible to instantiate a class more than once (for example, because it serves as a dataadministrator or data container), you can use the singleton concept. The class is defined with theaddition 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.

Page 379: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-9

SAP AG 2002

$EVWUDFW�DQG�ILQDO�FODVVHV$EVWUDFW�DQG�ILQDO�FODVVHV

9LVLELOLW\�RI�FRQVWUXFWRUV9LVLELOLW\�RI�FRQVWUXFWRUV

7KH�IULHQG�FRQFHSW�7KH�IULHQG�FRQFHSW�

3HUVLVWHQW�REMHFWV3HUVLVWHQW�REMHFWV

6SHFLDO�7HFKQLTXHV����

Page 380: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-10

SAP AG 2002

l ,Q�UDUH�FDVHV��FODVVHV�KDYH�WR�ZRUN�WRJHWKHU�FORVHO\�DQG�PDNH�DOOWKHLU�FRPSRQHQWV��LQFOXGLQJ�WKH�SURWHFWHG�DQG�SULYDWH�RQHV�DYDLODEOH�WR�HDFK�RWKHU

· (IILFLHQW�GLUHFW�DFFHVV�WR�WKH�GDWD�RI�D�FODVV�SURYLGLQJIULHQGVKLS

· 0HWKRGV�WKDW�DFFHVV�WKH�VDPH�GDWD�FDQ�WKHUHIRUH�EH�VSUHDG�RYHUVHYHUDO�FODVVHV

· 3DFNDJH�FUHDWLRQ�VXSSRUW

¸#¹3º�»½¼¿¾/À�¹½ºUÁ½¾ÂiÃ5Ä�ŠƽŠǽÈÅrÉ Ê-ËÍÌÎÇJÏJÐgÌiÐ

Ñ Ò�Ó�ŠƽŠǽÈÅrÉ Ê-Ë�ÌÎǽÈiÐÍÆgÌ�Ô-ÕiÖ×UØÍÙUÚ ¾ Ù À{¸U¹Uºg»J¼�¾» Ù ¾�Ày¸#¹½º�»½¼¿¾Û Ù ¹ Ù ¾ Ù À{¸U¹Uºg»J¼�¾

ÜÞÝ#ß�àUÝ�á ´

ârâ�â

ã{ä ³L´�µyåãæä ³L´�µ{åÜÞä�ç{è ³ åæ´-éëê ä ³L´#µæåyé-ìæ³Lí

)ULHQGV

n In rare cases, classes have to work together so closely that they need access to their protected andprivate components. To avoid making these components available to all users, there is the concept offriendship between classes.

n A class can provide friendship to other classes and interfaces (and hence all classes that implementthe interface). To do this you use the )5,(1'6�additions to the CLASS statement, in which allclasses and interfaces that are to be provided friendship are listed. Friends are allowed to access theprotected and private components of the class providing the friendship and can always createinstances of this class, regardless of the CREATE addition to the CLASS statement.

n In principle, providing friendship is one-sided: A class providing friendship is not automatically afriend of its friends. If a class providing friendship wants to access the non-public components of afriend, this friend has to explicitly provide friendship to it.

n Classes that inherit from friends and interfaces that contain a friend as a component interface alsobecome friends. Therefore, extreme caution is advised when providing friendship. The higher up afriend is in the inheritance tree, the more subclasses can access all components of a class providingfriendship. However, providing friendship, unlike the attribute of being a friend, is not inherited. Afriend of a superclass is therefore not automatically a friend of its subclasses.

Page 381: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-11

SAP AG 2002

$EVWUDFW�DQG�ILQDO�FODVVHV$EVWUDFW�DQG�ILQDO�FODVVHV

9LVLELOLW\�RI�FRQVWUXFWRUV9LVLELOLW\�RI�FRQVWUXFWRUV

7KH�IULHQG�FRQFHSW�7KH�IULHQG�FRQFHSW�

3HUVLVWHQW�REMHFWV3HUVLVWHQW�REMHFWV

6SHFLDO�7HFKQLTXHV����

Page 382: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-12

SAP AG 2002

$%$3�SURJUDP

3HUVLVWHQFH�VHUYLFH

'DWDEDVH

î ä5Ý µæéU³L´-µæ¶ ç{ïWð ´ ß ¶�é

Ü ´ ä é�³ é-¶�´#µy¶~å Ý ¶ Ý ³ µ¶ Ý-ï ²L´�é

ñ ´ Ý åUò�ó ä ³L¶�´ ç{ïWð ´ ß ¶é

3HUVLVWHQFH�6HUYLFH

n The persistence service helps the programmer to work object-oriented with data in relationaldatabases.

n In principle, ABAP programs work with data and objects that exist(s) in the internal session atruntime. They are transient when the program is stopped. If this data is to be stored program-independently, that is persistently, it must be stored in the database. (You could also use files onoperating system level.)

n In ABAP Objects, this is done using the OPEN SQL interface.To be able to work with ABAP Objects persistently, the SHUVLVWHQFH�VHUYLFH was introduced withSAP R/3 Basis Release 6.10. Objects and their attributes can be written to the database and re-imported.

Page 383: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-13

SAP AG 2002

l 2EMHFWV�RI�D�UXQQLQJ�$%$3�SURJUDP�DUH� £õô�ö��¡÷æ��¢U�ø£

l 7KH�SHUVLVWHQFH�VHUYLFH�DOORZV�\RX�WR�ZRUN�ZLWK�ù ¢Uô�÷æ�ú÷¡£+¢U�ø£ REMHFWVl 3HUVLVWHQW�FODVVHV�DUH�FUHDWHG

LQ�WKH�&ODVV�%XLOGHU

l 3HUVLVWHQFH�VHUYLFH�WDVNV· /RDGLQJ�WKH�REMHFWV�IURP�WKH�GDWDEDVH· 0DQDJLQJ�FKDQJHV�WR�WKH�REMHFWV· 6WRULQJ�WKH�REMHFWV�LQ�WKH�GDWDEDVH

Normal ABAP Class

&UHDWH�&ODVV�&/B3(56,67(17B&/$66

Exception Class

3HUVLVWHQW�&ODVV ...

Class Type

)HDWXUHV�RI�WKH�3HUVLVWHQFH�6HUYLFH

n To use the persistence service for objects, their classes must be created as so-called SHUVLVWHQWFODVVHV�in the Class Builder.

n The term SHUVLVWHQW�FODVV indicates that the objects of the class and their state are managed by thepersistence service. In ABAP programs, objects of these classes are, for example, not created usingthe normal statement CREATE OBJECT, but instead using a method of the persistence service thatensures the correct initialization.

n When creating a persistent class, the Class Builder automatically creates a corresponding class, theso called FODVV�DFWRU�or DJHQW, the methods of which are used to manage the objects of the persistentclass.

n As well as their unique identity, persistent classes can also contain so-called key attributes, which thepersistence service uses to ensure the uniqueness of the persistent objects’ contents.

Page 384: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-14

SAP AG 2002

...

'$7$��UBFDUULHU�7<3(�5()�72�FOBFDUULHU�������UBDJHQW���7<3(�5()�72�FDBFDUULHU��������FDUUQDPH��7<3(��������VBFDUUQDPH�UBDJHQW� �FDBFDUULHU !DJHQW�75<���UBFDUULHU�� �UBDJHQW�!JHWBSHUVLVWHQW��LBFDUULG� �/+�����FDUUQDPH��� �UBFDUULHU�!JHWBFDUUQDPH������:5,7(��/+����FDUUQDPH���&$7&+�F[BRVBREMHFWBQRWBIRXQG�(1'75<�

l :LWKLQ�WKH�SHUVLVWHQFH�VHUYLFH��WKH�FODVV�DFWRU�RU� û  �ö�÷�÷üöæ�ø¢#�ý£ �PDQDJHVWKH�SHUVLVWHQW�REMHFWV

· 7KH�DJHQW�SURYLGHV�D�UDQJH�RI�VHUYLFHV��PHWKRGV���ZKLFK�DUH�XVHG�WRPDQDJH�WKH�REMHFWV�DQG�WKH�HQFDSVXODWHG�GDWD

· 7HFKQLFDOO\��WKH�DJHQW�LV�D�VLQJOHWRQ��ZKLFK�LV�DGGUHVVHG�XVLQJ�WKH�SXEOLFVWDWLF�DWWULEXWH�$*(17

7KH�DJHQW�D�VLQJOHWRQ�RI�WKH�FODVV&$B&$55,(5�DQGIULHQG�RI�WKH�SHUVLVWHQWFODVV�&/B&$55,(5

&ODVV�$JHQW

n For every persistent class cl_persistent, the Class Builder generates two further classesca_persistent and cb_persistent. These classes make up the class-specific part of thepersistence service.

n ca_persistent is the so-called FODVV�DFWRU�(or agent), which is used to manage the managedobject of the class cl_persistent, and in which all actual database accesses take place. Theclass actor inherits the relevant methods from the abstract superclass cb_persistent. Theprogrammer 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 managedclass. It has the attribute CREATE PRIVATE and exactly one instance of the class actor is createdwhen it is first accessed.

n 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 classca_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 thepersistent class. For each program there is only one object of the class ca_persistent, becauseyou cannot create objects from outside using CREATE OBJECT.

n The class actor manages one or more objects of the relevant persistent class. These objects must havedifferent keys.

Page 385: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-15

SAP AG 2002

l &UHDWH�DQG�GHVFULEH�DEVWUDFW�DQG�ILQDO�FODVVHVl ([SODLQ�WKH�YLVLELOLW\�FULWHULD�RI�FRQVWUXFWRUVl 'HVFULEH�WKH�IULHQG�FRQFHSWl 'HVFULEH�WKH�SHUVLVWHQFH�VHUYLFH�RI�2EMHFW

6HUYLFHV

<RX�DUH�QRZ�DEOH�WR�

6SHFLDO�7HFKQLTXHV��8QLW�6XPPDU\

Page 386: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-16

([HUFLVHV���RSWLRQDO8QLW��6SHFLDO�7HFKQLTXHV7RSLF��6LQJOHWRQ�&ODVVHV

At the conclusion of these exercises, you will be able to:

• Create a singleton; requirement for next exercise

0RGHO�VROXWLRQ� &/B6,1*/(7216$3%&���B63&6B0$,1B$

1 �$GYDQFHG�Create the global class =&/B��B6,1*/(721 in the Class Builder.The following must be specified for the class:

1-1 You must be able to be instantiate the class only once. (Tip: Instantiation shouldtake place automatically when the class is first accessed.)

1-2 Instantiation only takes place within the class.

1-3 The class has a static reference variable UBVLQJOHWRQ that refers to the instantiatedobject.

1-4 The class has a static reference variable UBVLQJOHWRQ that refers to the instantiatedobject.

2 In the main program =%&���B��B0$,1B63(&,$/, instantiate the singleton class=&/B��B6,1*/(721 by calling the JHWBVLQJOHWRQ method.

2-1 Why must the JHWBVLQJOHWRQ method in this example be static?

2-2 Verify your blueprint by calling JHWBVLQJOHWRQ several times. (Debug.) When doesinstantiation take place and how often?

Page 387: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-17

([HUFLVHV���RSWLRQDO8QLW��6SHFLDO�7HFKQLTXHV7RSLF��&RQFOXGLQJ�3URMHFW�([HUFLVH

At the conclusion of these exercises, you will be able to:

• Create a “friends” relationship and access the data of a class providingthe friendship (a singleton) from the befriended class

(Note that the friends concept is mainly used in bigger projects withmore complex classes)

0RGHO�VROXWLRQ� &/B$*(1&<6$3%&���B63&6B0$,1B%

3 �$GYDQFHG�Use your singleton and the main program from the last exercise.

3-1 Add a static attribute FRQQHFWLRQBOLVW (with type TY_CONNECTIONS, that is anitab) to the singleton class.

3-2 When instantiating the singleton, this internal table is to be automatically filled withthe flight connections from the table SPFLI.

4 Create a global class =&/B��B$*(1&<, which is to be provided friendship by thesingleton.

4-1 Define and implement a constructor for =&/B��B$*(1&<.In the constructor, the private attribute of the class QDPH is to be initialized.

4-2 This class is to have a public method JHW�FRQQHFWLRQ.

4-2-1 Import parameter:LPBFDUULG and LPBFRQQLG (types: S_CARR_ID and S_CONN:ID)

4-2-2 Export parameter:A structure with line type SPFLIName the export parameter H[BFRQQHFWLRQ.

Page 388: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-18

4-2-3 In the method, the internal table of the class providing the friendship is to beaccessed in a single record access. (READ TABLE...)If the requested record does not exist, it is sufficient in this example todisplay an appropriate message using the WRITE statement.

5 Go back to the singleton class.Here, provide friendship to the class =&/B��B$*(1&<.

6 In your main program, instantiate the class =&/B��B$*(1&<.The singleton should have been instantiated in the last exercise.There, the internal table with the flight connections is already filled.

6-1-1 Execute the JHWBFRQQHFWLRQ method of =&/B��B$*(1&< requesting a flightconnection that exists in the SPFLI table.(for example ‚LH‘ / ‚0400‘)

FRQQHFWLRQBOLVW

����FOBVLQJOHWRQ����OFOBDJHQF\

JHWBFRQQHFWLRQ

IULHQG

Page 389: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-19

6ROXWLRQV���RSWLRQDO8QLW� 6SHFLDO�7HFKQLTXHV7RSLF� 6LQJOHWRQ�&ODVVHV

*&---------------------------------------------------------------------*

*& Report SAPBC401_spcS_MAIN_A *

*&---------------------------------------------------------------------*

*& Work with singleton:

*& The singleton class consists of a static pointer r_singleton

*& pointing to the class itself; the class is final and has

*& the attribute „create private“.

*& in the class-constructor the class is instantiated via

*& this reference. The static method get_singleton passes the

*& reference to the outside world so that the singleton can be used!

*&---------------------------------------------------------------------*

REPORT sapbc401_spcs_main_a.

'$7$��UBVLQJOH�W\SH�UHI�WR�FOBVLQJOHWRQ�

START-OF-SELECTION.

*########################

UBVLQJOH� �FOBVLQJOHWRQ !JHWBVLQJOHWRQ����

Page 390: 003 - Bc401 - Abap Objects

© SAP AG BC401 13-20

6ROXWLRQV���RSWLRQDO8QLW� 6SHFLDO�7HFKQLTXHV7RSLF� &RQFOXGLQJ�3URMHFW�([HUFLVH

*&---------------------------------------------------------------------*

*& Report SAPBC401_spcS_MAIN_B *

*&---------------------------------------------------------------------*

*& Practice with singleton & friends (show package concept )

*&

*& The class cl_agency is a friend of the singleton;

*& cl_agency works directly with the private data of the singleton

*& the method get_connection reads single records out of an

*& internal table of the singleton; so the singleton acts as a kind

*& of data-container. cl_agency uses this data in the singleton

*& directly. (performance aspects, package-concept...)

*&---------------------------------------------------------------------*

REPORT sapbc401_spcs_main_b.

'$7$��UBVLQJOH�W\SH�UHI�WR�FOBVLQJOHWRQ�������UBDJHQF\�W\SH�UHI�WR�FOBDJHQF\�������UHF�W\SH�VSIOL�

START-OF-SELECTION.

*########################

UBVLQJOH� �FOBVLQJOHWRQ !JHWBVLQJOHWRQ����

FUHDWH�REMHFW�UBDJHQF\�H[SRUWLQJ�LPBQDPH� �$JHQF\�

UBDJHQF\�!JHWBFRQQHFWLRQ��H[SRUWLQJ�LPBFDUULG� �/+������������������������������������LPBFRQQLG� �������������������������������LPSRUWLQJ�H[BFRQQHFWLRQ� �UHF���

ZULWH����UHF�FDUULG��UHF�FRQQLG�

Page 391: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-1

SAP AG 2002

l 3UHGHILQHG�H[FHSWLRQV�DQG�H[FHSWLRQV�\RX�GHILQH\RXUVHOI

l 5DLVLQJ��KDQGOLQJ��DQG�SDVVLQJ�DORQJ�H[FHSWLRQV

&RQWHQWV�

([FHSWLRQ�+DQGOLQJ

Page 392: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-2

SAP AG 2002

l &UHDWH�\RXU�RZQ�H[FHSWLRQ�FODVVHVl 5DLVH�H[FHSWLRQV�LQ�D�SURJUDPl +DQGOH�H[FHSWLRQVl 3DVV�H[FHSWLRQV�DORQJ

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

([FHSWLRQ�+DQGOLQJ��8QLW�2EMHFWLYHV

Page 393: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-3

SAP AG 2002

([FHSWLRQ�+DQGOLQJ��,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ��� ��� � � �� ��� ��� ����� � � � � � ����� � � � � � � � ��� � �!

� � � � ��" " � � #�$�% % & '�(*),+�- ( + $�.0/$ '21!) $ / / & '�(*3 $�4 $57698 :�;!< :�= >@? :9A�BC A9D B > 6E A = A C�F@G > 6 A :�HE A = AJI DLK >�M = 6 8 :E > = A 8 B N�O7P9Q R�SUTWV S Q V@X S@Y

Z7[�\ ]�^ \ _2` acb0d2e2fhgji a ^k l f [�\ a ] k a�m Z [ d9n[ o�p p \ ] nq ]2r a [ \ k o ] ^ a

s o b k \ ] nq ] k a [ e o ^ abt7u a ] k b

v ` d g o ` s ` o b b a b o ] m q ] k a [ e o ^a bw _ a ^ \ o `�x a ^ r ] \ y�z ab

{}|!~!�!���,� ������� �U��� � �0�{}|!~ � ���,� ������� �U��� � �0�

Page 394: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-4

SAP AG 2002

&ODVV�%DVHG�([FHSWLRQV��2YHUYLHZ

F[BH[FHSWLRQ� � �

� � �

([FHSWLRQ�FODVV�HLWKHU�SUHGHILQHGRU�GHILQHG�E\�\RX�

����5$,6(�(;&(37,21�7<3(�F[BH[FHSWLRQ����������>(;3257,1*�DWWU�� �������������������������DWWU�� ����@����� �([FHSWLRQ�KDQGOLQJ����

�����������������������¡ ��U�¢�£�

([FHSWLRQ�UDLVHGHLWKHU�LQ�$%$3VWDWHPHQW�RU�E\UXQWLPH�V\VWHP

���¤���¦¥ ��§�¨!��©¡�

n We use the term H[FHSWLRQ to refer to a situation that arises while an ABAP program is beingexecuted, where there is no point in continuing to run the program in the normal way. Since SAP R/3Basis 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 thusreplaces) them.

n Class-based exceptions are raised either using the ABAP statement RAISE EXCEPTION or by theABAP runtime environment. For example, if the program tries to divide by zero the runtimeenvironment raises the exception. You can, however, ascertain that this situation has arisen using aquery in the ABAP program. From there, you can then raise an exception yourself.

n You can define exception classes yourself, but there is already a range of predefined exceptionclasses 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 containinformation about the error situation.

n The use of class-based exceptions is not limited to object-oriented contexts. Class-based exceptionscan be raised and handled in all ABAP processing blocks. In particular, the catchable runtime errorsused until now can be handled as class-based exceptions.

n If a class-based exception occurs, the system interrupts the normal program flow and tries tonavigate to a suitable handler. If it cannot find a handler, a runtime error occurs.

Page 395: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-5

SAP AG 2002

([FHSWLRQ�&ODVVHV��7KH�,QKHULWDQFH�+LHUDUFK\

F[BVWDWLFBFKHFNF[BG\QDPLFBFKHFNF[BQRBFKHFN

F[BV\BPRYHBFDVWBHUURUF[BV\BDULWKPHWLFBHUURU

F[BV\BDULWKPHWLFBRYHUIORZ

ªj«h¬�ªc­ ®¯ «U°±¡«J² ³0«0°�°@­ ®´�´µ´¶ «¡ªµ³·ª¢«W¬�ª¶ «¡ªµ³·¸�¹�ºJ°¼»¡«½³*¾�¹0¸J­ ª@­ ¹*±

F[BURRW

n 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 commonsuperclass CX_ROOT. The way in which exception classes are assigned to one of these three pathsin the hierarchy defines how the associated exceptions are passed along. (This will be discussed inmore detail later in this unit.)

n All exception classes begin with the prefix CX_. In general, they are defined globally in the ABAPWorkbench &ODVV�%XLOGHU. However you can also define local exception classes.

n 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), andline number in the source code where the exception occurred. The GET_TEXT method returns anexception text of a class in the form of a string. You can assign several texts to each class. You canthen specify which text is to used when an exception is raised by passing an identifier to theIMPORTING parameter TEXTID of the instance constructor.

n All exception classes inherit the KERNEL_ERRID attribute from CX_ROOT. This attribute containsthe 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 isnot listed, a runtime error occurs.

Page 396: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-6

SAP AG 2002

75<��������&$7&+�F[B�����F[B����������>,172�UBH[F�@������������������������������&$7&+�F[B����������>,172�UBH[F�@�������������������������������&/($183��������(1'75<�

+DQGOLQJ�([FHSWLRQV

Code whoseexceptions (ifany) are to be

handled

Handlers for thespecified

exception classesDQG their

subclasses

Block for "tidying up"if there is no handleravailable in the TRY-ENDTRY structure

Handlers for thespecified exceptionclasses DQG their

subclasses

n 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-ENDTRYstructures themselves.

n The TRY block contains the application code that is to handle the exceptions. If an exception occursin 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 TRY-ENDTRY structures. If it finds one, it navigates to this handler. If it cannot find a handler but theTRY-ENDTRY structure is in a procedure, it then tries to pass the exception along to the callingprogram. (This will be discussed in more detail later.)

n A CATCH block contains the exception handler that is executed if a specified exception hasoccurred in the TRY block in the same TRY-ENDTRY structure. After the CATCH statement, youcan specify as many exception classes as you wish. In this way, you define an exception handler forall these exception classes and their subclasses. After an exception occurs, the system searchesthrough the listed exception handlers in the order specified. It then executes the first exceptionhandler whose CATCH statement contains the relevant exception class or one of its superclasses.

n In some cases, the system cannot find a handler for an exception within a specific TRY-ENDTRYstructure but the exception is handled in a surrounding TRY-ENDTRY structure or passed along to acalling program. If this occurs, a CLEANUP block is executed before leaving the TRY-ENDTRYstructure.

Page 397: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-7

SAP AG 2002

([DPSOH��+DQGOLQJ�D�3UHGHILQHG�([FHSWLRQ

3$5$0(7(56��LQW���7<3(�L�������������LQW���7<3(�L�'$7$��UHVXOW�7<3(�L�������WH[W���7<3(�VWULQJ�������UBH[F��7<3(�5()�72�F[BURRW���������75<�������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<�������

,I�DQ�RYHUIORZ�HUURU�RFFXUV��WKHUXQWLPH�V\VWHP�UDLVHV�WKHF[BV\BDULWKPHWLFBRYHUIORZ

H[FHSWLRQ

Information

2YHUIORZ�LQ�WKH�RSHUDWLRQ�

"

LL

n In the above calculation, if the value range for data type i is exceeded, the runtime system raisesthe exception CX_SY_ARITHMETIC_OVERFLOW. This exception is handled in theimplemented CATCH block.

n The object reference to the exception object is stored in the reference variable r_exc. Usingr_exc and the functional method get_text, the handler accesses the exception text for thisexception object and stores in the string variable text.

n To display exception texts as messages, the MESSAGE statement has been extended so that you canuse 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.

n If the value range for data type i is not exceeded, no exception is raised and the TRY block isprocessed completely. The program then continues executing after the keyword ENDTRY.

n The class CX_SY_ARITHMETIC_OVERFLOW is a subclass of the classesCX_SY_ARITHMETIC_ERROR, CX_DYNAMIC_CHECK, and CX_ROOT. Thus the exceptionraised above can also be handled if you enter one of these classes after the CATCH statement.

n The keyword documentation for each keyword lists the exception classes whose exceptions mayoccur when the appropriate ABAP statement is executed.

Page 398: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-8

SAP AG 2002

([DPSOH��8VLQJ�<RXU�2ZQ�([FHSWLRQV

¿UÀ£ÁÃÂ0ÂÅÄ·ÆUÄ�Ç È É�Ê�Ë Ä�È·ÌJÍÏÎ,ÐÒÑ�ÀJÓhÐ�Ó·Ô*ÕWÁ¤Õ*Î�Ö�ÔØ××·×U×Ð ÓUÕ0Ù0Ö·ÚÜ۷ͤÝ�Ç�Ý�Í!Æ¡ÞUÌ*É·Æ·È*Ä7ÇÒÈUÝJÝ�Ê!ÉWßUà�Ý¡Í�á�×Â�ÓUÀ·ÓU¿JÕâ·ÎhÔ!ãJÀ�ÓÅäUÍ*É¡Û�Þ}ÝåÝ¡È0ÌJæUÆ·È�ËÏç¡èUÖWÐéá¡È¡Ë¤Ä¡ÈJÌ0ÍÎWÔ!Õ0ÖëêjÍJì£Ç}ä0Í�É�Û0Þ�Ýîí�Í0ì�ÇÒÝ�È�ÌUæJÆ0È¡ËÒïð Ù·ÓJè0ÓåˤÄ�ÈUÌ·Í�ÝWñJËUÍÅòâÉWó�ÇÃÝ£ñJËUÍô×Î�çõá£ñ�ö�á¡à¡ß Ê0Æø÷ ùâúî×ÍJìWÇ ä0ͤÉWÛJÞ�ÝûòÏüJú0úJú·ú!ú�×ÍJìWÇÃÝ�ÈUÌ·æ!Æ·È�ËÜòÏü0úJú·ú!ú�×Ó0Ô0Ú}Ρçý×

ÓJÔ0Ú�Ð*Ó!Õ·ÙJÖ¡Úþ××·×U×ÓJÔ·Ú�¿0ÀhÁ�ÂJÂÿ×

¿UÀ£ÁÃÂ0ÂÅÄ·ÆUÄ�Ç È É�Ê�Ë Ä�È·ÌJÍ ÚJÓ�çJÎ�Ô Î¡Õ*Î�Ö�Ô ××·×U×Ð ÓUÕ0Ù0Ö·Ú Â Û!ÍUݽÇÒÝ¡ÍUÆ�ÞJÌ�É�Æ0ÈUÄ£Ç È*Ý0Ý·ÊUÉhßJà Ý�ͤáÎ,ÐÒÑ�Ö0èJդνÔ*ãõɽó·ÇÒÝhñ0Ë*Í Õ � Ñ�Ó á�È�Ë Ä�È�ÌJÍ ö£Ë Ä·È·ÌJÍ*Ýhñ0Ë!ÍÓ��¤Ñ�Ö0èJդνÔ*ãåÍ0ìhÇ�äUÍ*É�Û·Þ Ý Õ � Ñ�Ó á7Ç�Ë Ä�È�ÌhÇ�äUÍ*ÉÍ0ìhÇÒÝ�È·Ì!æJÆ0È�ËõÕ � Ñ�Ó á7Ç�Æ·È�ËUÈUÆUÉJÝWñØ××·×U×ÓJÔ·Ú�¿0ÀhÁ�ÂJÂÿ×

$GDSWLQJ�WKH�SURJUDP�,I�WKHUH�LV�QR�WDEOH�HQWU\�DYDLODEOH�UDLVH�DQG�KDQGOH�DQ�H[FHSWLRQ�\RX

KDYH�ZULWWHQ

n The above program source code shows the method get_technical_attributes of the classlcl_airplane, which was implemented in an earlier exercise in this training course. It receivesan airplane type as an import parameter and returns its weight and tank capacity as exportparameters.

n The relevant information is read from the database table VDSODQH. If the airplane type passed is notavailable in this table (that is, if sy-subrc <> 0), the values 100.000 and 10.000 respectivelyare assigned to the export parameters ex_weight and ex_tankcap. We will now change thisbehavior: If an airplane type is not entered in the table, an exception that we have defined should beraised and handled appropriately.

Page 399: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-9

SAP AG 2002

&UHDWLQJ�<RXU�2ZQ�([FHSWLRQ�&ODVVHV

������� ����������� �������������������������� ����!�

"$#&%&')( *MIME RepositoryObject NavigatorRepository InfosystemTag LibraryTransport Organizer

Object Name Description

SULYDWSURWHFWHG������+����DEVWUDFW

,QKHULWV�)URP

&ODVV�7\SH�8VXDO�$%$3�&ODVV([FHSWLRQ�&ODVV3HUVLVWHQW�&ODVV

)LQDO�&ODVV2QO\�PRGHOHG

,.-$/103254�6�232/87 9;:;:�<)= >5( 95( ?/@7 9A:A:3#;:

Workbench Edit Goto Utilities Environment System Help

&;B67$7,&6B&+(&.=&;B:521*B3/$1(7<3(&ODVVB)9AC5DE9;F�#

,G-$/H032&4�6�2A2

!

n Exceptions are represented by objects that are instances of exception classes. Defining an exceptionis thus synonymous with creating an exception class.

n Exception classes are generally defined globally. For special exceptions that will only occur within asingle ABAP program however, you can also define local exception classes.

n Global exception classes are defined and managed in the Class Builder. When you create a newclass, 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.

n The ([FHSWLRQ�%XLOGHU offers all the functions you need to create exception classes and generatesspecified components that cannot be changed. When you create an exception class, you must alsospecify which category of exception it will be - that is, whether it is derived fromCX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK.

Page 400: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-10

SAP AG 2002

0DLQWDLQLQJ�<RXU�2ZQ�$WWULEXWHV�DQG�([FHSWLRQ7H[WV

Class Edit Goto Utilities Environment System Help

IKJMLONPNRQTS�UEJ�VXW!Y[Z\IK]XLO^`_aWbIKJMLONPNdceIgfihXjlknmpo qrhPsut\vwoyxiz\{\sux

|�}~}M��� ���.}�� �H�8�1� � � �H�H�8� �$� |��.�.�8�$� �)}��1���1�@�.����

�8���1}��|}~}���� �H�.}��)�

���@� �[�� ���� � ���~�� � �$�!� �O�¡ ¢!� � ���¡£ � � �!� �¤��¡£ � �H¥����

�H�8�1�  ¦� � � ��� � � ��H�8�1�  ¦� � � ��� � � ��H�8�1� � � ���¡� ���§� ��H�8�1�  ¦¨1©Gª � �!� � ��H�8�1�   |��¦£�|��[�@«��¦£G|��!�8�.¥�¦�

�§¬ �G�)���¤�)}����¤­��1�)� ® ���8�H¯ �°� ��± � �¦£�|��!�H�H¥�¡� � ²\� ¬ �$²P���G}��1�1³ |��)}�� ´$��§¬ �G�G�\�°�H�H�$²µ�H�.}��)}¶� �¡�

� �¡�.�)} � � �¦�8� � ��¤�)�1}��H� � � �¦�8� � ��¤�)�1}��H� � � �¦�8� � ��¤�)�1}��H� � � �¦�8� � ��¤�)�1}��H� � � �¦�8� � �

·8� ¬ }��H�<RXU�RZQ�DWWULEXWHVSURYLGH�WKH�XVHU�ZLWKDGGLWLRQDO�LQIRUPDWLRQ ¸

¸¸¸

([FHSWLRQ�WH[WXVHG�WR�GHVFULEHH[FHSWLRQ�VLWXDWLRQLQ�PRUH�GHWDLO

Exception ID Text

CX_ROOT An exception occurred

ZCX_WRONG_PLANETYPE This airplane type is unknown

n The methods are all inherited from CX_ROOT. You can also add your own methods. The instanceconstructor is generated automatically.

n 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 IMPORTINGparameters for these attributes.

n The exception texts of global classes are defined on the 7H[WV tab of the Exception Builder. They cancontain parameters. To do this, use the elementary attributes of the exception class by enclosing theirname in ampersands (’&’) in the exception text.

n The exception texts of global exception classes are stored in their different translations in the 2SHQ7H[W�5HSRVLWRU\��OTR). Note that several texts can be assigned to a single class. You assign a text toan exception using the TEXTID attribute, which contains the globally unique ID of the text objectwithin an exception object at runtime. The method GET_TEXT then exports this text, replaces anytext parameters with the contents of the relevant attributes as necessary, and returns the text as acharacter string.

n For each global class, the Exception Builder generates a default text whose name matches the classname. (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 theOTR. You can then specify which text is to used when an exception is raised by passing an identifierto the IMPORTING parameter TEXTID of the instance constructor. If you do not specify a text, thedefault text is used.

Page 401: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-11

SAP AG 2002

Ð*Ó!Õ·ÙUÖ�ÚÜÛJÍ*Ý�Ç�Ý¡ÍUÆ·ÞJÌ�É�Æ0È!Ä�ǤÈ*Ý·ÝJÊJÉhßJà Ý�Í áî×Ú·Á ÕhÁº¹�Ê£Ç�ÍJìUÆÅÕ � Ñ�ÓÅè0Ó*çÅÕ�ÖÜÆ·ìWÇ�Ê�»[»!ÝÿíÝ¡Í·ì�Ý Õ � Ñ�Ó á·Ý·ÊJÉ·Ì·ÛôסÓUÀ·ÓU¿JÕõÂ0ÎWÔ!ãUÀ�ÓÅäUÍ*É¡Û·Þ Ý Ý�È0ÌJæUÆ·È�Ëâç¡è!ÖhÐ á¡È¡Ë¤Ä¡È0Ì0ÍÎhÔ!Õ0ÖëêjÍJìWÇ ä0ͤÉhÛ0Þ�Ýîí�Í0ìhÇÒÝ·È·ÌUæ0Æ0È¡ËÒïð Ù·ÓJè0ÓåˤġÈ0̷ͤݣñJËUÍÅòâÉ£ó�Ç�ÝWñJËJÍô×ηçõá£ñ�ö·áhà�ß�Ê0Æ ÷ ùâúî×Õ·è � × è¡Á�Î0ÂhÓ Ó°�!¿·Ó�Ñ�Õ*Î�Ö¡ÔâÕ � Ñ�Ó½¼¡Æ·ìWǤä!Ê°»�ÌJÛWÇ�Ë Ä�È·ÌUÍ*Ýhñ0Ë!ÍÓ��¤Ñ�Ö0èUÕ�Î½Ô ãûË Ä�ÇÒÝhñ0Ë!Í òâÉ�ó·ÇÒÝhñ0Ë!Í ×¿hÁ ÕJ¿�Ù¾¼·Æ�ìhÇ�ä*Ê�»0Ì0ÛhÇ!Ë�Ä�È0Ì0Í�ÝWñJËUÍÏÎhÔ!Õ0Ö Ê½Ç¤Í0ì!Æ�×Ý¡Í·ì�Ý òÜʽǤÍ0ì!Æ*ö�ù�Û!Í!Ý�ÇÒÝ¡Í0ì�Ý ê ïî×Ð�Ó*ÂJÂ�Á}ã�ÓÏÝ¡Í0ì�Ý Õ � ÑWÓÀ¿2Îg¿0×Ó0Ô0Ú*Õ�è � ×

ÓJÔ0Ú}Ρçý×Ó·ÔJÚ¡Ð ÓUÕ0Ù0Ö·Ú ×

5DLVLQJ�DQG�+DQGOLQJ�([FHSWLRQV�<RX�+DYH�:ULWWHQ

([DPSOH�LPBW\SH� �$�������

Information

7KH�DLUSODQH�W\SH�$��������LVXQNQRZQLL

"

n If the airplane type passed to the method has not been stored in the table VDSODQH, the exception wedefined previously, ]F[BZURQJBSODQHW\SH, is raised. In addition, a TRY-ENTRY control structure isimplemented that is only processed if sy-subrc <> 0.

n The TRY block contains the application code that is to handle the exceptions. When the exception israised, the IMPORTING parameter SOBW\SH of the instance constructor is filled. (This parameter isautomatically generated by the Exception Builder.) Using this parameter, the program then assignsthe value of the airplane type to the identically-named attribute.

n The exception that has been raised is handled in the CATCH block. The reference to the exceptionobject is stored in the reference variable UBH[F, which was created as a local data object in themethod (TYPE REF TO cx_root).

n Since the IMPORTING parameter TEXTID of the instance constructor was not filled when theexception was raised, the default text generated when the exception class was created is addressedusing the functional method JHWBWH[W. The method GET_TEXT then exports this text, replaces thetext parameter with the contents of the attribute SOBW\SH, and returns the text as a character string.

n The returned text is stored in the local data object WH[W, which has the type VWULQJ� The text is thendisplayed as an information (type ,) message.

Page 402: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-12

SAP AG 2002

3DVVLQJ�([FHSWLRQV�$ORQJ¿UÀWÁÒÂ0 Æ!Ä¡È á�á Î�Ð�Ñ�À�Ó¡Ð*Ó0Ô!Õ�Á Õ�ÎWÖ·Ô ××0×J×Ð*Ó!Õ·ÙJÖ�Ú ó ÍUÝ�Þ ××J×0×è¡ÁÒÎ0ÂhÓÅÓ°�U¿JÓ*Ñ�Õ*Î�Ö�ÔõÕ � Ñ¡ÓõÆ¡ìhÇ ÍJìUÆ·Í�Ë}Ý0ɦ»·Ì ×Ó·ÔJÚ¡Ð�ÓUÕ0Ù0Ö0Ú ×ÓJÔ0Ú*¿0ÀhÁ�ÂJÂÿ×

¿JÀ£ÁÒÂ0 Æ*Ä�È á0á ÚUÓUç*ΣÔ}Î�Õ ÎhÖ·Ô ××·×J×Ð!Ó!Õ·ÙJÖ�Ú} ó*Í�ÝhÞÏÎ�Ð�Ñ�Ö�è*Õ*ÎWÔ!ã ÷�É�ó·Ç*Ë�ÈJÊ0Èhó�Í*Ý¡ÍUÊ0ùÓ°� Ñ�Ö�è*Õ*ÎWÔ!ã ÷�Í·ìhÇ*Ë�ÈJÊ0Èhó�Í*Ý¡ÍUÊ0ùè¡ÁÒÎJÂ0ÎhÔ!ã Æ·ì£Ç¤Í0ì*Æ�Í·Ë ÝJÉ¡»0Ì ××·×J×Ó0Ô·Ú*¿0ÀhÁ�ÂUÂî×

èUÓ�Ñ¡Ö·è!Õ Ë*Ê°»�Ë È�ÛUÈ!Ý¡ÍWǤÍ0ì!Æ�Í0Ë ÝJÉ@»0Ì�áÿ××U×0×Ú·Á ÕhÁ ʽÇ�»¡ß�Á Õ � ÑhÓÅèJÓ!çåÕJÖÜÆJÄ�È*áJáî××U×0×Õ0è � ×ʽÇ�»¡ß�ÁJö�ùWó¤Í!Ý�Þþê Ó°� Ñ¡Ö·è*Õ*ÎWÔUã ×J×0×Î�Ð�Ñ¡Ö·è*Õ*ÎWÔUã ×J×0× ïî׿hÁ ÕJ¿·ÙÜÆ·ì£Ç�Í·ì!Æ�Í·Ë ÝJÉ¡»0Ì ××J×0×

ÓJÔ0Ú*Õ�è � ×

 à ¬·»�«0¾¡ª­ ¹*±¡¸ ªAÄ¡Å¡ª!¹J»�»0ºJ° ­�±¾J°c¹J»h«¡® ºJ°¢«¡¸þ®J¹ ±�¹·ª ±·«¡»h«h¸�¸¡Å0°�­�² Ʊ¡«¡«�® ªj¹wÇ¡«uĦÅJ±�®�² «¡®þª3Ä¡«J°¢«�Èhª;Ä¡«�Æ»8ÅU±eÇ¡« ¾¡Åh¸¡¸�«¡®ÉÅ0² ¹*± ¶ ªj¹ ª;Ä�«»8ÅU²�²�­�± ¶ ¾J°¼¹ ¶ °�Å¦Ê ´

ó

n Exceptions that occur in procedures (methods, function modules, or subroutines) do not necessarilyneed to be handled there; they can be passed along to the calling program. The calling program canthen handle the exception itself or also pass it along to its own caller, and so on.

n The highest levels to which an exception can be passed are processing blocks without local dataareas - that is, event blocks or dialog modules. The exceptions passed along by the called proceduresmust be dealt with there, as must any exceptions raised within this processing block itself. Otherwisea runtime error occurs.

n To pass along an exception from a procedure, you generally use the RAISING addition whendefining the procedure interface.

n In methods of local classes and subroutines, specify the RAISING addition directly when definingthe procedure (METHODS meth ... RAISING cx_... cx_..., FORM form ... RAISING cx_... cx_...).After RAISING, list the exception classes whose objects are to passed along.

n In methods of global classes, the exception classes whose objects are to be propagated are entered inthe exception table of the method in the Class Builder. Check the ([FHSWLRQ�&ODVV field in thisexception table. Similarly, exceptions raised by function modules are passed along by being enteredin the Function Builder.

Page 403: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-13

SAP AG 2002

([DPSOH��3DVVLQJ�([FHSWLRQV�$ORQJ

Ð�ÓUÕ0Ù0Ö0ÚÜÛ·Í�Ý,ÇÃÝ�Í!Æ¡Þ!Ì*É·Æ·È*Ä7Ç}È!ÝJÝ�Ê*ÉWßUà�Ý¡Í�áÿ×ÂhÓ!À�Ó*¿0ÕÜÂUΣÔ*ã0À·Ó äJÍ�É�Û0Þ*ÝÅÝ�È·Ì!æJÆ0È¡Ëç�è0Ö�Ðéá�È�Ë Ä·È·ÌJÍÎWÔUÕ0ÖëêjÍJìWǤäJÍ�É�Û0Þ*Ýîí�Í0ì�ÇÒÝ�È·ÌUæJÆ0È�ËÒïð Ù�ÓJè0ÓåˤġÈJÌ0Í�ÝWñJËJÍÅòâÉWó�ÇÃÝWñJËUÍô×Ρçõáhñ}ö0á£à0ß*ÊJÆø÷�ùÏúÿ×è¡ÁÒÎJÂhÓåÓ°�!¿·Ó�Ñ�Õ*ΡÖ�ÔÕ � Ñ�Ó½¼¡Æ·ìWǤä!Ê°»·ÌJÛ£Ç�Ë Ä�È·ÌUÍ*Ýhñ0Ë!ÍÓ°��Ñ¡Ö·è!Õ*ÎWÔ!ã ˤÄ,ÇÃÝ£ñJËUÍÅòÏÉ£ó�ÇÃÝWñJËJÍ ×Ó·ÔJڤηç�×Ó0Ô0Ú�Ð*Ó*Õ·ÙJÖ¡Ú ×

¿JÀ£Á�·ÂÅÄ·ÆUÄ�ǤÈ*É�Ê�Ë Ä�È�ÌJÍ ÚJÓ*ç!ÎhԤΡÕ*Î�Ö¡Ô ×Ð�ÓUÕ0Ù0Ö·Ú ÂÌË ÉJáWË�Ä�È¡ñ¡Ç¤È*Ý0Ý0ÊJÉ�ßJà ÝhÍ áî×Ð�ÓUÕ0Ù0Ö·Ú ÂûÛUÍ*Ý�Ç�Ý¡ÍUÆ�ÞJÌ�É�ÆJÈUĽǤÈ*Ý·Ý·ÊUÉhßJà¤ÝhͤáÎ�Ð�Ñ¡Ö·è*Õ!ÎhÔ!ãõÉ£ó¡ÇÃÝWñJËUÍ Õ � ÑhÓ á¡È¡Ë¤Ä¡È0Ì0ͤö�Ë�Ä�È0Ì0Í�ÝWñJËUÍÓ°� Ñ¡Ö·è!Õ*ÎhÔ!ãåÍJì£Ç äJÍ�É�ÛJÞ�Ý Õ � ÑhÓ á,Ç*ˤġÈ0ÌWÇ ä0ͤÉÍJì£ÇÃÝ¡È0ÌJæ!Æ·È�Ë Õ � ÑhÓ á,Ç}Æ0È¡Ë*ÈJÆ!É·Ý¡ñè¡ÁÒÎ0ÂJÎWÔ!ã ¼�Æ¡ìhÇ�ä*Ê�»JÌ0ÛhÇ*ˤġÈ0Ì·Í�ÝWñUËUÍ ×Ó0Ô·Ú ¿·ÀhÁ�ÂJÂÿ×

Ð�ÓUÕ0Ù·Ö·ÚÍË*ÉUá£Ë¤Ä¡È�ñ�Ç}ÈUÝ!Ý¡Ê!ÉWßUà�Ý¡Í�áÿ×Ú¡Á¤Õ£Áu¹ ʽǤÍ0ì!ÆåÕ � ÑWÓ èJÓ!çåÕ0ÖÜÆ�ì�Ç}Ê°»�»*Ý�íÝ�ÍJì*Ý Õ � ÑWÓ á0Ý�Ê!É�ÌJÛ ××0×J×Õ�è � ×Û·Í�Ý,Ç�Ý�Í!Æ¡ÞUÌ*É·Æ·È*Ä7ÇÒÈUÝJÝ�Ê!ÉWßUà�Ý¡Í�áôêÓ°� Ñ¡Ö·è!Õ*ÎWÔUãâÉ£ó�ÇÃÝWñJËUÍÅòåˤÄ�È0Ì0Í�ÝWñJËUÍÎ�Ð�Ñ¡Ö·è!Õ*ÎWÔUãÅÍJìWÇ äJÍ�É�Û0Þ�Ý ò äUÍ*É¡Û·Þ ÝÍJìWÇÃÝ¡È0ÌJæUÆ·È·ËÜò Æ·È�Ë ïÃ×ð è¤Î�Õ0ÓιdÏ�¿@ã0Í·ä É¡Æ0Þ*Ý�¹[¿*ê�úJú�Ð*ï�í ä*Í!É¡Û·Þ Ý�í¿@ÕJÈ·ÌUæ·æ!È�Ë*È�¼0É0ݦÑ!Ý�¹[¿�êú!ú�Ò!ï�í Æ0È¡ËØ׿hÁ�ÕJ¿·Ù½¼�Æ�ìhÇ�ä*Ê�»0Ì·Û¡Ç!ˤġÈ0Ì0Í�ÝWñJËUÍ ÎWÔUÕ0ÖÜʽǤÍ0ì*Æ ×ÝhÍJì*Ý òÜ棂 ÍJìUÆ�öWùUÛ·Í�Ý,ÇÃÝ�ÍJì*Ýôê ï�×Ð!Ó Â0£Á ã·ÓÏÝ�ÍJì*ÝûÕ � Ñ�ÓÀ¿�Î�¿·×Ó·ÔJÚUÕ·è � ×Ó0Ô0Ú�Ð!Ó!Õ·ÙUÖ�Ú ×

ó

n As in the previous example, the exception we have defined (]F[BZURQJBSODQHW\SH) is raised if theairplane 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.

n To pass the exception along to the caller of the method, we enter it after the RAISING keyword.

n Now, the caller - that is, the method GLVSOD\BDWWULEXWHV�- handles the exception. For this purpose, wehave implemented a TRY-ENDTRY control structure in this method. The methodJHWBWHFKQLFDOBDWWULEXWHV is now called in the TRY block of this control structure.

n If the exception is raised in the method JHWBWHFKQLFDOBDWWULEXWHV, the program continues by handlingthis exception. That is, the method JHWBWHFKQLFDOBDWWULEXWHV is terminated and the appropriateCATCH block is processed within the caller. Note in particular that the program no longer executesthe WRITE statements entered in the TRY block DIWHU JHWBWHFKQLFDOBDWWULEXWHV is called.

Page 404: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-14

SAP AG 2002

([FHSWLRQV�7KDW�0XVW�%H�'HFODUHG

F[BG\QDPLFBFKHFN F[BVWDWLFBFKHFNF[BQRBFKHFN

F[BURRW

Â Ó ¹*ºØ»@Å0±ÉĦÅ0±�®�² «ØªEÄ¡«¡¸¡««h¬·»h«J¾¡ª@­ ¹*±�¸ ´@Ô Õ Æ·¹*ºØ®U¹ ±�¹Jª�Öª3Ä¡«�ÆKÅU°¢«¦¾¦Åh¸�¸·«¡®uÅ0²�¹*± ¶ÅJº�ª¼¹!ÊaÅ�ªc­ »@Å0²�² Æ ´ Ó ¹*º »8ÅJ±U±�¹·ª¾¦Åh¸�¸þª;Ä¡«¦Êû«h¬!¾J²�­ »·­ ª9² Æ º�¸U­�± ¶ª3Ä¡«a×§Ø Ô Ù�Ô�ÚÜÛ Å�®J®*­ ª­ ¹*±Â Ú ¹·ª ¾¦Å0°¢ª!¹ Õ ¸8Æ*±�ª�Åh¬ »�Ä¡«�» ¯

Â Ó ¹*ººÊ º�¸�ª§Ä¦ÅJ±�®*² «ôªAÄ�«�¸¡««h¬0»h«0¾�ª9­ ¹*±�¸ ¹*° ¾¡Å�¸¡¸Øª;Ä¡«¦ÊÅJ² ¹*± ¶ «h¬!¾J²�­ »0­ ª² Æ º¡¸J­�± ¶ ªAÄ¡«×§Ø Ô Ù�Ô�ÚÜÛ Å�®J®*­ ª­ ¹*±Â Ú ¹·ª ¾¡ÅU°¢ª!¹ Õ ¸8ÆU±�ª¶Åh¬ »°Ä¡«¡» ¯

Â Ó ¹�ººÊ º¡¸¡ª§Ä¦Å0±�®*² «ªAÄ¡«�¸¡« «W¬0»h«J¾¡ª@­ ¹*±�¸ ¹*°¾¡Åh¸¡¸Øª;Ä¡«�ÊÝÅ0² ¹*± ¶«W¬U¾U²�­ »·­ ª² Ʀº�¸!­�± ¶ ª3Ä¡«×Ø Ô Ù�Ô�Ú§Û Å�®J®*­ ªc­�¹*±Â Þ ÅJ°¢ª!¹ Õ ¸8Æ!±¡ª¶ÅW¬ »[Ä¡«¡» ¯

n Subclasses of CX_STATIC_CHECK: The relevant exception must either be handled, or passedalong explicitly using the RAISING addition. The syntax check ensures that this is the case. Atpresent, only exceptions you define yourself for error situations in the application code aresubclasses of CX_STATIC_CHECK.

n Subclasses of CX_DYNAMIC_CHECK: The relevant exception does not have to be declared. Ifsuch an exception occurs at runtime, just as with subclasses of CX_STATIC_CHECK, it must eitherbe handled or passed along explicitly using a RAISING addition. However, this is not checked in thesyntax check. If such an exception occurs at runtime and is not either handled or passed along, aruntime error occurs. Most predefined exceptions with the prefix CX_SY_... for error situations inthe runtime environment are subclasses of CX_DYNAMIC_CHECK.

n Subclasses of CX_NO_CHECK: These exceptions cannot be declared. These exceptions can behandled. Otherwise they are automatically passed along. The syntax check never finds an error here.All exceptions of the category CX_NO_CHECK that are not handled in the call hierarchy areautomatically passed to the top level, If they are not caught there, they cause a runtime error. Somepredefined exceptions with the prefix CX_SY_... for error situations in the runtime environment aresubclasses of CX_NO_CHECK.

Page 405: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-15

SAP AG 2002

l &UHDWH�\RXU�RZQ�H[FHSWLRQ�FODVVHVl 5DLVH�H[FHSWLRQV�LQ�D�SURJUDPl +DQGOH�H[FHSWLRQVl 3DVV�H[FHSWLRQV�DORQJ

<RX�DUH�QRZ�DEOH�WR�

([FHSWLRQ�+DQGOLQJ��8QLW�6XPPDU\

Page 406: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-16

([HUFLVH��8QLW� ([FHSWLRQ�+DQGOLQJ7RSLF�� 'HILQLQJ��UDLVLQJ��SURSDJDWLQJ��DQGFDWFKLQJ�FODVV�EDVHG�H[FHSWLRQVAt the conclusion of these exercises, you will be able to:

• Define exception classes

• Raise class-based exceptions

• Pass exceptions along

• Catch class-based exceptions

Improve your program so that, when the airplane attributes aredisplayed, an error text is displayed instead of the default valuesif the airplane type is invalid.Implement this using the class-based exceptions concept.

3URJUDP� =%&���B��B5$,6(B75<7HPSODWH� 6$3%&���B(9(6B0$,1B�%0RGHO�VROXWLRQ� 6$3%&���B(;&6B5$,6(B75<���LV�\RXU�WZR�GLJLW�JURXS�QXPEHU

1-1 Copy your solution to the last exercise from the unit (YHQWV, or the correspondingmodel solution 6$3%&���B(9(6B0$,1B%, with all their includes. Give them thenew names =%&���B��B5$,6(B75<, =%&���B��B5$,6(B75<B&/�, and=%&���B��B5$,6(B75<B&/�.

1-2 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 toensure that, after the relevant exception is raised, it is either handled or passedexplicitly along using a RAISING addition.Add an attribute for the airplane type (suggested name: 3/$1(7<3() and assignthe type 6B3/$1(7<3( to it.Create a default error message that can be enhanced dynamically to include the airplanetype.

1-3 Raise the exception in the JHWBWHFKQLFDOBDWWULEXWHV method of your localclass OFOBDLUSODQH.Add a 5$,6,1* addition to the definition part of the method, so that the exceptioncan be passed along.

Page 407: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-17

1-4 Catch the exception in the GLVSOD\BDWWULEXWHV method of your local classOFOBDLUSODQH.To do this, you will need a local reference to the exception instance (suggestedname: UBH[FHSWLRQ). Assign a type to this reference using the superclass&;B5227.Read the error text from the exception instance using the JHWBWH[W method. Todo this you will need an auxiliary variable of the type VWULQJ (suggested nameH[FBWH[W).

Page 408: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-18

�6ROXWLRQ��8QLW� ([FHSWLRQ�+DQGOLQJ7RSLF��'HILQLQJ��SURSDJDWLQJ��DQG�UDLVLQJ�HYHQWV

0RGHO�VROXWLRQ��,QFOXGH�%&���B(;&6B5$,6(B75<B&/��

*&---------------------------------------------------------------------*

*& Include BC401_EXCS_RAISE_TRY_CL2 *

*&---------------------------------------------------------------------*

...

*------------------------------------------------------------------*

* CLASS lcl_airplane DEFINITION *

*------------------------------------------------------------------*

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

"---------------------------------------------

CONSTANTS: pos_1 TYPE i VALUE 30.

METHODS: constructor IMPORTING

im_name TYPE string

im_planetype TYPE saplane-planetype,

display_attributes.

CLASS-METHODS: display_n_o_airplanes.

EVENTS: airplane_created.

PRIVATE SECTION.

"----------------------------------------------

METHODS: get_technical_attributes

IMPORTING im_type TYPE saplane-planetype

EXPORTING ex_weight TYPE s_plan_wei

ex_tankcap TYPE s_capacity

RAISING cx_bc401_invalid_planetype.

DATA: name TYPE string,

planetype TYPE saplane-planetype.

Page 409: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-19

CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS. "lcl_airplane DEFINITION

*------------------------------------------------------------------*

* CLASS lcl_airplane IMPLEMENTATION *

*------------------------------------------------------------------*

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[BEF���BLQYDOLGBSODQHW\SH�,172�UBH[FHSWLRQ���������H[FBWH[W� �UBH[FHSWLRQ�!JHWBWH[W������������:5,7(����H[FBWH[W�&2/25�&2/B1(*$7,9(�����(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)

Page 410: 003 - Bc401 - Abap Objects

© SAP AG BC401 14-20

WHERE planetype = im_type.

IF sy-subrc <> 0.

������5$,6(�(;&(37,21�7<3(�F[BEF���BLQYDOLGBSODQHW\SH���������������������������(;3257,1*�SODQHW\SH� �LPBW\SH� ENDIF.

ENDMETHOD. "get_technical_attributes

ENDCLASS. "lcl_airplane IMPLEMENTATION

Page 411: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-1

SAP AG 2002

l '\QDPLF�DWWULEXWHVl 'DWD�UHIHUHQFH�YDULDEOHVl )LHOG�V\PEROVl 5XQWLPH�7\SH�,GHQWLILFDWLRQ��577,�

&RQWHQWV�

'\QDPLF�3URJUDPPLQJ

Page 412: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-2

SAP AG 2002

l 8VH�G\QDPLF�DWWULEXWHV�LQ�VXLWDEOH�$%$3VWDWHPHQWV

l 8VH�GDWD�REMHFWV�JHQHUDWHG�DW�UXQWLPHl $VFHUWDLQ�WKH�DWWULEXWHV�RI�GDWD�REMHFWV�DW�UXQWLPH

$W�WKH�FRQFOXVLRQ�RI�WKLV�XQLW��\RX�ZLOO�EH�DEOH�WR�

'\QDPLF�3URJUDPPLQJ��8QLW�2EMHFWLYHV

Page 413: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-3

SAP AG 2002

'\QDPLF�3URJUDPPLQJ�,QWHJUDWLRQ�LQ�&RXUVH�&RQWHQW

������� ��� � � ���� �� � � �� ��� ��� ����� � � � � ������� � � � � � � � � � � �! � � �

� ��" " � � #�$�% % & '�(*),+�- ( + $�.0/$ '21!) $ / / & '3(*4 $�5 $687�9 :�;!< :�= >@? :�A�BC A�D B > 7E A = A C�F@G > 7 A :3HE A = AJI DLK >�M = 7 9 :E > = A 9 B NPO8Q�R S�TVUXW T R W@Y T@Z

[,\ ] ^�_ ] `�a bcJd2e2fXgih b _kj l f \�] b ^ j bkm [ \ d3n\ o�p p ] ^ nq ^2r b \s] j o ^ _ b

t o c j ] ^ nq ^ j b \ e o _ b cu8v b ^ j cw a d g o�a t a o c c b c o ^ m q ^ j b \ e o _@b c

x ` b _ ] o�a�y b _ r ^ ] z�{ b cu�| _ b ` j ] d ^ r o ^ m a ] ^ n

}�~��0����� �������� � ��� � � � �}�~ �0� � � �

� ����� � � � ��� � �

Page 414: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-4

SAP AG 2002

)LHOG�6\PEROV�LQ�'HWDLO)LHOG�6\PEROV�LQ�'HWDLO

'\QDPLF�&DOOV'\QDPLF�&DOOV

'DWD�5HIHUHQFHV'DWD�5HIHUHQFHV

'\QDPLF�3URJUDPPLQJ����

$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\

Page 415: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-5

SAP AG 2002

)LHOG�6\PEROV

'$7$�LQW�7<3(�L�9$/8(����),(/'�6<0%2/6��IVBLQW!�7<3(�L�$66,*1�LQW�72��IVBLQW!�:5,7(����LQW���IVBLQW!��IVBLQW!� ����:5,7(����LQW���IVBLQW!�81$66,*1��IVBLQW!�,)��IVBLQW!�,6�$66,*1('���:5,7(����LQW���IVBLQW!�(/6(���:5,7(����ILHOGV\PERO�QRW�DVVLJQHG�IQD��(1',)�

�2������*����� �s���

�2������*����� �s���

),(/'�6<0%2/6��IV!�7<3(_/,.(�����_7<3(�$1<`�$66,*1�����GDWDREMHFW�72��IV!�81$66,*1��IV!������IV!�,6�$66,*1('����

*HQHULF�RUFRPSOHWH�W\SHVSHFLILFDWLRQ

�2������*����� �s���

�2������*����� �s���

n You declare field symbols using the ),(/'�6<0%2/6��IV! statement. Note that the parentheses(<>) DUH part of the syntax.

n Field symbols allow you to access an assigned data object - that is, all the accesses that you make tothe field symbol are made to the data object assigned to it. Field symbols are similar to dereferencedpointers. Thus, you can only access the content of the data object to which the field symbol points.(That is, field symbols use value semantics).

n You use the ASSIGN statement to assign a data object to the field symbol �IV!. If the fieldsymbol 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 assignedto 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.

n Use the expression <fs>�IS ASSIGNED to find out whether the field symbol <fs> is assigned toa field.

n The statement UNASSIGN <fs>. sets the field symbol <fs> so that it points to nothing. Thelogical expression <fs> IS ASSIGNED is then false.

Page 416: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-6

SAP AG 2002

7KH�&$67,1*�$GGLWLRQ

7<3(6��%(*,1�2)�VWBGDWH�����������\HDU�����7<3(�Q�����������PRQWK����7<3(�Q�����������GD\������7<3(�Q��������(1'�2)�VWBGDWH� �RSWLRQ����LPSOLFLW),(/'�6<0%2/6��IV!�7<3(�VWBGDWH�$66,*1�V\�GDWXP�72��IV!�&$67,1*� �DFFHVV�DIWHU�FDVWLQJ�:5,7(�����IV!�\HDU���IV!�PRQWK���IV!�GD\� �RSWLRQ����H[SOLFLW),(/'�6<0%2/6��IV!�7<3(�$1<�$66,*1�V\�GDWXP�72��IV!�&$67,1*�7<3(�VWBGDWH����

�,�8�X�2���8���

���

��� ����

�0  �¢¡�s��� �s���

�s���

$66,*1�����GDWDREMHFW�72��IV!�&$67,1*�>7<3(�W\SH_���@��

,QKDOW�GHV�]XJHZLHVHQHQ'DWHQREMHNWV�ZLUG�VRLQWHUSUHWLHUW��DOV�RE�HU�GHQLPSOL]LW�E]Z��H[SOL]LWDQJHJHEHQ�7\S�KlWWH

7KH�FRQWHQW�RI�WKH�DVVLJQHGGDWD�REMHFW�LV�LQWHUSUHWHG�DV

LI�LW�KDG�WKH�LPSOLFLW�RUH[SOLFLW�W\SH�VSHFLILHG

n If you use the CASTING addition when you assign a data object to a field symbol that has a differenttype, you can remove the restrictions of having to use the data object’s original type. The access isthen interpreted as though the data object had the data type of the field symbol.

n If you use the CASTING TYPE <type> addition when you assign a data object to a field symbolthat has a different type, you can access the data object using the field symbol as if the object had thetype <type>.

n In the above example, note that the system field sy-datum is an elementary character-typecomponent of length �.

n 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 asthe line type of the transparent table passed using tabname. (You define the dummy data objectonly to ensure that line has the correct alignment).

Page 417: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-7

SAP AG 2002

)LHOG�6\PEROV�LQ�'HWDLO)LHOG�6\PEROV�LQ�'HWDLO

'\QDPLF�&DOOV'\QDPLF�&DOOV

'\QDPLF�3URJUDPPLQJ����

'DWD�5HIHUHQFHV'DWD�5HIHUHQFHV

$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\

Page 418: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-8

SAP AG 2002

8VLQJ�'\QDPLF�$WWULEXWHV�LQ�6WDWHPHQWV

l 5HSODFLQJ�D�OLWHUDO�ZLWK�D�YDULDEOH�&$//�75$16$&7,21�7B&2'(� �G\QDPLF�GDWDBREMHFW� �7B&2'(��&$//�75$16$&7,21�GDWDBREMHFW�

68%0,7�UHSRUWBQDPH� �G\QDPLF�GDWDBREMHFW� �5(3257B1$0(��68%0,7��GDWDBREMHFW��

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

l 5HSODFLQJ�D�OLVW�ZLWK�DQ�LQWHUQDO�WDEOH�

l 5HSODFLQJ�DQ�LGHQWLILHU�ZLWK�D�YDULDEOH�LQ�SDUHQWKHVHV��£¥¤L¦�§©¨�ª«¦ �VSDFHV��

n In many ABAP statements, you can pass attributes dynamically. The syntax for doing this differs,depending on the statement you use:

n If you pass the attribute as a literal in the static form of the statement, you can replace the literal witha variable. If you pass the attribute as an identifier in the static form of the statement, you can replacethe identifier with a variable in parentheses. Make sure there are no spaces between eitherparenthesis and the variable. If the attribute is a list, you can replace it with an internal table.

n Note: you PXVW�use upper case when filling data objects with literals.

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

n In Open SQL statements, you can also specify the logical conditions of the WHERE clausedynamically at runtime, by entering the ABAP source code in a variable. In the FROM clause of theSELECT statement, you can specify either individual table names or a dynamic JOIN expressionusing a variable. Similarly, you can program the GROUP-BY and HAVING clauses in SELECTstatements. For more information, refer to the keyword documentation.

Page 419: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-9

SAP AG 2002

&DOOLQJ�0HWKRGV�'\QDPLFDOO\

7<3(�322/6�DEDS�'$7$��SWDE�7<3(�DEDSBSDUPELQGBWDE�������HWDE�7<3(�DEDSBH[FSELQGBWDE������GDWDBREMHFW� �0(7+2'B1$0(� �)LOO�SWDE�DQG�HWDE�����&$//�0(7+2'�UHI�!�GDWDBREMHFW���������������������3$5$0(7(5�7$%/(�SWDE��������������������(;&(37,21�7$%/(�HWDE����

�  �¬ ¡®­ �¯� � °  J±s² ¡³ �  J´

�  �¬ ¡ °  J±s² ¡¡��  J´

7<3(�322/�DEDS�7<3(6�DEDSBSDUPELQGBWDE����7<3(6�DEDSBH[FSELQGBWDE�������

,QWHUQDOWDEOH�W\SHV

µ�¶�·s¶�¸¶X¹�º�¶¼»s½¼»¯¾�¶¿ ºP»�À ¿�Á2ÂV¿ ¸ ¿�à ¶�»�¶�¸

Ä�½ÆÅ�Ç ¿ ¸ÉÈ ¿�Ê�Á ¶È Ë ÂV¿ Ë�Ë�¶�ÌÍ�½  » È ½X¹ ¿�Á Î

Ï ¿Xà ¶Ð½�·�·s½�¸ ÃÑ¿�ÁÂ!¿ ¸ ¿�à ¶�»s¶�¸

Ï ¿�à ¶Ñ½�·¶PÒ�ºP¶  » È ½X¹Ó ¿XÁ À�¶¼·s½�¸Ë,Ô�ÕLË�À Ê ¸�º

n You can call instance and static methods dynamically using parentheses in the syntax, as is normal inABAP. Use the PARAMETER-TABLE and EXCEPTION-TABLE additions of the CALL METHODstatement to pass the actual parameters dynamically.

n The parameter table must have the attributes of the ABAP_PARMBIND_TAB table type. The tablehas three columns: NAME (for the name of the formal parameter); KIND for the way the parameteris passed (exporting, importing, changing, or receiving); VALUE with the type REF TO data (forthe value of the actual parameter).

n The way the parameter is passed is specified for each formal parameter in the declaration of thecalled method. Thus, the content of the KIND column can be initial.

n For the value of the actual parameter, the VALUE reference of the line in the table must point to thedata object containing that value. You can use the GET REFERENCE statement to achieve this.

n The exception table must have the attributes of the ABAP_EXCPBIND_TAB table type. This tablehas two columns: NAME for the name of the exception; VALUE for the value to be assigned to sy-subrc. VALUE must be of type i.

Page 420: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-10

SAP AG 2002

'\QDPLF�3URJUDPPLQJ����

)LHOG�6\PEROV�LQ�'HWDLO)LHOG�6\PEROV�LQ�'HWDLO

'\QDPLF�&DOOV'\QDPLF�&DOOV

'DWD�5HIHUHQFHV'DWD�5HIHUHQFHV

$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\

Page 421: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-11

SAP AG 2002

'DWD�5HIHUHQFH�9DULDEOHV

'$7$���UHI�7<3(�5()�72�L��������GR��7<3(�L�9$/8(����

*(7�5()(5(1&(�2)�GR�,172�UHI�

�JÖ× ¡��

�JÖ× ¡��

�2�

�2�*HW�D�UHIHUHQFH�WR�D�GDWD

REMHFW

7<3(6�UHIW\SH��^��7<3(�5()�72�W\SHBQDPH�_������������������/,.(�5()�72�GRBQDPH���_�

���7<3(�5()�72�GDWD��`�'$7$��UHI������^��7<3(�5()�72�W\SHBQDPH�_������������������/,.(�5()�72�GRBQDPH���_�

���7<3(�5()�72�GDWD��`�*(7�5()(5(1&(�2)�GDWDREMHFW�,172�UHI�

*HQHULF�W\SHDVVLJQPHQW

$Q\�FRPSOHWHO\VSHFLILHG�W\SH

n Data reference variables contain data references - that is, pointers to data objects. You use theTYPES reftype TYPE REF TO type_name statement to define a reference type to a dataobject, where type_name is any completely specified type. You can also use the generic variantTYPE 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) ora data object of the specified type.

n You work with data references using references. That is, when you access a data reference variablethe data reference itself is accessed, so that changes are made to the addresses.

n Data references are handled in ABAP like any data object with an elementary data type. This meansthat a reference variable can be defined not only as a single field, but also as the smallest indivisibleunit in a complex data object, such as a structure or an internal table.

n 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 thisvariable to get a reference to a data object that has already been declared (GET REFERENCE OFdataobject INTO ref). You can also assign an existing data reference from another datareference variable or generate a data object dynamically using it. (This will be discussed in moredetail later.)

Page 422: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-12

SAP AG 2002

([DPSOH��8VLQJ�'DWD�5HIHUHQFH�9DULDEOHV�LQ'\QDPLF�0HWKRG�&DOOV

Ø0Ù*Ú�Û�Ü0Ú�ÝJÝJÞ*ßáà�âãà�äÑåæ�ç�Ø�çÑèéäëê�à�â ØJÙ*Ú�Ûìà�â*à�ä�í*äãàJî�ïVâëð�ñJòXí�ê�à0âôóõ à�íãäãê�à�âöÞ�÷XøJÛìÞ*÷Xù0ÛúÝ*ûáä�ê�à�âüóïãý*ê�þ ØJÙ*Ú�Û ÿÆêÆîJð�ñ � óîÆý�� ØJÙ*Ú�Û��0Û*û Ø�ÝúòVà!ê�à ó� òXí!ï*à��ÆýúØJÙ*Ú�Û ÿÆêÆîJð�ñ � ååJå0å

� ÛJØ�JÛ!û�Û�JÛÆù���ÛìÝ!û � ò�íJï�à �Jý ÷�ù*ØÆÝìîÆý��üåõ àPí�ä�ê�à�â«Ü��*à����Jý��ìîÆý��ôåõ àPí�ä�ê�à�â«Ü�ñVà�ïãý����Û ��í��Æçëø�Û��Æå÷�ùãß�Û �!Ø õ àXí*ä�ê�àÆâ ÷�ù*ØÆÝ Ø�ç��*Þ�Û äëê�à�â¼åï�ý!ê�þ���� � ÛVØXí��Vç*øJÛ��0å��çãÞVÞ��*Û!Ø��JÝ�æ îXí©êÆî �"!��Ü$#�%�ï�ý*ê�þ'&Ú,ç���ç��*Û!Ø�Û��ëÜ�ØXç��JÞJÛ ä�ê�à�â å()�ã÷�Ø�Û � ò�í!ïãà���ý¼å

�VÞXç©ß0ß��!���í"�*ýÆþ�ð�!���ý æVÛVû�÷Pù�÷�Øã÷�ÝÆù¼åå0åJå�ãÛVØ �0ÝÆæãß � ý*ê�íVï�à �0ýÛ ��Ú�Ý�!Ø*÷Xù � ý�*Xí!ïãà��ÆýúØJÙ*Ú�Û ÿÆêÆîVð�ñ � åå0åJå

ÛJù0æ"�0Þ�ç«ßJß�å

�VÞPç�ßÆß��!���í"�!ýÆþëð�!���ý ÷��«Ú�Þ�Û��*Û0ù!Ø�çãØã÷�ÝÆù¼åå�å!å�ëÛJØ �0ÝÆæ � ý�ê8í*ïãà���ýüåý�*Xí!ïãà��Æý�� ï�à�0ý åÛVùÆæ��*Û!Ø�JÝ�æÑåå�å!åÛJù�æ+�ÆÞ�ç«ßJß å

�  �¬ ¡ ­ ��� � °  0±¯² ¡³ �  0´ , - ��.0/21 ,

3 � � ¬¥  ­ ¡ 46587�465

92:<;>=@?6;BA"=�C DE�F�GIHKJ8GL=�;KA"M�CNJOF�PQPBR

n In this example, the get_make method of the lcl_vehicle class is called dynamically. It hasonly one export parameter and no exceptions.

n First we define a parameter table ptab with the global table type abap_parmbind_tab from thetype group abap. The associated work area is called wa_ptab�

n We fill the work area wa_ptab with the associated values. We assign the name of the exportparameter EX_MAKE to the component name. The component value contains the reference to thecorresponding actual parameter gd_make. We also define the data reference variable ref (TYPEREF TO data) and fill the corresponding reference using GET REFERENCE. The kindcomponent of the work area need not be filled.

n We then insert this work area in the parameter table ptab using the INSERT statement. Finally weassign the name of the calling method to the data object meth. We then call the method using thesyntax already shown.

n After the method call, the actual parameter gd_make contains the passed value.

Page 423: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-13

SAP AG 2002

*HQHUDWLQJ�'DWD�2EMHFWV�DW�5XQWLPH

'$7$��UHI�������^�7<3(�5()�72�W\SHBQDPH�_�/,.(�5()�72�GRBQDPH��`�&5($7(�'$7$�UHI�

3$5$0(7(56�SDBWDE���7<3(�GG����WDEQDPH�'$7$�������UHIBLWDE�7<3(�5()�72�GDWD�

&5($7(�'$7$�UHIBLWDE�7<3(�67$1'$5'�7$%/(���������������������������2)��SDBWDE���������������������������:,7+�'()$8/7�.(<�

'$7$��UHI��������7<3(�5()�72�GDWD�&5($7(�'$7$�UHI��7<3(�^�W\SHBQDPH�_�LWDEBW\SHBGHI�`�

× ¡����*� �  0´

6WDQGDUG�WDEOH�ZLWK�VWDQGDUG�NH\DQG�OLQH�W\SH�DSSURSULDWH�IRU�WKHFRQWHQW�RI�WKH�GDWD�REMHFW�SDBWDE

ó

<RX�FDQ�DOVRDVVLJQ�D�W\SHG\QDPLFDOO\

n All data objects declared in the declaration part of a program using the appropriate statement (suchas 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 dynamicallywhile the program is executing. You can use either of the above variants to do this.

n 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 hasbeen executed. This dynamically generated data object does not have its own name; it can only beaddressed using the data reference variable. If you want to access the content of the data object, youneed to dereference the data reference first.

n In the second variant, you specify the data type of the data object you want to generate after theTYPE addition of the CREATE-DATA statement. In this case, you can specify the data typedynamically: CREATE DATA ref TYPE (dataobject). You cannot do this in other ABAPstatements. dataobject is the name of a field containing the name of the relevant data type.

n When you generate internal tables using the second variant, you must specify the table kindstatically. You can, however, specify the line type either statically or dynamically. You can alsospecify the key components either statically or dynamically (as the contents of a table containing thecomponent names). You can also specify the initial number of table lines statically or dynamically(as the contents of a variable).

Page 424: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-14

SAP AG 2002

'HUHIHUHQFLQJ�'DWD�5HIHUHQFHV

3$5$0(7(56�SDBWDE�������7<3(�GG����WDEQDPH�'$7$�������UHIBLWDE�����7<3(�5()�72�GDWD�),(/'�6<0%2/6��IVBLWDE!�7<3(�$1<�7$%/(�

&5($7(�'$7$�UHIBLWDE�7<3(�67$1'$5'�7$%/(���������������������������2)��SDBWDE���������������������������:,7+�'()$8/7�.(<�$66,*1�UHIBLWDE�! �72��IVBLWDE!�6(/(&7� �)520��SDBWDE��,172�7$%/(��IVBLWDE!�

× ¡����*� �  0´

$66,*1�UHI�! �72��IV!�>&$67,1*�����@�

�����ã� �  J´

<RX�FDQ�WKHQ�DGGUHVVWKH�FRQWHQWV�RI�WKH

LQWHUQDO�WDEOH�XVLQJ�WKHILHOG�V\PERO

n To access the contents of the data object, you need to dereference the data reference first. To accessdata 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 dataobject (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 fullygenerically typed, it adopts the type of the data object. If the field symbol is partially or completelytyped, the system checks the compatibility of the data types. You can also cast to the assigned dataobject.

n If the data reference in ref is initial or invalid, it cannot be dereferenced. In that case the fieldsymbol remains unchanged and sy-subrc is set to 4.

n If the data reference variable ref is completely typed (that is, not generically), you can use theprefix ref�! to access the contents of the data object to which ref is pointing. You can write thisexpression in any operand position. If the data reference is typed, you can also address thecomponents 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.

Page 425: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-15

SAP AG 2002

([DPSOH��*HQHUDWLQJ�'DWD�2EMHFWV�DW�5XQWLPH

3$5$0(7(56�SDBGEWDE�7<3(�GG��O�WDEQDPH�'()$8/7�6)/,*+7�'$7$�GBUHI�7<3(�5()�72�GDWD�),(/'�6<0%2/6���IVBZD!�7<3(�$1<���IVBFRPS!�7<3(�$1<�67$57�2)�6(/(&7,21��&5($7(�'$7$�GBUHI�7<3(��SDBGEWDE���$66,*1�GBUHI�! �72��IVBZD!��6(/(&7� �)520��SDBGEWDE��,172��IVBZD!���'2����$66,*1�&20321(17�V\�LQGH[�2)�6758&785(��IVBZD!�72��IVBFRPS!����,)�V\�VXEUF�1(�������6.,3�����(;,7����(1',)����:5,7(��IVBFRPS!���(1''2��(1'6(/(&7�

n This example displays the content of a transparent table. You can make the FROM clause of theSELECT statement dynamic. For the INTO clause, you will need a data object that has a line typecompatible with that of the table being displayed. Since the name - and thus the line type of the tableis not known until runtime, you should not create the data object until then.

n Unlike conventional data objects, you can specify the type of a data object FUHDWHG�DW�UXQWLPHdynamically. The TYPE addition of the CREATE DATA statement contains the name of the table, sothat the system creates the appropriate structure.

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

n You can now write each data record from the SELECT statement into the compatibly-typed dataobject using the field symbol <fs_wa>.

n 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 thereare. For this reason, you must display the components using the ASSIGN-COMPONENT variant: Thecomponents of the structure �IVBZD are assigned one-by-one to the field symbol �IVBFRPS! andthen displayed. When the loop runs out of components, the program reads the next data record.

Page 426: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-16

SAP AG 2002

)LHOG�6\PEROV�LQ�'HWDLO)LHOG�6\PEROV�LQ�'HWDLO

'\QDPLF�&DOOV'\QDPLF�&DOOV

$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\$VFHUWDLQLQJ�'DWD�$WWULEXWHV�'\QDPLFDOO\

'\QDPLF�3URJUDPPLQJ����

'DWD�5HIHUHQFHV'DWD�5HIHUHQFHV

Page 427: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-17

SAP AG 2002

$VFHUWDLQLQJ�,QWHUQDO�7DEOH�$WWULEXWHV

'$7$��QRBRIBOLQHV���7<3(�L�������LQLWLDOBOLQHV�7<3(�L�������WDEOHBNLQG����7<3(�F��'(6&5,%(�7$%/(�LWDE���/,1(6��QRBRIBOLQHV���2&&856�LQLWLDOBOLQHV���.,1'���WDEOHBNLQG� �RWKHU�YDULDQW�QRBRIBOLQHV� �OLQHV��LWDE���

7<3(�322/��V\GHV�•�V\GHVBNLQG�XQGHILQHG•�V\GHVBNLQG�VWDQGDUG•�V\GHVBNLQG�VRUWHG•�V\GHVBNLQG�KDVKHG

WDEOHBNLQG

1XPEHU�RI�OLQHV�LQWKH�LQWHUQDO�WDEOH

1XPEHU�RI�OLQHVUHVHUYHG�ZKHQ�WDEOHZDV�ILUVW�FUHDWHG

7DEOH�NLQG

&RQVWDQWV�XVHGWR�HYDOXDWH�WKHUHWXUQ�YDOXH

n The DESCRIBE TABLE statement allows you to obtain the following information about an internaltable:

l The number of lines:After LINES, you must enter a type L variable (here no_of_lines)

l The number of table lines initially reserved:After OCCURS, you must enter a type L variable(here: initial_lines)

l The table kind:After KIND, you must enter a type F variable. The runtime system fills the variable with aconstant defined in the type group SYDES - SYDES_KIND-UNDEFINED, SYDES_KIND-STANDARD, SYDES_KIND-SORTED, or SYDES_KIND-HASHED.

n Since SAP R/3 Basis Release 6.10, there is also a built-in function, lines.

n 1RWH�You can use the information about the number of lines in an internal table to query at runtimewhether an internal table has any entries in it at all. Alternatively, use the IS INITIAL query. Thegenerically-typed interface parameters of subroutines, function modules, and methods can use theinformation on the table kind.

Page 428: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-18

SAP AG 2002

'\QDPLF�7\SH�'HVFULSWLRQV�IRU�577,�&ODVVHV

+LHUDUFK\�RI�GHVFULSWLRQ�FODVVHV�S 7 ��/2T+/VU� WXYU ,+Z[,�\ S^]S 7 � /2T�/8U�� W X[U ,+Z_,�\ S^]

S 7 ��/2T+/VU� 5 T)` , S_W Z6,"\ S2]S 7 �/VT�/VU� 5 T)` , SaW Za,�\ S2]

S 7 �/VT"/8U���S 5 .bU 7 , -YZa,�\ S2]S 7 ��/2T+/VU���S 5 .bU 7 ,-^Za,�\ S0]

S 7 ��/2T+/VU� Z /^W/ Z[, \ SY]S 7 �/VT�/VU� Z /^W/ Z[,�\ S^]

S 7 �/VT"/8U���S 7 / \)\_Z_,�\ S2]S 7 ��/2T+/VU���S 7 / \+\[Za,�\ S^]

S 7 �/VT"/8U�� , 7 , . Z_,�\ SY]S 7 ��/2T+/VU�� , 7 , . Z[, \ SY]

S 7 ��/VT+/2U��W/VT 7 ,)Z_,�\ S^]S 7 ��/8T"/8U�� W�/2T 7 ,+Z[,�\ S0]

S 7 �/VT"/8U��"cQd'W+e Z6,�\ S0]S 7 ��/2T+/VU���cQd)W)e Za,�\ S2]

S 7 �/VT"/8U���] , e Za,�\ S0]S 7 ��/2T+/VU��+] , e Za,�\ S2]

S 7 ��/VT+/2U� \ W']af)S_W Za,�\ S2]S 7 ��/8T"/8U�� \ W+][f)SaW Za,�\ S^]

l 3DVV�WKH�GDWD�REMHFW�WR�WKHVWDWLF�PHWKRG'(6&5,%(B%<B'$7$�RI�WKHFODVV�&/B$%$3B7<3('(6&5

l *HW�WKH�UHIHUHQFH�WR�WKHDSSURSULDWH�GHVFULSWLRQ�REMHFW

l (YDOXDWH�WKH�SXEOLF�DWWULEXWHVRI�WKLV�REMHFW

g�h"i�j�k ¦mlã¤on«¤onqp�¦k§ j ¦mr�s j lã¦k¦ k ¤ot«ª©¦ j�h ¨qualv l*¦wl ¨6t@x j�i ¦ql㦠k ªqn«¦�¤Ky j"z

5XOHV�

n Since the introduction of ABAP Objects, there is now a system called the RTTI concept (Run TimeType Information) that you can use to find out type attributes at runtime. It is based on systemclasses. The concept includes all ABAP types, and so covers all of the functions of the statementsDESCRIBE FIELD and DESCRIBE TABLE.

n There is a description class for each type with special attributes for special type attributes. The classhierarchy of the description classes corresponds to the hierarchy of the types in the ABAP typesystem. In addition, the description classes for complex types, references, classes, and interfaceshave special methods used to specify references to sub-types. Using these methods, you can navigatethrough a compound type to all its sub-types.

n To obtain a reference to a description object of a type, you must use the static methods of the classCL_ABAP_TYPEDESCR or the navigation methods of the special description class. The descriptionobjects are then created from one of the subclasses. At runtime, exactly one description object existsfor each type. The attributes of the description object contain information on the attributes of thetype.

Page 429: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-19

SAP AG 2002

'HFODULQJ�7\SHV�'\QDPLFDOO\��([DPSOH

����'$7$���GHVFUBUHI�7<3(�5()�72�FOBDEDSBVWUXFWGHVFU���ZDBFRPS���7<3(�DEDSBFRPSGHVFU�����

67$57�2)�6(/(&7,21������ �JHW�UHIHUHQFH�WR�W\SH�GHVFULSLRQ�REMHFW�E\�ZLGHQLQJ�FDVW���GHVFUBUHI�" �FOBDEDSBW\SHGHVFU !GHVFULEHBE\BGDWD���IVBZD!��������

723�2)�3$*(���/223�$7�GHVFUBUHI�!FRPSRQHQWV�,172�ZDBFRPS�����:5,7(�ZDBFRPS�QDPH���(1'/223�

n We can now enhance the example of dynamic type declarations so that the system also displays thecolumn names of the transparent table in the list.

n Since we need the attributes of a structure, we first define a reference to the appropriate descriptionclass. Instances of this class possess a COMPONENTS attribute, which you use to describe theindividual components of the relevant structure. This attribute is an internal table. Therefore you alsoneed to define a work area with a compatible line type.

n The (functional) method call returns the reference to the description instance of the structure. (Thesystem 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. ItsRETURNING parameter is typed as a reference to this superclass. However, since the actualparameter descr_ref has the type of the subclass CL_ABAP_STRUCTDESCR, we need to assignthe object using a (widening) cast.

n You can then access the attributes of the description instance in any form. In this example, thecomponent names are displayed as the column headers. (We have omitted the formatting options forthe sake of clarity.)

n For more information and syntax examples, refer to the online documentation, either under thekeyword 577, or the class CL_ABAP_TYPEDESCR.

Page 430: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-20

SAP AG 2002

l 8VH�G\QDPLF�DWWULEXWHV�LQ�VXLWDEOH�$%$3VWDWHPHQWV

l 8VH�GDWD�REMHFWV�JHQHUDWHG�DW�UXQWLPHl $VFHUWDLQ�WKH�DWWULEXWHV�RI�GDWD�REMHFWV�DW�UXQWLPH

<RX�DUH�QRZ�DEOH�WR�

'\QDPLF�3URJUDPPLQJ��8QLW�6XPPDU\

Page 431: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-21

2SWLRQDO8QLW� '\QDPLF�3URJUDPPLQJ7RSLF��&DVWLQJ�7\SHV

At the conclusion of these exercises, you will be able to:

• Use field symbols to cast types

• Perform calculations on dates

Change your program so that the default value for the key date iscalculated differently. It should be the first day of the following month.

3URJUDP� =%&���B��B&$67,1*7HPSODWH� 6$3%&���B7$%6B352&(66B'$7$0RGHO�VROXWLRQ� 6$3%&���B'<16B&$67,1*

1-1 Copy your solution to the last exercise from the unit 8VLQJ�,QWHUQDO�7DEOHV=%&���B��B352&(66B'$7$ or the corresponding model solution6$3%&���B7$%6B352&(66B'$7$ and give it the new name=%&���B��B&$67,1*.

1-2 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!).

1-3 In the /2$'�2)�352*5$0 event block:Copy today’s 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 thepseudo-components for the year, month, and day, using the field symbol.

Page 432: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-22

6FUHHQ�([HUFLVH��8QLW� '\QDPLF�3URJUDPPLQJ7RSLF� '\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 GridControl instance, which you should also use to display the data.The purpose of this program is to make you more familiar with dynamicprogramming techniques. (There is already a 'DWD�%URZVHU, installed as astandard tool in the ABAP Workbench.)

3URJUDP� =%&���B��B&5($7(B'$7$B64/7HPSODWH� SAPBC401_DYN7_CREATE_DATA_SQL0RGHO�VROXWLRQ� SAPBC401_DYN6_CREATE_DATA_SQL

2-1 Copy your program, SAPBC401_DYN7_CREATE_DATA_SQL, giving it the name=%&���B��B&5($7(B'$7$B64/.

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 aninternal table whose line type is compatible with the selected transparent table. Theformer should then be filled with the complete contents of the latter using adynamic SQL statement. The internal table filled in this way should then be passedto a SAP Grid Control instance.

2-3 Define a reference for the internal table that will be generated (we suggest the nameUHIBLWDE).

2-4 At program runtime, generate the internal table. At this time, it is known which linetype must be used.The table must be a standard table with a non-unique standard key.

Page 433: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-23

2-5 Assign a generically-typed field symbol to the generated internal table (suggestedname: �IVBLWDE!), so that its contents can be accessed.

2-6 Before calling the screen, insert an Open SQL statement that copies the completecontents 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 havecreated, ,1,7B&21752/6B����. Again, if an error occurs, make sure theprogram 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 called6(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$%.

Page 434: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-24

6ROXWLRQ�����2SWLRQDO8QLW� '\QDPLF�3URJUDPPLQJ7RSLF� &DVWLQJ�7\SHV

5(3257�VDSEF���BG\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�����PRQWK����7<3(�Q�����GD\������7<3(�Q���(1'�2)�VWBGDWH�

CONSTANTS c_number TYPE i VALUE 30.

DATA:

datastring TYPE string,

set_string TYPE string,

wa_flight_c TYPE st_flight_c,

wa_flight TYPE st_flight.

Page 435: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-25

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���$66,*1�SDBGDWH�72��IVBGDWH!�&$67,1*�

���IVBGDWH!�GD\� ������,)��IVBGDWH!�PRQWK�����������IVBGDWH!�PRQWK� ��IVBGDWH!�PRQWK�������(/6(������IVBGDWH!�PRQWK� ���������IVBGDWH!�\HDU� ��IVBGDWH!�\HDU�������(1',)�

AT SELECTION-SCREEN.

IF pa_date < sy-datum.

MESSAGE e085(bc401). " date in the past

ENDIF.

START-OF-SELECTION.

...

Page 436: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-26

6ROXWLRQ��8QLW� '\QDPLF�3URJUDPPLQJ7RSLF� '\QDPLF�2SHQ�64/�6WDWHPHQWV�DQG�*HQHUDWH'DWD�2EMHFWV�DW�5XQWLPH

2-1 The copy template displays a selection screen where you can enter the name of atransparent 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 theprogram.

This runtime behavior is implemented using standard ABAP programmingtechniques. A docking container control instance that fills the whole screen hasalready been generated and attached to the screen.

2 0RGHO�VROXWLRQ� 6$3%&���B'<16B&5($7(B'$7$B64/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’.

Page 437: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-27

START-OF-SELECTION.

��&5($7(�'$7$�UHIBLWDE�7<3(�67$1'$5'�7$%/(�2)��SDBWDE������������������������:,7+�121�81,48(�'()$8/7�.(<���$66,*1�UHIBLWDE�! �72��IVBLWDE!���6(/(&7� �)520��SDBWDE������������,172�7$%/(��IVBLWDE!���,)�V\�VXEUF��!�������0(66$*(�D����UIZ��:,7+�WH[W�QGW���(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.

CREATE OBJECT ref_docking EXPORTING* SIDE = DOCK_AT_LEFT extension = 2000 EXCEPTIONS OTHERS = 6 . IF sy-subrc <> 0. MESSAGE a015(rfw). ENDIF.

����&5($7(�2%-(&7�UHIBDOY������(;3257,1*��������LBSDUHQW���������� �UHIBGRFNLQJ������(;&(37,216��������HUURUBFQWOBFUHDWH� ����������HUURUBFQWOBLQLW��� ����������HUURUBFQWOBOLQN��� ����������HUURUBGSBFUHDWH��� ����������27+(56������������ �����������

Page 438: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-28

����,)�V\�VXEUF��!���������0(66$*(�D����UIZ��:,7+�WH[W�DHU�����(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$*(�D����UIZ������(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

Page 439: 003 - Bc401 - Abap Objects

© SAP AG BC401 15-29

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