View
240
Download
0
Category
Preview:
Citation preview
8/12/2019 TAW 10 (2-2) - Objects
1/303
0
SAP AG 2002
Classification of objects
UML
Class diagrams
Sequence diagrams
Contents:
Analysis and Design
SAP AG TAW10 5-1
8/12/2019 TAW 10 (2-2) - Objects
2/303
0.2
SAP AG 2002
List some important diagram types in UML
Create a class diagram
Create a sequence diagram
At the conclusion of this unit, you ill be able to:
Analysis and Design: Unit !bjecti"es
SAP AG TAW10 5-2
8/12/2019 TAW 10 (2-2) - Objects
3/303
0.3
SAP AG 2002
Analysis and Design #$%
ClassificationClassification
UML & the modeling standardUML & the modeling standard
SAP AG TAW10 5-3
8/12/2019 TAW 10 (2-2) - Objects
4/303
0.4
SAP AG 2002
Classification of !bjects
Real world
lcl'"ehicle lcl'people
Class creation / modeling
In the real world, there are objects, such as ar!ous a!r"lanes , cars, and "eo"le# So$e o% these
objects are er& s!$!lar, that !s, the& can be descr!bed us!n' the sa$e attr!butes or character!st!cs and
"ro!de the sa$e %unct!ons#
S!$!lar objects are 'rou"ed to'ether !n classes# (ach class !s descr!bed once, and each object !s then
created !n accordance w!th th!s blue"r!nt#
A class !s there%ore a descr!"t!on o% a )uant!t& o% objects character!*ed b& the sa$e structure and the
sa$e beha!or# +or ea$"le, the eh!cle $a.e , ### ser!al nu$ber !s an object o% the class
eh!cle# Th!s object !s there%ore a concrete !nstance o% the class#
SAP AG TAW10 5-/
8/12/2019 TAW 10 (2-2) - Objects
5/303
0.5
SAP AG 2002
Different Abstractions of a Class
Car enthusiast Scrap merchant
Ma(ema(e
)ypemodel
*S
Doorsdoors
Colorcolor
Equippingequipping
Weightweight
+heelsheels
Scrap valuescrapvalue
Priceprice
lcl'"ehicle
& ma(e
&
- price
- equipping
- color
lcl'"ehicle
& ma(e
&
- weight
- scrapvalue
In th!s contet, abstract!ons are a s!$"l!%!ed re"resentat!ons o% co$"le relat!onsh!"s !n the real
world# An actuall& e!st!n' object !s abstracted to the s!'n!%!cant d!$ens!ons that are to be $a""ed#
Ins!'n!%!cant deta!ls are le%t out !n order to a!d understand!n' o% the oerall s&ste$#
Th!s ea$"le concerns cars# So%tware %or a car enthus!ast and so%tware %or a scra" $erchant conta!n
d!%%erent abstract!ons classes %or these objects#
A class can conta!n er& d!%%erent objects de"end!n' on the abstraction#
Wh!le !n one so%tware s&ste$ the classlcl_vehicledescr!bes all trans"ort eh!cles !nclud!n'
b!.es, !n another s&ste$ !t descr!bes all $otor!*ed $ach!nes w!th wheels#
oth classes hae the sa$e na$e but descr!be d!%%erent objects#
In our ea$"les, the class lcl_vehicledescr!bes all $otor!*ed eh!cles w!th / wheels#
SAP AG TAW10 5-5
8/12/2019 TAW 10 (2-2) - Objects
6/303
0.6
SAP AG 2002
-epresentation of a Class
lcl'"ehicle
ma(e
modell
price
color
set'ma(e
display'attributes
increase'speed
Attributes
Class name
Methods!unctions"
Assoziationsme
Optional, does
not have to be
specified
4 notat!on6
A class !s re"resented w!th !ts na$e, attr!butes, and $ethods# W!th 4, &ou also hae the o"t!on o%
o$!tt!n' e!ther the attr!bute or the $ethod "art#
Attr!butes descr!be the data that can be stored !n the objects !n a class#
The& de%!ne the state o% an object#
4ethods descr!be the %unct!ons that can be eecuted on the data o% the object#
The& conta!n the eecutable source code and there%ore also re"resent an object7s beha!or#
AAP 8bjects eents are not !ncluded !n class d!a'ra$s#
SAP AG TAW10 5-9
8/12/2019 TAW 10 (2-2) - Objects
7/303
0.7
SAP AG 2002
Classes and !bjects
lcl'"ehicle
Class:
.eneral description of objects
#blueprint%
Specifies status data #attributes%
and beha"ior #methods%
!bject #instance%: -epresentation of real orld
Specific instance of a class
Represented in course b#$
lcl'"ehicle
(5) lcl'"ehicle
or
or
A class!s a set o% objects that hae the sa$e structure and the sa$e beha!or# A class !s there%ore
l!.e a blue"r!nt, !n accordance w!th wh!ch all objects !n that class are created#
(er& objecthas an !dent!t&, a status set o% attr!bute alues, and beha!or set o% $ethods and
eents# The structure and beha!or o% s!$!lar objects are de%!ned !n the!r shared class#
Ident!t& !s an attr!bute that d!st!n'u!shes each object %ro$ all other objects# Ident!t& !s o%ten con%used
w!th ha!n' the sa$e attr!bute alues or w!th a un!)ue na$e# Two different objects can have
identical attribute values and still not be identical.
(a$"le6
Two co%%ee cu"s are the sa$e he!'ht and d!a$eter, hae the sa$e handle and are both wh!te#
Althou'h the& loo. eactl& the sa$e, the& are st!ll two se"arate cu"s#
In Ger$an l!terature on the subject o% object or!entat!on, we o%ten s"ea. o% !nstances# In objector!entat!on an !nstance !s s!$"l& an object# Another s&non&$ o% object!s instance#
SAP AG TAW10 5-:
8/12/2019 TAW 10 (2-2) - Objects
8/303
0.8
SAP AG 2002
Analysis and Design #/%
ClassificationClassification
UML & the modeling standardUML & the modeling standard
SAP AG TAW10 5-;
8/12/2019 TAW 10 (2-2) - Objects
9/303
0.9
SAP AG 2002
UML stands for Unified Modeling Language
0t is a global standard
0t is a language and form of notation for the specification,
construction, "isuali1ation, and documentation of models
for softare systems
Diagram types:
Class diagram
2eha"ior diagram, such as a sequence diagram
Component diagram
Distribution diagram
Modeling ith UML
4 n!%!ed 4odel!n' an'ua'e !s a standard!*ed $odel!n' lan'ua'e# It !s used %or the
s"ec!%!cat!on, construct!on, !sual!*at!on, and docu$entat!on o% $odels %or so%tware s&ste$s and
enables un!%or$ co$$un!cat!on between ar!ous users# 4 does not descr!be the ste"s !n the
object-or!ented deelo"$ent "rocess#
4 !s an !ndustr& standard and has been standard!*ed b& the 84G 8bject 4ana'e$ent Grou"
s!nce Se"te$ber 1
8/12/2019 TAW 10 (2-2) - Objects
10/303
0.#0
SAP AG 2002
34ample of a Class Diagram
lcl'rental
lcl'"ehicle
lcl'car lcl'truc( lcl'bus
lcl'boo(ing lcl'customer
lcl'heel
10..*0..*1
1
1..*1
1..*
A class d!a'ra$ descr!bes the ele$ents conta!ned !n the $odel and the!r ar!ous stat!c relat!onsh!"s#
There are two bas!c %or$s o% stat!c relat!onsh!"s6
Assoc!at!ons %or ea$"le, a car rental custo$er boo.s a car
General!*at!on ? s"ec!al!*at!on %or ea$"le, a car and a bus are both eh!cles
In class d!a'ra$s, classes can also be shown w!th the!r attr!butes and $ethods#
SAP AG TAW10 5-10
8/12/2019 TAW 10 (2-2) - Objects
11/303
0.##
SAP AG 2002
Association
A customer can rent
several cars
Common cardinalities
books
Association name
* or 0..* an!
1 "#actl! one
1..* One or more
0..1 $ero or one
10..*lcl'boo(ing lcl'customer
A boo%in& refers to
a customer
An assoc!at!on descr!bes a se$ant!c relat!onsh!" between classes# The s"ec!%!c relat!onsh!" between
objects !n these classes !s .nown as an object l!n.# 8bject l!n.s are there%ore the !nstances o% an
assoc!at!on#
An assoc!at!on !s usuall& a relat!onsh!" between d!%%erent classes# Boweer, an assoc!at!on can also
be recurs!eC !n th!s case, the class would hae a relat!onsh!" w!th !tsel%# In $ost cases, recurs!e
assoc!at!ons are used to l!n. two d!%%erent objects !n one class#
The "o!nts below assu$e that the assoc!at!ons are b!nar
(ach assoc!at!on has two roles, one %or each d!rect!on o% the assoc!at!on boo.!n' custo$er,
custo$erboo.!n'# Doles can hae na$es %or ea$"le, the assoc!at!on car reserat!on could
be called reserat!on#
(ach role has a card!nal!t& that shows how $an& !nstances "art!c!"ate !n th!s relat!onsh!"# The$ult!"l!c!t& !s the nu$ber o% "art!c!"at!n' objects !n one class that hae a relat!onsh!" to an object !n
the other class#
4 notat!on6
An assoc!at!on !s re"resented b& a l!ne between the class s&$bols
The card!nal!t& o% the relat!onsh!" can be shown at each end o% the l!ne
Assoc!at!ons can be '!en a na$e %or ease o% !dent!%!cat!on a erb or a short tet# Th!s na$e !s
wr!tten !n !tal!cs aboe the l!ne and $a& hae an arrow to show the read d!rect!on# oth are
o"t!onal#
SAP AG TAW10 5-11
8/12/2019 TAW 10 (2-2) - Objects
12/303
0.#2
SAP AG 2002
Aggregation and Composition
Aggregation is a special
case of association, a
hole&part relationship
Composition is a specialcase of aggregation, an
e4istence&dependent
hole&part relationship
'omposition s!mbol
A&&re&ation s!mbol
lcl'rental lcl'boo(ing1..*
1
lcl'"ehicle lcl'heel1
0..*
A''re'at!on !s a s"ec!al .!nd o% assoc!at!on# A''re'at!on descr!bes one object that conta!ns another
or cons!sts o% other objects whole-"art# A eh!cle cons!sts o% wheels# The relat!onsh!" can be
descr!bed b& the words cons!sts o% or !s a "art o%#
4 notat!on %or a''re'at!on6
An a''re'at!on, l!.e an assoc!at!on, !s re"resented b& a l!ne between two classes, wh!ch then
add!t!onall& has a s$all rho$bus at one end# The rho$bus !s alwa&s at the a''re'ate end, that !s, the
whole object end# 8therw!se the notat!on conent!ons are the sa$e as %or assoc!at!ons#
@o$"os!t!on !s a s"ec!al .!nd o% a''re'at!on# @o$"os!t!on descr!bes the %act that the object
conta!ned cannot e!st w!thout the a''re'ate %or ea$"le, a car reserat!on cannot e!st w!thout the
car rental#
!%%erences to a''re'at!on6The card!nal!t& on the a''re'ate s!de can onl& be one# (ach "art !s onl& "art o% one co$"os!te object,
otherw!se the e!stence de"endenc& would be contrad!ctor The l!%et!$e o% the !nd!!dual "arts !s
l!n.ed to the l!%et!$e o% the a''re'ate6 Parts are created e!ther w!th or !$$ed!atel& a%ter the
a''re'ate, and the& are destro&ed e!ther w!th or !$$ed!atel& be%ore the a''re'ate#
4 notat!on %or co$"os!t!on6
!.e a''re'at!on, co$"os!t!on !s shown as a l!ne between two classes and $ar.ed w!th a s$all
rho$bus on the a''re'ate s!de# Boweer, !n contrast to a''re'at!on, the rho$bus !s %!lled !n#
SAP AG TAW10 5-12
8/12/2019 TAW 10 (2-2) - Objects
13/303
0.#3
SAP AG 2002
.enerali1ation and Speciali1ation
or
Generalization
Specialization
Generalization
Specialization
lcl'"ehicle
lcl'car lcl'truc(
lcl'"ehicle
lcl'car lcl'truc(
(nheritance arro)
4 notat!on6
General!*at!on and s"ec!al!*at!on are denoted b& tr!an'ular arrows that "o!nt %ro$ the subord!nate
class to the su"erclass#
Seeral arrows can be co$b!ned !nto a tree#
SAP AG TAW10 5-13
8/12/2019 TAW 10 (2-2) - Objects
14/303
0.#4
SAP AG 2002
Sequence Diagrams: 34ample
ife line
of ob+ect
'ontrol focus
Return value
1:Method (parameter)
ime
lcl'dri"er lcl'car
Process
description
(optional)
Se)uence d!a'ra$s, unl!.e class d!a'ra$s, show the d&na$!cs between objects# The& are used to
re"resent a "art!cular "rocess or a "art!cular s!tuat!on# Se)uence d!a'ra$s %ocus on the t!$e se)uence
o% the !n%or$at!on echan'e6
a @reat!on and delet!on o% objects
b 4essa'e echan'e between objects
Se)uence d!a'ra$s hae no notat!on %or re"resent!n' stat!c $ethods#
The object l!%e l!ne !s re"resented b& ert!cal dotted l!nes#
The control %ocus !s shown as a ert!cal rectan'le on the object l!%e l!ne# The control %ocus shows the
object7s act!e "er!od6
An object !s act!e when act!ons are eecuted
An object !s !nd!rectl& act!e !% !t !s wa!t!n' %or a subord!nate "rocedure to end# 4essa'es are shown as hor!*ontal arrows between the object l!nes# The $essa'e !s wr!tten aboe the
arrow !n the %or$ method (parameter)# There are ar!ous wa&s o% re"resent!n' the re"l&C !n th!s
ea$"le, the arrow !s shown as a return!n' arrow#
>ou can also !nclude a descr!"t!on o% the "rocess and add co$$ents to the object l!%e l!ne as
re)u!red#
SAP AG TAW10 5-1/
8/12/2019 TAW 10 (2-2) - Objects
15/303
0.#5
SAP AG 2002
1: get_fuel_level( )
2: get_fuel_level( )
re_level
re_level
lcl'dri"er lcl'car lcl'tan(
re_level =fuel / fuel_max * 100.
re_level =tank->get_fuel_level( ).
Sequence Diagram: Delegation
In dele'at!on, two objects are !noled !n handl!n' a re)uest6 The rec!"!ent o% the re)uest dele'ates
the eecut!on o% the re)uest to a dele'ate#
(a$"le6
The dr!er lcl_driver calls the $ethod get_fuel_level%or the class car lcl_car# The
car cannot carr& out th!s tas. !tsel%# There%ore, the car calls the get_fuel_level$ethod %or the
class lcl_tank, that !s the car deleatesthe eecut!on o% the $ethod to the tan.#
The $a!n adanta'e o% dele'at!on as a re-use $echan!s$ l!es !n the o"t!on o% chan'!n' the
beha!or o% the rec!"!ent b& subst!tut!n' the dele'ate at runt!$e# +or ea$"le, dele'at!on enables
the car to be e)u!""ed w!th a new tan., w!thout the call chan'!n' %or the cl!ent or %or the car class#
Good enca"sulat!on o%ten %orces the use o% dele'at!on6 I% tank!n the aboe ea$"le were a "r!ate
attr!bute o% the class lcl_car, the user could not address the tan. d!rectl&, but onl& throu'h the car#
SAP AG TAW10 5-15
8/12/2019 TAW 10 (2-2) - Objects
16/303
0.#6
SAP AG 2002
List some diagram types in UML
Create a class diagram
Create a sequence diagram
5ou are no able to:
Analysis and Design: Summary
SAP AG TAW10 5-19
8/12/2019 TAW 10 (2-2) - Objects
17/303
0.#7 $nal%sis and &esin '(ercises
)nit* $nal%sis and &esin
To+ic* ),- Class &iara!s
At the conclus!on o% these eerc!ses, &ou w!ll be able to6
@reate a 4 class d!a'ra$
An a!rl!ne needs to $ana'e !ts a!r"lanes#
1-1 se a "enc!l and "a"er to create a 4 class d!a'ra$ that conta!ns the %ollow!n'
classes6
- A!rl!ne6 lcl_carrier
E A!r"lane 'eneral6 lcl_airplane
E Passen'er a!r"lane6 lcl_passenger_plane
E @ar'o "lane6 lcl_cargo_plane
1-1-1 Include so$e a""ro"r!ate attr!butes and $ethods %or eer& class#
1-1-2 raw l!nes to re"resent the relat!onsh!"s between the classes and !nd!cate
"oss!ble card!nal!t!es#
SAP AG TAW10 5-1:
8/12/2019 TAW 10 (2-2) - Objects
18/303
0.#8 $nal%sis and &esin olutions
)nit* $nal%sis and &esin
To+ic* ),- Class &iara!s
SAP AG TAW10 5-1;
8/12/2019 TAW 10 (2-2) - Objects
19/303
#
SAP AG 2002
Classes
!bjects
Attributes
Methods
6isibility7encapsulation
0nstantiation Constructor
.arbage collector
Contents:
*rinciples of !bject&!riented *rogramming
SAP AG TAW10 9-1
8/12/2019 TAW 10 (2-2) - Objects
20/303
#.2
SAP AG 2002
Create classes
Create objects
Call methods
34plain ho the garbage collector or(s
At the conclusion of this unit, you ill be able to:
*rinciples of !bject&!riented *rogramming: Unit!bjecti"es
SAP AG TAW10 9-2
8/12/2019 TAW 10 (2-2) - Objects
21/303
#.3
SAP AG 2002
*rinciples #$%
Classes, attributes, and methodsClasses, attributes, and methods
!bjects, instances of classes!bjects, instances of classes
Accessing attributes and methodsAccessing attributes and methods
)he constructor)he constructor
Additional principlesAdditional principles
SAP AG TAW10 9-3
8/12/2019 TAW 10 (2-2) - Objects
22/303
#.4
SAP AG 2002
34ample of a Class
lcl'"ehicle
*ri"ate
components
Methods:
(mplementation
*ublic
components
*ri"ate access
- "ncapsulation- .ormall! attributes
*ublic access
- (nterface
- .ormall! methods, events
ma(e
modelset'ma(e
get'ma(e
Represented in
course b#
In the 'ra"h!c, the "ubl!c co$"onent o% the class !s accessed us!n' the 'reen node or start!n' "o!nt
d!s"la&ed on the le%t# A user or cl!ent can use th!s node to access the "ubl!c co$"onents and hence
also !nd!rectl& access the "r!ate co$"onents# Boweer, the "r!ate co$"onents o% the class cannot
be addressed d!rectl& # The& are not !s!ble to the outs!de user#
Wh& are the "r!ate co$"onents o% a class h!ddenF
Th!s "r!nc!"le !s called infor!ation hidinor enca+sulationand !s used to "rotect the user#
et us assu$e that a class chan'es !ts "r!ate co$"onents, wh!le !ts !nter%ace re$a!ns unchan'ed#
An& user who s!$"l& needs to access the !nter%ace o% the class can carr& on wor.!n' w!th the class as
usual# The user does not not!ce the chan'e#
Boweer, !% an class chan'es !ts "ubl!c co$"onents, then an& user who accesses these "ubl!c
co$"onents $ust ta.e these chan'es !nto account#
SAP AG TAW10 9-/
8/12/2019 TAW 10 (2-2) - Objects
23/303
#.5
SAP AG 2002
Defining Classes
CLASS ll_ve!"le #$%&'&&'.
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'.
$'#CLASS.
lcl'"ehicle
*ri"ate
components
Methodimplementation
*ubliccomponents
A%AP code
A class!s a set o% objects that hae the sa$e structure and the sa$e beha!or# A class !s there%ore
l!.e a blue+rint, !n accordance w!th wh!ch all objects !n that class are created#
The co$"onents o% the class are de%!ned !n the de%!n!t!on "art# The co$"onents are attr!butes,
$ethods, eents, constants, t&"es, and !$"le$ented !nter%aces# 8nl& $ethods are !$"le$ented !n the
!$"le$entat!on "art#
The @ASS state$ent cannot be nested, that !s, &ou cannot de%!ne a class w!th!n a class#
SAP AG TAW10 9-5
8/12/2019 TAW 10 (2-2) - Objects
24/303
#.6
SAP AG 2002
Attributes
Attributes can ha"e any (ind of
data type:
C, 8, 0, *, , S)-08.
Dictionary types
User&defined types
)5*3 -39 )!
defines a reference to an
object, in this case r'car;
(5) lcl'car
PrivatePublic
make TYPE STRING,
...
r_motor
TYPE REF TO ...
(3) lcl'rental
r&car
Attr!butes descr!be the data that can be stored !n the objects o% a class#
@lass attr!butes can be o% an& t&"e6
ata t&"es6 scalar %or ea$"le data ele$ent, structured, !n tables
AAP ele$entar& t&"es @, I, ###
8bject re%erences
Inter%ace re%erences
(a$"les o% attr!butes %or the class lcl_carare6
$a.e car $a.e
$odell t&"e , $odel
serno ser!al nu$ber
color colorcart&"e estate, conert!ble, ###
$aseats nu$ber o% seats
r$otor re%erence to class lcl_motor
###
SAP AG TAW10 9-9
8/12/2019 TAW 10 (2-2) - Objects
25/303
#.7
SAP AG 2002
Attributes, )ypes, and Constants: Synta4
CLASS ,laname> #$%&'&&'....
+$S: ,nrmale ef"n"t"n>.C'SA'S: ntant +$ ,te> 3AL4$ ,value>.
#AA: var"a5le1 +$ ,te>6var"a5le2 +$ ,"_te>6var"a5le7 L&8$ var"a5le16var"a5le9 +$ ,te> 3AL4$ ,value>6var"a5le +$ ,te> ;$A#-'L6
var"a5le< +$ ;$% ,laname>6var"a5le +$ ;$% ,"nterfae>.
CLASS-#AA: nl_ne +$ ... .
$'#CLASS.
In classes, &ou can onl& use the TYPEadd!t!on to re%er to data t&"es#
>ou can onl& use the LIKEre%erence %or local data objects#
The READ-ONLYadd!t!on $eans that a "ubl!c attr!bute declared w!th DATAcan be read %ro$
outs!de, but can onl& be chan'ed b& $ethods o% the class#
>ou can currentl& onl& use the READ-ONLYadd!t!on !n the "ubl!c !s!b!l!t& sect!on P!LI"
#E"TION o% a class declarat!on or !n an !nter%ace de%!n!t!on#
SAP AG TAW10 9-:
8/12/2019 TAW 10 (2-2) - Objects
26/303
#.8
SAP AG 2002
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.$$$
+;&3A$ S$C&'.#AA: make +$ tr"ng.
$'#CLASS.
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.#AA: make +$ tr"ng.
+;&3A$ S$C&'.
$'#CLASS.
Attributes and 6isibility
*ublic attributes
Can be "ieed and
changed by all users and
in all methods
Direct access
*ri"ate attributes
Can only be "ieed and
changed from ithin the
class
8o direct access
from outside the class
>ou can "rotect attr!butes a'a!nst access %ro$ outs!de b& character!*!n' the$ as "r!ate attr!butes
de%!ned !n the PRI%ATE #E"TIOH#
Attr!butes and the!r alues that $a& be used d!rectl& b& an eternal user are "ubl!c attr!butes and are
de%!ned !n the P!LI" #E"TION#
In the aboe ea$"le %or the class lcl_car, the attr!bute make!s de%!ned as a "ubl!c attr!bute#
Publ!c attr!butes belon' to the interfaceo% the class, that !s the!r !$"le$entat!on !s "ubl!c!*ed# I% &ou
want to h!de the !nternal !$"le$entat!on %ro$ users, &ou $ust de%!ne !nternal and eternal !ews o%
attr!butes#
As a 'eneral rule, &ou should de%!ne as %ew "ubl!c attr!butes as "oss!ble#
SAP AG TAW10 9-;
8/12/2019 TAW 10 (2-2) - Objects
27/303
#.9
SAP AG 2002
Accessing *ri"ate Attributes
(5) lcl'"ehicle
*ri"ate*ublic
r&vehicle
make
Client
et_make
get_make$$$
>ou can access an object7s "r!ate attr!butes us!n' "ubl!c $ethods, wh!ch !n turn out"ut th!s attr!bute
or chan'e !t#
It !s not "oss!ble to d!rectl& access "r!ate attr!butes %ro$ outs!de %or ea$"le, $a!n "ro'ra$ or
other object# The onl& ece"t!on l!es !n the so-called %r!end conce"t, wh!ch w!ll be dealt w!th later
!n the course#
SAP AG TAW10 9-
8/12/2019 TAW 10 (2-2) - Objects
28/303
#.#0
SAP AG 2002
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.$$$
+;&3A$ S$C&'.#AA: make +$ tr"ng6
$$$
CLASS-#AA: n__ve!"le +$ ".
$'#CLASS.
0nstance Attributes and Static Attributes #$%
0nstance attributes
!ne per instance
Statement: DA)A
Static attributes
!nly one per class
Statement: CLASS&DA)A
Also (non as class
attributes
There are two .!nds o% attr!butes6
Stat!c attr!butes
Instance attr!butes
Instance attr!butes are attr!butes that e!st se"aratel& %or each object#
Instance attr!butes are de%!ned us!n' the AAP .e&word DATA#
Stat!c attr!butes e!st onl& once %or each class and are !s!ble %or all runt!$e !nstances !n that class#
Stat!c attr!butes usuall& conta!n !n%or$at!on that a""l!es to all !nstances, such as6
ata that !s the sa$e !n all !nstances
Ad$!n!strat!e !n%or$at!on about the !nstances !n that class %or ea$"le, counters, ###
Stat!c attr!butes are de%!ned us!n' the "LA##-DATA.e&word#
>ou $a& co$e across the e"ress!on class attr!butes !n docu$entat!on, howeer, the o%%!c!al ter$!n AAP 8bjects as !n @, Jaa !s stat!c attr!butes#
SAP AG TAW10 9-10
8/12/2019 TAW 10 (2-2) - Objects
29/303
#.##
SAP AG 2002
0nstance Attributes and Static Attributes #/%
Internal sessionInternal session
'lobal data ob(ects
)" lcl'"ehicle
*" lcl'"ehicle
n__ve!"le &+L$$'A&'.$?#,met!_name>.
...$'#$?#.
$'#CLASS.
CLASS ,laname> #$%&'&&'....$?#S:,met!_name>
@ &+;&' ,"m_var> +$ ,te>$B+;&' ,ex_var> +$ ,te>C?A'&' ,!_var> +$ ,te>;$4;'&' 3AL4$(,re_var>) +$ ,te>E("EPTION# )e*ce+tion,
;A&S&' ,la_exet"n> .$'#CLASS.
Methods can have
a si%nature
Methods contain the
source code& and hence
de'ine the behavior o' an
ob$ect
4ethods are !nternal "rocedures !n classes that deter$!ne the beha!or o% an object# The& can access
all attr!butes !n the!r class and can there%ore chan'e the state o% an object#
4ethods hae a "ara$eter !nter%ace called sinature that enables the$ to rece!e alues when the&
are called and "ass alues bac. to the call!n' "ro'ra$#
In AAP 8bjects, $ethods can hae IPORTIN., E(PORTIN., "/AN.IN., and RETRNIN.
"ara$eters as well as ece"t!on "ara$eters# All "ara$eters can be "assed b& alue or re%erence# As
o% SAP D?3 as!s Delease 9#10, &ou should no lon'er use the E("EPTION#"ara$eter %or
ece"t!ons but use the RAI#IN.add!t!on !nsteadC th!s w!ll be d!scussed !n $ore deta!l later#
>ou can de%!ne a return code %or $ethods us!n' RETRNIN.# >ou can onl& do th!s %or a s!n'le
"ara$eter, wh!ch add!t!onall& $ust be "assed as a alue# Also, &ou cannot then de%!ne E(PORTIN.
and "/AN.IN."ara$eters# >ou can de%!ne %unct!onal $ethods us!n' the RETRNIN."ara$eterth!s w!ll be e"la!ned !n $ore deta!l#
All !n"ut "ara$eters IPORTIN., "/AN.IN."ara$eters can be de%!ned as o"t!onal "ara$eters
!n the declarat!on us!n' the OPTIONALor DE0ALTadd!t!ons# These "ara$eters then do not
necessar!l& hae to be "assed when the object !s called# I% &ou use the OPTIONALadd!t!on, the
"ara$eter re$a!ns !n!t!al!*ed accord!n' to t&"e, whereas the DE0ALTadd!t!on allows &ou to enter
a start alue#
SAP AG TAW10 9-12
8/12/2019 TAW 10 (2-2) - Objects
31/303
#.#3
SAP AG 2002
Accessing *ri"ate Methods
(5) lcl'"ehicle
*ri"ate*ublic
r&vehicle
"n"t_make
et_make
make...
...
Client
4ethods also hae to be ass!'ned to a !s!b!l!t& area# Th!s deter$!nes whether the $ethods can be
called %ro$ outs!de or onl& %ro$ w!th!n the class#
It !s not "oss!ble to d!rectl& access "r!ate $ethods %ro$ outs!de# Boweer, a "r!ate $ethod can be
called b& a "ubl!c $ethod#
oth $ethod t&"es can access the "ubl!c and "r!ate attr!butes#
SAP AG TAW10 9-13
8/12/2019 TAW 10 (2-2) - Objects
32/303
#.#4
SAP AG 2002
Methods and 6isibility
*ublic methods
Can be called from
anyhere
*ri"ate methods
Can only be called
ithin the class
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.$?#S: et_make "mrt"ng
"m_make +$ tr"ng.+;&3A$ S$C&'.$?#S: "n"t_make.#AA: make +$ tr"ng.
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'.$?# "n"t_make.make = Dn makeD.$'#$?#.
$?# et_make.&% "m_make &S &'&&AL.
* Calling init_make...
$LS$.make = "m_make.
$'#&%.$'#$?#.
$'#CLASS.
In th!s ea$"le, init_make!s a "r!ate $ethod that !s called b& the "ubl!c $ethod 'et_make.
SAP AG TAW10 9-1/
8/12/2019 TAW 10 (2-2) - Objects
33/303
#.#5
SAP AG 2002
0nstance Methods and Static Methods
0nstance methods
Can use both static and instance components in their
implementation part
Can be called using an instance
Static methods
Can only use static components in their implementation
part
Can be called using the class
Rules:
Stat!c $ethods are de%!ned at class leel# The& are s!$!lar to !nstance $ethods, but w!th the
restr!ct!on that the& can onl& use stat!c co$"onents such as stat!c attr!butes !n the !$"le$entat!on
"art# Th!s $eans that stat!c $ethods do not need !nstances and can be called %ro$ an&where# The&
are de%!ned us!n' the "LA##-ET/OD#state$ent, and the& are bound b& the sa$e s&nta and
"ara$eter rules as !nstance $ethods#
The ter$ class $ethod !s co$$on, but the o%%!c!al ter$ !n AAP 8bjects as !n @, Jaa !s
stat!c $ethod#
SAP AG TAW10 9-15
8/12/2019 TAW 10 (2-2) - Objects
34/303
#.#6
SAP AG 2002
0nstance Methods and Static Methods: 34ample
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.$?#S: et_make &+;&' "m_make +$ tr"ng.CLASS-$?#S: get_unt $B+;&' ex_unt +$ ".
+;&3A$ S$C&'.#AA: make +$ tr"ng.CLASS-#AA: n__ve!"le +$ ".
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'....
$?# get_unt.ex_unt = n__ve!"le.
$'#$?#
$'#CLASS.
In the stat!c $ethod get_count, &ou can onl& use the stat!c attr!bute n_o_vehicle'. All other
attr!butes o% the class are !nstance attr!butes and can onl& a""ear !n !nstance $ethods#
SAP AG TAW10 9-19
8/12/2019 TAW 10 (2-2) - Objects
35/303
#.#7
SAP AG 2002
Summary and UML 8otation
, re!ers to public components
- re!ers to private components
& static components are underlined
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.$?#S: et_make &+;&' "m_make +$ tr"ng.CLASS-$?#S: get_unt $B+;&' ex_unt +$ ".
+;&3A$ S$C&'.#AA: make +$ tr"ng. ...CLASS-#AA: n__ve!"le +$ ".
$?#S: "n"t_make.$'#CLASS.
lcl'"ehicle& ma(e
& model
& n'o'"ehicles
= set'ma(e
& init'ma(e
= get'count
A 4 class d!a'ra$ shows %!rstl& the class na$e and, underneath that, the class attr!butes and
$ethods#
The !s!b!l!t& o% co$"onents !n a class !s shown !n 4 us!n' the characters and -6
!nd!cates "ubl!c co$"onents
/ !nd!cates "r!ate co$"onents
Alternat!el&, "ubl!c and "r!ate can be "re%!ed to the $ethods# The th!rd o"t!on %or "ro!ders o%
$odel!n' tools !n 4 !s to !ntroduce the!r own s&$bols %or !s!b!l!t
De"resentat!on o% !s!b!l!t& character!st!cs !s o"t!onal and !s nor$all& onl& used %or $odels that are
close to !$"le$entat!on#
Stat!c co$"onents are $ar.ed w!th an underscore#
The $ethod s!'nature !s re"resented as %ollows o"t!onal6The !n"ut and out"ut "ara$eters and the "ara$eters to be chan'ed are shown !n brac.ets#
The return code !s se"arated %ro$ the t&"e na$e b& a colon#
SAP AG TAW10 9-1:
8/12/2019 TAW 10 (2-2) - Objects
36/303
#.#8
SAP AG 2002
*rinciples #/%
Classes, attributes, and methodsClasses, attributes, and methods
!bjects, instances of classes!bjects, instances of classes
Accessing attributes and methodsAccessing attributes and methods
)he constructor)he constructor
Additional principlesAdditional principles
SAP AG TAW10 9-1;
8/12/2019 TAW 10 (2-2) - Objects
37/303
#.#9
SAP AG 2002
Creating !bjects
Rules: !bjects are created using the C-3A)3 !2>3C)statement
!bjects can only be created and addressed using
reference "ariables
lcl'"ehicle
& ma(e
& model& ser'no
& n'o'"ehicles
= set'ma(e
& init'ma(e
= get'count
(5) lcl'"ehicle
PrivatePublic
make
meller_n...
et_makeget_unt...
C;$A$ E$C r_ve!"le
r&vehicle
A class conta!ns the 'ener!c descr!"t!on o% an object# It descr!bes all the character!st!cs that are
co$$on to all objects !n that class# ur!n' the "ro'ra$ runt!$e, the class !s used to create s"ec!%!c
objects !nstances# Th!s "rocess !s called !nstant!at!on#
(a$"le6
The s"ec!%!c object K@ar & w!th Ser-Ho# 0;15 !s created throu'h !nstant!at!on %ro$ the class
lcl_vehicleC !t !s created !n the $a!n $e$or& at runt!$e#
The lcl_vehicleclass !tsel% does not e!st as an !nde"endent runt!$e object !n AAP 8bjects#
I$"le$entat!on6
8bjects are !nstant!ated us!n' the state$ent6"REATE O!1E"T#
ur!n' !nstant!at!on, the runt!$e en!ron$ent d&na$!call& re)uests $a!n $e$or& s"ace and ass!'ns
!t to the object#
SAP AG TAW10 9-1
8/12/2019 TAW 10 (2-2) - Objects
38/303
#.20
SAP AG 2002
-eference 6ariables
...
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'....
+;&3A$ S$C&'....
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'....
$'#CLASS.
#AA: r_ve!"le1 +$ ;$% ll_ve!"le6r_ve!"le2 +$ ;$% ll_ve!"le.
SA;-%_S$L$C&'.
$$$
r&vehicle+
r&vehicle)?
?
+hat do these
reference "ariables
point to?
DATA2 r_vehicle3 TYPE RE0 TO lcl_vehicledeclares a re%erence ar!able that acts as
a "o!nter to an object#
SAP AG TAW10 9-20
8/12/2019 TAW 10 (2-2) - Objects
39/303
#.2#
SAP AG 2002
Creating !bjects: Synta4
#AA: r_ve!"le1 +$ ;$% ll_ve!"le6r_ve!"le2 +$ ;$% ll_ve!"le.
C;$A$ E$C r_ve!"le1.C;$A$ E$C r_ve!"le2.
r&vehicle+
r&vehicle)
(3) lcl'"ehicle
PrivatePublic
() lcl'"ehicle
PrivatePublic
The "REATE O!1E"Tstate$ent creates an object !n the $a!n $e$or The attr!bute alues o% th!s
object are e!ther !n!t!al alues or corres"ond to the %ALEentr
De%erence ar!ables can also be ass!'ned to each other#
+or the aboe ea$"le th!s would $ean that r_vehicle3and r_vehicle4"o!nt to the sa$e
object#
SAP AG TAW10 9-21
8/12/2019 TAW 10 (2-2) - Objects
40/303
#.22
SAP AG 2002
.arbage Collector
#AA: r_ve!"le1 +$ ;$% ll_ve!"le6r_ve!"le2 +$ ;$% ll_ve!"le.
C;$A$ E$C r_ve!"le1.C;$A$ E$C r_ve!"le2.
r_ve!"le2 = r_ve!"le1
r&vehicle)() lcl'"ehicle
PrivatePublic
r&vehicle+
(3) lcl'"ehicle
PrivatePublic
As soon as no $ore re%erences "o!nt to an object, the Garba'e @ollector re$oes !t %ro$ the
$e$or
The Garba'e @ollector !s a s&ste$ rout!ne that auto$at!call& deletes objects that can no lon'er be
addressed %ro$ the $a!n $e$or& and releases the $e$or& s"ace the& occu"!ed#
SAP AG TAW10 9-22
8/12/2019 TAW 10 (2-2) - Objects
41/303
#.23
SAP AG 2002
.arbage Collector: *rocedure
All independent references in the global main memory are chec(ed )he
references point to acti"e objects, hich are mar(ed internally
-eferences of class or instance attributes to other objects are folloed
)hese objects are also mar(ed
!bjects that are not mar(ed are deleted from the main memory
(2) lcl'object
() lcl'object
(5) lcl'object
eleted
Inde"endent re%erences are re%erences that hae not been de%!ned w!th!n a class#
SAP AG TAW10 9-23
8/12/2019 TAW 10 (2-2) - Objects
42/303
#.24
SAP AG 2002
2uffering -eferences
#AA: r_ve!"le +$ ;$% ll_ve!"le6"ta5 +$ AL$ % ;$% ll_ve!"le.
C;$A$ E$C r_ve!"le.A++$'# r_ve!"le "ta5.
C;$A$ E$C r_ve!"le.A++$'# r_ve!"le "ta5.
(2) lcl'object
(3) lcl'object
L+ A "ta5 &' r_ve!"le.* work with the current instance
$'#L+.$$$
I% &ou want to .ee" seeral objects %ro$ the sa$e class !n &our "ro'ra$, &ou can de%!ne an !nternal
table, wh!ch, %or ea$"le, onl& cons!st o% one colu$n conta!n!n' the object re%erences %or th!s class#
>ou can "rocess the objects us!n' a LOOPthrou'h the !nternal table#
SAP AG TAW10 9-2/
8/12/2019 TAW 10 (2-2) - Objects
43/303
#.25
SAP AG 2002
Aggregation 34ample
(5) lcl'"ehicle
PrivatePublic
"ta5_F!eel
(1) lcl'heel
(25) lcl'heel
(2*) lcl'heel
(3+) lcl'heel
I% a class de%!nes object re%erences to a second class as attr!butes !n the aboe ea$"le6 De%erences
to objects o% the class lcl_5heel, onl& these object re%erences w!ll be stored !n an object
belon'!n' to that class#
The objects !n the second class lcl_5heelhae the!r own !dent!t The& are enca"sulated !n the
%!rst class lcl_vehicleand can onl& be addressed %ro$ th!s class us!n' re%erence ar!ables#
SAP AG TAW10 9-25
8/12/2019 TAW 10 (2-2) - Objects
44/303
#.26
SAP AG 2002
*rinciples #
8/12/2019 TAW 10 (2-2) - Objects
45/303
#.27
SAP AG 2002
Calling Methods
(5) lcl'"ehicle
PrivatePublic
r_ve!"le->mtr_n( )
-eference
Component selector
Method
Client
m/t/r_/n
(er& object behaes !n a certa!n wa Th!s beha!or !s deter$!ned b& !ts $ethods# There are three
t&"es o% $ethod6
1# 4ethods that tr!''er the beha!or and do not return alues see ea$"le
2# 4ethods that return a alue
3# 4ethods that return or chan'e seeral alues
An object that re)u!res the ser!ces o% another object sends a $essa'e to the object "ro!d!n' the
ser!ces# Th!s $essa'e na$es the o"erat!on to be eecuted# The !$"le$entat!on o% th!s o"erat!on !s
.nown as a $ethod#
+or the sa.e o% s!$"l!c!t&, $ethod w!ll hence%orth be used as a s&non&$ %or o"erat!on and $essa'e#
The ea$"le shows the new s&nta %or $ethod calls, !n wh!ch the "ALL-ET/OD"re%! !s o$!tted#
SAP AG TAW10 9-2:
8/12/2019 TAW 10 (2-2) - Objects
46/303
#.28
SAP AG 2002
Calling 0nstance Methods: Synta4
.nstance methods$CALL $?#,"ntane>->,"ntane_met!>$B+;&' ,"m_var> = ,var"a5le>
&+;&' ,ex_var> = ,var"a5le>C?A'&' ,!_var> = ,var"a5le>;$C$&3&' ,re_var> = ,var"a5le>E("EPTION# )e*ce+tion, 6 )nr,.
Shortened notation as o! 01C$ ,"ntane>->,"ntane_met!>(see online documentation) ( @a"t"n )
#AA: r_ve!"le +$ ;$% ll_ve!"le.#AA: make_name +$ S;&'.$$$
make_name = Dt!e make f t!e arD.
CALL $?# r_ve!"le->et_make $B+;&' "m_make = make_name.* other variant, since 6.1 available, see documentation!
r_ve!"le->et_make( make_name )* now getting something back from method...
r_ve!"le->get_make( &+;&' ex_make = make_name ).
Instance $ethods are called us!n' "ALL ET/OD )reference,-,)in'tance_method,#
S!nce SAP D?3 as!s Delease 9#10, the shortened %or$ !s also su""ortedC "ALL ET/OD!s o$!tted#
+or %urther !n%or$at!on, re%er to the onl!ne docu$entat!on#
S"ec!al case6 When call!n' an !nstance $ethod %ro$ w!th!n another !nstance $ethod, &ou can o$!t
the !nstance na$e# The $ethod !s auto$at!call& eecuted %or the current object#
+or the $ethod call 'et_make, &ou can o$!t the E(PORTIN.add!t!on !n the brac.ets# In th!s case,
!t !s su%%!c!ent to set the actual "ara$eter o% the caller !n the ea$"le6 make_name# Boweer, !%
the $ethod has two or $ore "ara$eters !n !ts !nter%ace, all actual and %or$al "ara$eters w!th!n the
brac.ets $ust hae the E(PORTIN.add!t!on#
In the case o% $ethods that return "ara$eters to the caller, the IPORTIN.add!t!on $ust alwa&s be
used and all actual and %or$al "ara$eters $ust be l!sted#
In $ethod calls, $ult!"le "ara$eters are se"arated b& s"aces#
SAP AG TAW10 9-2;
8/12/2019 TAW 10 (2-2) - Objects
47/303
#.29
SAP AG 2002
Calling Static Methods: Synta4
Static methods: CALL $?#,laname>=>,la_met!>( @a"t"n )
,laname>=>,la_met!>( @a"t"n )
#AA: r_ve!"le +$ ;$% ll_ve!"le.#AA: make_name +$ S;&'6
unt +$ &.
$$$
* get_count has one interface-parameter re_count
CALL $?# ll_ve!"le=>get_unt( &+;&' re_unt = unt ).
* other variant, since 6.1 available, see documentation
ll_ve!"le=>get_unt( &+;&' re_unt = unt ).
Stat!c $ethods also re%erred to as class $ethods are called us!n' "ALL ET/OD
)cla''name,6,)cla''_method,#
Stat!c $ethods are addressed w!th the!r class na$e, s!nce the& do not need !nstances#
Hote6
I% &ou are call!n' a stat!c $ethod %ro$ w!th!n the class, &ou can o$!t the class na$e#
SAP AG TAW10 9-2
8/12/2019 TAW 10 (2-2) - Objects
48/303
#.30
SAP AG 2002
9unctional Methods
When de!ining$
!nly one -3)U-808. parameter
!nly 0M*!-)08. parameters and e4ceptions are possible
When calling$
a% -3C30608. parameters possible
b% 6arious forms of direct call possible:
M!63, CAS3, L!!*
Logical e4pressions #09, 3LS309, +@0L3, C@3C, +A0)%
Arithmetic e4pressions and bit e4pressions #C!M*U)3%,
see e4ample: a B b = c
a = 5 G 9unctional methodE2ample
4ethods that hae a RETRNIN."ara$eter are descr!bed as %unct!onal $ethods# These $ethods
cannot hae E(PORTIN.or "/AN.IN."ara$eters, but has $an& or as %ew IPORTIN.
"ara$eters and ece"t!ons as re)u!red#
+unct!onal $ethods can be used d!rectl& !n ar!ous e"ress!ons6
o'!cal e"ress!ons I0, EL#EI0, 7/ILE, "/E"K, 7AIT
The "A#Estate$ent "A#E, 7/EN
The LOOPstate$ent
Ar!th$et!c e"ress!ons "OPTE
!t e"ress!ons "OPTE
The O%Estate$ent#
SAP AG TAW10 9-30
8/12/2019 TAW 10 (2-2) - Objects
49/303
#.3#
SAP AG 2002
9unctional Methods: 34ample
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.$?#S: get_average_fuel
&+;&' "m_"tane +$ _"tane6"m_fuel +$ t_fuel
;$4;'&'3AL4$(re_fuel) +$ t_fuel6$'#CLASS.
#AA: r_ve!"le1 +$ ;$% ll_ve!"le6r_ve!"le2 +$ ;$% ll_ve!"le6avg_fuel +$ t_fuel.
$$$
* example for short sntax in aritmet. operation
avg_fuel =r_ve!"le1->get_average_fuel( "m_"tane = 00 "m_fuel = 0 )
G r_ve!"le2->get_average_fuel( "m_"tane =
8/12/2019 TAW 10 (2-2) - Objects
50/303
#.32
SAP AG 2002
34ternal Access to *ublic Attributes
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.#AA: make +$ tr"ng ;$A#-'L. ou access stat!c attr!butes us!n' )cla''name,=>)cla''_attri?ute, >ou access !nstance attr!butes us!n')in'tance,->)in'tance_attri?ute, =>and ->are the co$"onent selectors
SAP AG TAW10 9-32
8/12/2019 TAW 10 (2-2) - Objects
51/303
#.33
SAP AG 2002
Classes, attributes, and methodsClasses, attributes, and methods
!bjects, instances of classes!bjects, instances of classes
Accessing attributes and methodsAccessing attributes and methods
)he constructor)he constructor
Additional principlesAdditional principles
*rinciples #%
SAP AG TAW10 9-33
8/12/2019 TAW 10 (2-2) - Objects
52/303
#.34
SAP AG 2002
Constructor
Special method for creating
objects ith defined initial state
!nly has 0M*!-)08.
parameters and 3C3*)0!8S
0s e4ecuted only once per
instance
C;$A$ E$C
$?#S ntrutr &+;&' ,"m_arameter>$BC$+&'S ,exet"n>.
$?#S ntrutr &+;&' ,"m_arameter>$BC$+&'S ,exet"n>.
lcl'"ehicle& ma(e
& model
& n'o'"ehicles
= constructor
(3) lcl'"ehicle
PrivatePublic
The constructor !s a s"ec!al !nstance $ethod !n a class w!th the na$e con'tructor# The
%ollow!n' rules a""l&6
(ach class can hae one constructor#
The constructor !s auto$at!call& called at runt!$e w!th!n the "REATE O!1E"Tstate$ent#
I% &ou need to !$"le$ent the constructor, then &ou $ust de%!ne and !$"le$ent !t !n the
P!LI" #E"TION#
When ece"t!ons are ra!sed !n the constructor, !nstances are notcreated, so no $a!n $e$or& s"ace !s
occu"!ed#
SAP AG TAW10 9-3/
8/12/2019 TAW 10 (2-2) - Objects
53/303
#.35
SAP AG 2002
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.$?#S ntrutr &+;&' "m_make +$ tr"ng
"m_mel +$ tr"ng.+;&3A$ S$C&'.#AA: make +$ tr"ng6 Fe"g!t +$ .CLASS-#AA n__ve!"le +$ ".
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'.$?# ntrutr.make = "m_make.mel = "m_mel.A## 1 n__ve!"le.
$'#$?#.$'#CLASS.
Constructor: 34ample
#AA r_ve!"le +$ ;$% ll_ve!"le....C;$A$ E$C r_ve!"le
$B+;&' "m_make = D%errar"D"m_mel = D%90D.
Automatic constructor call
,all 'rom the
main pro%ram
>ou need to !$"le$ent the constructor when, %or ea$"le6
>ou need to allocate eternal resources
>ou need to !n!t!al!*e attr!butes that cannot be coered b& the %ALEadd!t!on to the DATA
state$ent
>ou need to $od!%& stat!c attr!butes
>ou cannot nor$all& call the constructor e"l!c!tl
SAP AG TAW10 9-35
8/12/2019 TAW 10 (2-2) - Objects
54/303
#.36
SAP AG 2002
*re"ie: Static Constructor
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.CLASS-$?#S: CLASS_C'S;4C;.
+;&3A$ S$C&'.CLASS-#AA: n__ve!"le +$ &.
$'#CLASS.
Special static method
Automatically called before
the class is first accessed
!nly e4ecuted once per
programCLASS ll_ve!"le &+L$$'A&'.$?# CLASS_C'S;4C;.
$$$
$'#$?#. $$$$'#CLASS.
* Static Constructor is called before following statements:* demo 1
C;$A$ E$C r_ve!"le.* demo "
unter = ll_ve!"le=>n__ve!"le. if attribute is public
* demo #
ll_ve!"le=>get_unt( "mrt"ng re_unt = unter ).
The stat!c constructor !s a s"ec!al stat!c $ethod !n a class w!th the na$e cla''_con'tructor# It
!s eecuted "rec!sel& once "er "ro'ra$# The stat!c constructor o% a class )cla''name,!s called
auto$at!call& when the class !s %!rst accessed, but be%ore an& o% the %ollow!n' act!ons are eecuted6
@reat!n' an !nstance !n the class us!n' "REATE O!1E"T )o?=,, where )o?=,has the data
t&"e RE0 TO )cla''name,
Address!n' a stat!c attr!bute us!n' )cla''name,6,)attri?ute,
@all!n' a stat!c attr!bute us!n' "ALL ET/OD )cla''name,6,)cla''method,
De'!ster!n' a stat!c eent handler $ethod us!n' #ET /ANDLER)cla''name,6,)handler_method, 0OR )o?=,
De'!ster!n' an eent handler $ethod %or a stat!c eent !n class )cla''name,#
The stat!c constructor cannot be called e"l!c!tl
SAP AG TAW10 9-39
8/12/2019 TAW 10 (2-2) - Objects
55/303
#.37
SAP AG 2002
*rinciples #E%
Classes, attributes, and methodsClasses, attributes, and methods
!bjects, instances of classes!bjects, instances of classes
Accessing attributes and methodsAccessing attributes and methods
)he constructor)he constructor
Additional principlesAdditional principles
SAP AG TAW10 9-3:
8/12/2019 TAW 10 (2-2) - Objects
56/303
8/12/2019 TAW 10 (2-2) - Objects
57/303
#.39
SAP AG 2002
)he -eference 6ariable M3
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.$?#S ntrutr
&+;&' "m_make +$ tr"ng"m_mell +$ tr"ng.
+;&3A$ S$C&'.#AA make +$ tr"ng. $$$
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'.$?# ntrutr. H=u't a demo
#AA make +$ tr"ng 3AL4$ DenID.C'CA$'A$ "m_make make &'$->make.
$'#$?#.
$?# #4.> calling o5n method' from in'ide cla''
> call method di'+la@_attri?ute'8 9
> 5ith the 'hort '@nta* variant2
"la_attr"5ute( ).$'#$?#.
$'#CLASS.
lcl'"ehicle
*ri"ate
components
Methodimplementation
*ublic
components
Attribute
make
$->make
>ou can address the object !tsel% w!th!n !nstance $ethods us!n' the !$"l!c!tl& aa!lable re%erence
ar!able me#
escr!"t!on o% ea$"le6
In the constructor, the !nstance attr!bute make!s coered b& the locall& de%!ned ar!able make# In
order to st!ll be able to address the !nstance attr!bute, &ou need to use me#
The dumm@$ethod de$onstrates how to call a class7s own $ethod# >ou can o$!t the "re%! me-,#
8ther !$"ortant use6
An object calls another object7s $ethod and "asses !ts own address#
SAP AG TAW10 9-3
8/12/2019 TAW 10 (2-2) - Objects
58/303
#.40
SAP AG 2002
Create classes
Create objects
Call methods
34plain ho the garbage collector or(s
5ou are no able to:
*rinciples of !bject&!riented *rogramming: UnitSummary
SAP AG TAW10 9-/0
8/12/2019 TAW 10 (2-2) - Objects
59/303
#.4#rinci+les of 1bject/1riented rora!!in '(ercises
)nit* rinci+les of 1bject/1riented rora!!in
To+ic* Creatin a Class
At the conclus!on o% these eerc!ses, &ou w!ll be able to6
@reate a local class
An a!rl!ne needs to $ana'e !ts a!r"lanes#
,odel solution* SA+C901_A&;S_A&'_ASA+C901_A&;S_A !nclude "ro'ra$
1-1 @reate the "ac.a'e JC901_KKwhere LL6 !s &our 'rou" nu$ber and sae all there"os!tor& objects &ou hae created dur!n' the course !n th!s "ac.a'e#
1-2 @reate the $a!n "ro'ra$ JC901_KK_A&'_A&;+LA'$and the !nclude"ro'ra$ JC901_KK_A&;+LA'$the $a!n "ro'ra$ conta!ns LL6 'rou"nu$ber#
1-3 @reate the class lcl_airplane!n the !nclude "ro'ra$#
1-3-1 Th!s class has two +rivate instance attributes6
- name
-planetype#
The attr!bute %or the a!r"lane na$e should be o% the t&"e STDIHG# e%!ne
the attr!bute %or the "lane t&"e us!n' the table %!eld saplane-planetype#
1-3-2 The class has a +rivate static attribute6
- n_o_airplanes#
Th!s attr!bute should be t&"e I#
1-3-3 The class has a +ublic instance !ethodset_attributesto set the "r!ate
!nstance attr!butes na$e and "lane t&"e# (nter two corres"ond!n' !$"ort!n'
"ara$eters %or the declarat!on o% the $ethod !n the de%!n!t!on "art# The
de%!n!t!on o% these "ara$eters should be analo'ous to the two attr!butes#I$"le$ent the $ethod !n the !$"le$entat!on "artC set both attr!butes# (ach
t!$e the $ethod !s called, the stat!c attr!bute n_o_airplanesshould !ncrease
b& one# Th!s !s actuall& not r!'ht but w!ll be corrected later#
SAP AG TAW10 9-/1
8/12/2019 TAW 10 (2-2) - Objects
60/303
1-3-/ The class !s to hae another +ublic instance !ethoddisplay_attributesto
d!s"la& the !nstance attr!butes# eclare th!s $ethod and, !n the
!$"le$entat!on "art, out"ut the attr!butes us!n' the WDIT( state$ent &ou
can also d!s"la& the !con !con_5'_+lane, !n wh!ch case &ou $ust add
TYPE-POOL# iconto the "ro'ra$#
1-3-5 eclare and !$"le$ent a +ublic static !ethoddisplay_n_o_airplanesto
d!s"la& the stat!c attr!bute n_o_airplanes#
In the re$a!n!n' eerc!ses !n th!s course, &ou can alwa&s start w!th the "ro'ra$ &ou
created !n the "re!ous eerc!se or co"& the corres"ond!n' $odel solut!on and cont!nue
wor.!n' w!th th!s "ro'ra$#
SAP AG TAW10 9-/2
8/12/2019 TAW 10 (2-2) - Objects
61/303
'(ercises
)nit* rinci+les of 1bject/1riented
rora!!in
To+ic* nstantiatin 1bjects
At the conclus!on o% these eerc!ses, &ou w!ll be able to6
Instant!ate objects
,odel solution* SA+C901_A&;S_A&'_SA+C901_A&;S_A !nclude "ro'ra$
2-1 @reate a re%erence to the class lcl_airplane#
2-2 e%!ne an !nternal table %or bu%%er!n' objects o% the class lcl_airplane# The t&"e o% the
!nternal table should there%ore beREF TO lcl_airplane#
2-3 @reate seeral objects o% the class lcl_airplaneand store the$ !n the !nternal table#
2-/ +ollow the "ro'ra$ %low !n the ebu''er#
In the re$a!n!n' eerc!ses !n th!s course, &ou can alwa&s start w!th the "ro'ra$ &ou
created !n the "re!ous eerc!se or co"& the corres"ond!n' $odel solut!on and
cont!nue wor.!n' w!th th!s "ro'ra$#
SAP AG TAW10 9-/3
8/12/2019 TAW 10 (2-2) - Objects
62/303
'(ercises
)nit* rinci+les of 1bject/1riented
rora!!in
To+ic* ,ethod Calls
At the conclus!on o% these eerc!ses, &ou w!ll be able to6
@all !nstance $ethods and stat!c $ethods
,odel solution* SA+C901_A&;S_A&'_CSA+C901_A&;S_C !nclude "ro'ra$
3-1 @all the stat!c $ethod display_n_o_airplanesbe%ore !nstant!at!n' an object !n class
lcl_airplane#
3-2 se theset_attributes $ethod to set the attr!butes %or all objects alread& created#
@hoose an a!r"lane na$e and a!r"lane t&"e and "ass the$ as tet l!terals# +or the
"lane t&"e &ou can use a t&"e %ro$ the table SAPAH(, %or ea$"le K:/:-/00M#
3-3 !s"la& the object attr!butes us!n' the display_attributes$ethod#
3-/ @all the stat!c $ethod display_n_o_airplanesa second t!$e#
3-5 Add a %unct!onal stat!c $ethod 'et_n_o_airplanesto the class lcl_airplane#The
$ethod $ust be "ubl!c and hae the re_countt&"e I return "ara$eter no !n"ut
"ara$eters#
3-9 Test &our %unct!onal $ethod b& call!n' !t %ro$ the $a!n "ro'ra$#
SAP AG TAW10 9-//
8/12/2019 TAW 10 (2-2) - Objects
63/303
8/12/2019 TAW 10 (2-2) - Objects
64/303
'(ercises
)nit* rinci+les of 1bject/1riented rora!!in
To+ic* Callin a rivate ,ethod
At the conclus!on o% these eerc!ses, &ou w!ll be able to6
@all $ethods w!th!n a class
,odel solution* SA+C901_A&;S_A&'_$SA+C901_A&;S_$ !nclude "ro'ra$
5 @reate the +rivate !ethodget_technical_attributes%or the class lcl_airplane !n
&our !nclude "ro'ra$ JC901_KK_A&;+LA'$#
5-1 The a!r"lane t&"e !s the !$"ort "ara$eter# T&"e sa"lane-"lanet&"e
5-2 The e"ort "ara$eter !s the we!'ht W(IGBT and the tan. ca"ac!t& TAHO@AP#De%er to the t&"es !n the table SAPAH(#
5-3 The result, both e"ort "ara$eters are ascerta!ned !n a database access to the
table SAPAH(#
There%ore, both attr!butes are read, de"end!n' on the a!r"lane t&"e#
5-/ I% the !$"orted "lane t&"e does not e!st !n the table, set de%ault alues we!'ht
100000, tan. ca"ac!t& 10000#
5-5 Test &our $ethodget_technical_attributes6
5-5-1 & call!n' !t %ro$ the $a!n "ro'ra$
5-5-2 & call!n' !t w!th!n the display_attributes$ethod
5-3-3 Wh!ch call was success%ul, and wh&F
SAP AG TAW10 9-/9
8/12/2019 TAW 10 (2-2) - Objects
65/303
#.42rinci+les of 1bject/1riented rora!!in olutions
)nit* rinci+les of 1bject/1riented
rora!!in
To+ic* Creatin a Class
>--------------------------------------------------------------------->
> Re+ort #AP!"B;3_AIR#_AIN_a >
>--------------------------------------------------------------------->
;$+; a5901_a"r_ma"n_a.
+$-+LS "n.
&'CL4#$ SA+C901_a"r_A.
>----------------------------------------------------------------->
> Include #AP!"B;3_AIR#_A >
>----------------------------------------------------------------->
>------------------------------------------------------------------>
> "LA## lcl_air+lane DE0INITION >
>------------------------------------------------------------------>
CLASS ll_a"rlane #$%&'&&'.
+4L&C S$C&'.
-------------------------------- C'SA'S: _1 +$ " 3AL4$ 70.
$?#S: et_attr"5ute &+;&'
"m_name +$ tr"ng
"m_lanete +$ alane-lanete6
"la_attr"5ute.
CLASS-$?#S: "la_n__a"rlane.
+;&3A$ S$C&'. ----------------------------------
#AA: name +$ tr"ng6
lanete +$ alane-lanete.
CLASS-#AA: n__a"rlane +$ ".
$'#CLASS.
>------------------------------------------------------------------>
> "LA## lcl_air+lane IPLEENTATION >
>------------------------------------------------------------------>
CLASS ll_a"rlane &+L$$'A&'.
SAP AG TAW10 9-/:
8/12/2019 TAW 10 (2-2) - Objects
66/303
$?# et_attr"5ute.
name = "m_name.
lanete = "m_lanete.
n__a"rlane = n__a"rlane G 1.
$'#$?#.
$?# "la_attr"5ute.
M;&$: / "n_F_lane a "n6
/ D'ame f a"rlane:D(001)6 A _1 name6
/ DA"rlane teD(002)6 A _1 lanete.
$'#$?#.
$?# "la_n__a"rlane.
M;&$: /6 / Dtal num5er f laneD(a1)6
A _1 n__a"rlane L$%-E4S&%&$#6 /.
$'#$?#.
$'#CLASS.
SAP AG TAW10 9-/;
8/12/2019 TAW 10 (2-2) - Objects
67/303
olutions
)nit* rinci+les of 1bject/1riented
rora!!in
To+ic* nstantiatin 1bjects
>--------------------------------------------------------------------->
> Re+ort #AP!"B;3_AIR#_AIN_? >
>--------------------------------------------------------------------->
> create and in'ert +lane' into internal ta?le >
>--------------------------------------------------------------------->
REPORT 'a+?cB;3_air'_main_?$
TYPE-POOL# icon$
IN"LDE #AP!"B;3_air'_A$
#AA: r_lane +$ ;$% ll_a"rlane6
lane_l"t +$ AL$ % ;$% ll_a"rlane.
#TART-O0-#ELE"TION$
>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C;$A$ E$C r_lane. A++$'# r_lane lane_l"t.
C;$A$ E$C r_lane.
A++$'# r_lane lane_l"t.
C;$A$ E$C r_lane.
A++$'# r_lane lane_l"t.
SAP AG TAW10 9-/
8/12/2019 TAW 10 (2-2) - Objects
68/303
olutions
)nit* rinci+les of 1bject/1riented
rora!!inTo+ic* ,ethod Calls
>--------------------------------------------------------------------->
> Re+ort #AP!"B;3_AIR#_AIN_" >
>--------------------------------------------------------------------->
> call method 'et_Attri?ute' to initialie o?=ect' >
> and vi'ualie content of o?=ect' via di'+la@_attri?ute' >
>--------------------------------------------------------------------->
REPORT 'a+?cB;3_air'_main_c$
TYPE-POOL# icon$
IN"LDE 'a+?cB;3_air'_c$
#AA: r_lane +$ ;$% ll_a"rlane6
lane_l"t +$ AL$ % ;$% ll_a"rlane6
unt te ".
#TART-O0-#ELE"TION$
>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ll_a"rlane=>"la_n__a"rlane( ).
C;$A$ E$C r_lane.
A++$'# r_lane lane_l"t.
r_lane->et_attr"5ute( "m_name = DL? erl"nD
"m_lanete = D9-900D ).
C;$A$ E$C r_lane.
A++$'# r_lane lane_l"t.
r_lane->et_attr"5ute( "m_name = DAA 'eF rkD
"m_lanete = D7-100D ).
L+ A lane_l"t &' r_lane.
r_lane->"la_attr"5ute( ).
$'#L+.
ll_a"rlane=>"la_n__a"rlane( ).
* calling the functional method get_n_o_airplanes
* long sntax for functional call$
* lcl_airplane%&get_n_o_airplanes' receiving re_count % count (.
* short sntax for functional call$
SAP AG TAW10 9-50
8/12/2019 TAW 10 (2-2) - Objects
69/303
unt = ll_a"rlane=>get_n__a"rlane( ).
Fr"te: / D'um5er f a"rlane : D6 unt.
SAP AG TAW10 9-51
8/12/2019 TAW 10 (2-2) - Objects
70/303
>--------------------------------------------------------------------->
> Include #AP!"B;3_AIR#_" >
> Develo+' functional 'tatic method get_n_o_air+lane' >
>--------------------------------------------------------------------->
>------------------------------------------------------------------>
> "LA## lcl_air+lane DE0INITION >>------------------------------------------------------------------>
CLASS ll_a"rlane #$%&'&&'.
+4L&C S$C&'.
--------------------------------
C'SA'S: _1 +$ " 3AL4$ 70.
$?#S: et_attr"5ute &+;&'
"m_name +$ tr"ng
"m_lanete +$ alane-lanete6
"la_attr"5ute.
CLASS-$?#S: "la_n__a"rlane6
get_n__a"rlane return"ng 3AL4$(re_unt) te &.
+;&3A$ S$C&'.
----------------------------------
#AA: name +$ tr"ng6
lanete +$ alane-lanete.
CLASS-#AA: n__a"rlane +$ ".
$'#CLASS.
>------------------------------------------------------------------>
> "LA## lcl_air+lane IPLEENTATION >
>------------------------------------------------------------------>
CLASS ll_a"rlane &+L$$'A&'.
$?# et_attr"5ute.
name = "m_name. lanete = "m_lanete.
n__a"rlane = n__a"rlane G 1.
$'#$?#.
$?# "la_attr"5ute.
M;&$: / "n_F_lane a "n6
/ D'ame f a"rlane:D(001)6 A _1 name6
/ DA"rlane teD(002)6 A _1 lanete.
$'#$?#.
$?# "la_n__a"rlane.
M;&$: /6 / Dtal num5er f laneD(a1)6
A _1 n__a"rlane L$%-E4S&%&$#6 /.
SAP AG TAW10 9-52
8/12/2019 TAW 10 (2-2) - Objects
71/303
$'#$?#.
met! get_n__a"rlane.
re_unt = n__a"rlane.
enmet!.
$'#CLASS.
SAP AG TAW10 9-53
8/12/2019 TAW 10 (2-2) - Objects
72/303
olutions
)nit* rinci+les of 1bject/1riented
rora!!in
To+ic* Constructor>--------------------------------------------------------------------->
> Re+ort #AP!"B;3_AIR#_AIN_d >
>--------------------------------------------------------------------->
> call con'tructor of cla'' lcl_air+lane >
>--------------------------------------------------------------------->
REPORT 'a+?cB;3_air'_main_d$
TYPE-POOL# icon$
IN"LDE 'a+?cB;3_air'_d$
#AA: r_lane +$ ;$% ll_a"rlane6
lane_l"t +$ AL$ % ;$% ll_a"rlane.
#TART-O0-#ELE"TION$
>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
lcl_air+lane6,di'+la@_n_o_air+lane'8 9$
C;$A$ E$C r_lane exrt"ng "m_name = DL? erl"nD
"m_lanete = D9-900D.
APPEND r_+lane TO +lane_li't$
C;$A$ E$C r_lane exrt"ng "m_name = DAA 'eF rkD
"m_lanete = D7-100D.
LOOP AT +lane_li't INTO r_+lane$
r_+lane-,di'+la@_attri?ute'8 9$
ENDLOOP$
lcl_air+lane6,di'+la@_n_o_air+lane'8 9$
SAP AG TAW10 9-5/
8/12/2019 TAW 10 (2-2) - Objects
73/303
>--------------------------------------------------------------------->
> Include #AP!"B;3_AIR#_d >
>--------------------------------------------------------------------->
>------------------------------------------------------------------>
> "LA## lcl_air+lane DE0INITION >
>------------------------------------------------------------------>
"LA## lcl_air+lane DE0INITION$
P!LI" #E"TION$
--------------------------------
"ON#TANT#2 +o'_3 TYPE i %ALE F;$
$?#S: ntrutr &+;&'
"m_name +$ tr"ng
"m_lanete +$ alane-lanete6
"la_attr"5ute.
"LA##-ET/OD#2 di'+la@_n_o_air+lane'$
PRI%ATE #E"TION$
----------------------------------
DATA2 name TYPE 'tringG
+lanet@+e TYPE 'a+lane-+lanet@+e$
"LA##-DATA2 n_o_air+lane' TYPE i$
END"LA##$
>------------------------------------------------------------------>
> "LA## lcl_air+lane IPLEENTATION >
>------------------------------------------------------------------>
"LA## lcl_air+lane IPLEENTATION$
$?# ntrutr.
name = "m_name.
lanete = "m_lanete.
n__a"rlane = n__a"rlane G 1.
$'#$?#.
ET/OD di'+la@_attri?ute'$
7RITE2 H icon_5'_+lane a' iconG
H Name of air+lane8;;39G AT +o'_3 nameG
H Air+lane t@+e2 8;;49G AT +o'_3 +lanet@+e$
ENDET/OD$
ET/OD di'+la@_n_o_air+lane'$
7RITE2 HG H Num?er of air+lane'2 8ca39G
AT +o'_3 n_o_air+lane' LE0T-1#TI0IEDG H$
ENDET/OD$
END"LA##$
SAP AG TAW10 9-55
8/12/2019 TAW 10 (2-2) - Objects
74/303
olutions
)nit* rinci+les of 1bject/1riented
rora!!in
To+ic* Callin a rivate ,ethod
>--------------------------------------------------------------------->
> Re+ort #AP!"B;3_AIR#_AIN_e >
> >
>--------------------------------------------------------------------->
> in'ide di'+la@_attri?ute' a +rivate method i' called to get >
> further detail' on technical a'+ect' of the +lanet@+e >
>--------------------------------------------------------------------->
REPORT 'a+?cB;3_air'_main_e$
TYPE-POOL# icon$
IN"LDE 'a+?cB;3_air'_e$
DATA2 r_+lane TYPE RE0 TO lcl_air+laneG
+lane_li't TYPE TA!LE O0 RE0 TO lcl_air+lane$
#TART-O0-#ELE"TION$
>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
lcl_air+lane6,di'+la@_n_o_air+lane'8 9$
"REATE O!1E"T r_+lane e*+orting im_name 6 L/ !erlin
im_+lanet@+e 6 JBJ-B;;$
APPEND r_+lane TO +lane_li't$
r_+lane-,di'+la@_attri?ute'8 9$
"REATE O!1E"T r_+lane e*+orting im_name 6 AA Ne5 York
im_+lanet@+e 6 J4J-4;;$
r_+lane-,di'+la@_attri?ute'8 9$
lcl_air+lane6,di'+la@_n_o_air+lane'8 9$
>--------------------------------------------------------------------->
> Include #AP!"B;3_AIR#_e >
>--------------------------------------------------------------------->
>------------------------------------------------------------------>
> "LA## lcl_air+lane DE0INITION >
>------------------------------------------------------------------>
"LA## lcl_air+lane DE0INITION$
P!LI" #E"TION$
--------------------------------
SAP AG TAW10 9-59
8/12/2019 TAW 10 (2-2) - Objects
75/303
"ON#TANT#2 +o'_3 TYPE i %ALE F;$
ET/OD#2 con'tructor IPORTIN.
im_name TYPE 'tring
im_+lanet@+e TYPE 'a+lane-+lanet@+eG
di'+la@_attri?ute'$
"LA##-ET/OD#2 di'+la@_n_o_air+lane'$
PRI%ATE #E"TION$
----------------------------------
$?#S: get_te!n"al_attr"5ute
&+;&' "m_te te alane-lanete
$B+;&' ex_Fe"g!t +$ _lan_Fe"
ex_tanka +$ _aa"t.
DATA2 name TYPE 'tringG
+lanet@+e TYPE 'a+lane-+lanet@+e$
"LA##-DATA2 n_o_air+lane' TYPE i$
END"LA##$ lcl_air+lane DE0INITION
SAP AG TAW10 9-5:
8/12/2019 TAW 10 (2-2) - Objects
76/303
>------------------------------------------------------------------>
> "LA## lcl_air+lane IPLEENTATION >
>------------------------------------------------------------------>
"LA## lcl_air+lane IPLEENTATION$
ET/OD con'tructor$ name 6 im_name$
+lanet@+e 6 im_+lanet@+e$
n_o_air+lane' 6 n_o_air+lane' 3$
ENDET/OD$ con'tructor
ET/OD di'+la@_attri?ute'$
data2 5eight t@+e 'a+lane-5eightG
ca+ t@+e 'a+lane-tankca+$
7RITE2 H icon_5'_+lane A# I"ONG
H Name of air+lane8;;39G AT +o'_3 nameG
H Air+lane t@+e2 8;;49G AT +o'_3 +lanet@+e$
get_technical_attri?ute'8 e*+orting im_t@+e 6 +lanet@+e
im+orting e*_5eight 6 5eight
e*_tankca+ 6 ca+ 9$
5rite2 H 5height2 8;;F9G 5eightG
tankca+2 8;;B9G ; ca+$
ENDET/OD$ di'+la@_attri?ute'
ET/OD di'+la@_n_o_air+lane'$
7RITE2 HG H Num?er of air+lane'2 8ca39G
AT +o'_3 n_o_air+lane' LE0T-1#TI0IEDG H$ ENDET/OD$ di'+la@_n_o_air+lane'
$?# get_te!n"al_attr"5ute.
S$L$C S&'L$ Fe"g!t tanka %; alane
&' (ex_Fe"g!t6 ex_tanka)
M?$;$ lanete = "m_te.
&% -u5r ,> 0.
ex_Fe"g!t = 100000.
ex_tanka = 10000.
$'#&%. $'#$?#. get_te!n"al_attr"5ute
END"LA##$ lcl_air+lane IPLEENTATION
SAP AG TAW10 9-5;
8/12/2019 TAW 10 (2-2) - Objects
77/303
2
SAP AG 2002
.enerali1ation 7 speciali1ation of classes
Contents:
0nheritance
SAP AG TAW10 :-1
8/12/2019 TAW 10 (2-2) - Objects
78/303
2.2
SAP AG 2002
Define an inheritance relationship beteen
classes
-edefine methods
At the conclusion of this unit, you ill be able to:
0nheritance: Unit !bjecti"es
SAP AG TAW10 :-2
8/12/2019 TAW 10 (2-2) - Objects
79/303
2.3
SAP AG 2002
lcl'truc(
& ma4'cargo
& n'o'tan(s
& n'o'heels
=
=get'cargo
0nheritance: UML 34ample
lcl'"ehicle
& ma(e
& modell
& ser'no
& color
& n'o'"ehicles
=constructor
=display'attributes
=get'count
lcl'car
& car'type
& ma4'seats
& accelertion
=
=get'type
lcl'bus
& ma4'passengers
& la"atory
& tele"ision
=
=get'passengers
-is a-
relationship
Inher!tance !s a relat!onsh!", !n wh!ch one class the subclass !nher!ts all the $a!n character!st!cs o%
another class the su"erclass# The subclass can also add new co$"onents attr!butes, $ethods, and
so on and re"lace !nher!ted $ethods w!th !ts own !$"le$entat!ons#
Inher!tance !s an !$"le$entat!on relat!onsh!" that e$"has!*es s!$!lar!t!es between classes# In the
ea$"le aboe, the s!$!lar!t!es between the car, bus, and truc. classes are etracted to the eh!cle
su"erclass# Th!s $eans that co$$on co$"onents are onl& de%!ned?!$"le$ented !n the su"erclass and
are auto$at!call& "resent !n the subclasses#
The !nher!tance relat!onsh!" !s o%ten descr!bed as an !s a relat!onsh!"6 A truc. is aeh!cle#
SAP AG TAW10 :-3
8/12/2019 TAW 10 (2-2) - Objects
80/303
2.4
SAP AG 2002
Multiple 0nheritance?
Specialization
Generalization
3o
multiple inheritance
lcl'super$ lcl'super/
lcl'sub$ lcl'sub/ lcl'subou can, howeer, s!$ulate $ult!"le !nher!tance !n AAP 8bjects us!n' !nter%aces see the sect!on
on !nter%aces#
S!n'le !nher!tance does not $ean that the !nher!tance tree onl& has one leel# 8n the contrar&, the
d!rect su"erclass o% one class can !n turn be the subclass o% a %urther su"erclass# In other words6 The
!nher!tance tree can hae an& nu$ber o% leels, so that a class can !nher!t %ro$ seeral su"erclasses,
as lon' as !t onl& has one d!rect su"erclass# Inher!tance !s a one-s!ded relat!onsh!"6 Subclasses .now the!r d!rect su"erclasses, but
su"erclasses do not .now the!r subclasses#
SAP AG TAW10 :-/
8/12/2019 TAW 10 (2-2) - Objects
81/303
8/12/2019 TAW 10 (2-2) - Objects
82/303
2.6
SAP AG 2002
0nheritance: Synta4
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'.$?#S: et"mate_fuel
&+;&' "m_"tane +$ _"tane;$4;'&' 3AL4$(re_fuel) +$ t_fuel.
+;&3A$ S$C&'.#AA: make +$ tr"ng6
$$$
$'#CLASS.
CLASS ll_truk #$%&'&&' &'?$;&&' %;ll_ve!"le.
+4L&C S$C&'.$?#S: get_arg ;$4;'&' 3AL4$(re_arg) +$ t_arg.
+;&3A$ S$C&'.#AA: max_arg +$ t_arg.
$'#CLASS.
Hor$all& the onl& other entr& re)u!red %or subclasses !s what has chan'ed !n relat!on to the d!rect
su"erclass# 8nl& additionsare "er$!tted !n AAP 8bjects, that !s, !n a subclass &ou can neer ta.e
so$eth!n' awa& %ro$ a su"erclass# All co$"onents %ro$ the su"erclass are auto$at!call& "resent !n
the subclass#
The attr!butes o% the su"erclass lcl_vehiclee!st !n the subclass lcl_truckC the $ethod
e'timate_fuel!s also aa!lable !n the subclass#
The subclass de%!nes a $ethod get_cargo# It !s not !s!ble !n the su"erclass#
SAP AG TAW10 :-9
8/12/2019 TAW 10 (2-2) - Objects
83/303
2.7
SAP AG 2002
CLASS ll_truk &+L$$'A&'.$?# et"mate_fuel.
...uer->et"mate_fuel(...)
$'#$?#.$'#CLASS.
-edefining Methods
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.$?#S et"mate_fuel
&+;&' "m_"tane +$ t_"tane;$4;'&' 3AL4$(re_fuel) +$ t_fuel.
$'#CLASS.
CLASS ll_truk #$%&'&&' &'?$;&&' %; ll_ve!"le.+4L&C S$C&'.$?#S et"mate_fuel ;$#$%&'&&'....
$'#CLASS.
,annot chan%ethe inter'ace
.e/
implementation o'
method0l/a!s points to the
superclass component
The REDE0INITIONstate$ent %or the !nher!ted $ethod $ust be !n the sa$e #E"TIONas the
de%!n!t!on o% the or!'!nal $ethod# It can there%ore not be !n the PRI%ATE #E"TION, s!nce a class7s
"r!ate $ethods are not !s!ble and there%ore cannot be rede%!ned !n subclasses#
I% &ou rede%!ne a $ethod, &ou do not need to enter !ts !nter%ace a'a!n !n the subclass, but onl& the
na$e o% the $ethod# The reason %or th!s !s that AAP 8bjects does not su""ort oerload!n'#
In the case o% rede%!ned $ethods, chan'!n' the !nter%ace overloadin !s not "er$!ttedC ece"t!on6
8erload!n' !s "oss!ble w!th the constructor#
W!th!n the rede%!ned $ethod, &ou can access co$"onents o% the d!rect su"erclass us!n' the #PER
re%erence#
To !$"le$ent a rede%!ned $ethod !n a subclass, &ou o%ten need to call the $ethod o% the sa$e na$e
!n the immediatesu"erclass# In AAP 8bjects &ou can call the $ethod %ro$ the su"erclass us!n' the"seudo-re%erence 'u+er6
The "seudo-re%erence 'u+ercan onl& be used !n rede%!ned $ethods#
SAP AG TAW10 :-:
8/12/2019 TAW 10 (2-2) - Objects
84/303
2.8
SAP AG 2002
0nheritance and -edefining the Constructor
CLASS ll_ve!"le #$%&'&&'.+4L&C S$C&'.$?#S: ntrutr &+;&'
"m_make +$ tr"ng.$'#CLASS.
CLASS ll_truk #$%&'&&' &'?$;&&' %; ll_ve!"le.+4L&C S$C&'.$?#S: ntrutr &+;&' "m_make +$ tr"ng
"m_arg +$ t_arg.+;&3A$ S$C&'.#AA: max_arg +$ t_arg.
$'#CLASS.
CLASS ll_ve!"le &+L$$'A&'.$?# C'S;4C;.make = "m_make.$'#$?#.
$'#CLASS.
CLASS ll_truk &+L$$'A&'.$?# ntrutr.
CALL $?# uer->ntrutr( "m_make = "m_make ).max_arg = "m_arg.$'#$?#.
$'#CLASS.
,an chan%e theinter'ace
,all constructor o' immediate
superclass
The constructor o% the su"erclass !ustbe called w!th!n the constructor o% the subclass# The reason
%or th!s !s the s"ec!al %unct!on o% the constructor6 To ensure that objects are !n!t!al!*ed correctl 8nl&
the class !tsel%, howeer, can !n!t!al!*e !ts own "r!ate co$"onents correctl&C th!s tas. cannot be
carr!ed out b& the subclass# There%ore !t !s essent!al that all !nstance constructors are called !n an
!nher!tance h!erarch& !n the correct se)uence#
+or stat!c constructors, unl!.e !nstance constructors, the stat!c constructor !n the su"erclass !s called
auto$at!call&, that !s the runt!$e s&ste$ auto$at!call& ensures that the stat!c constructors o% all !ts
su"erclasses hae alread& been eecuted be%ore the stat!c constructor !n a "art!cular class !s eecuted#
SAP AG TAW10 :-;
8/12/2019 TAW 10 (2-2) - Objects
85/303
8/12/2019 TAW 10 (2-2) - Objects
86/303
2.#0
SAP AG 2002
CLASS ll_ve!"le #$%&'&&'.
+4L&C S$C&'. $$$
+;$C$# S$C&'.#AA tank +$ ;$% ll_tank.
+;&3A$ S$C&'.#AA make +$ tr"ng.
$'#CLASS.
0nheritance and 6isibility
*ublic components
6isible to all
Direct access
*rotected components
!nly "isible ithin the class
and its subclasses
*ri"ate components
!nly "isible ithin the class
8o access from outside the
class,
not e"en from the subclass
lcl'"ehicle& ma(e
& modell
G tan(
...
=constructor
=display'attributes
=get'count
, public
4 protected
- private
Inher!tance "ro!des an etens!on o% the !s!b!l!t& conce"t6 There are "rotected co$"onents# The
!s!b!l!t& o% these co$"onents l!es between that o% the "ubl!c co$"onents !s!ble to all users, all
subclasses, and the class !tsel%, and "r!ate !s!ble onl& to the class !tsel%# Protected co$"onents
are !s!ble to and can be used b& all subclasses and the class !tsel%#
Subclasses cannot access the "r!ate co$"onents "art!cularl& attr!butes o% the su"erclass# Pr!ate
co$"onents are 'enu!nel& "r!ate# Th!s !s "art!cularl& !$"ortant !% a su"erclass needs to $a.e local
enhance$ents to handle errors6 It can use "r!ate co$"onents to do th!s w!thout .now!n' or
!nal!dat!n' subclasses#
In AAP 8bjects, &ou $ust .ee" to the sect!on se)uence P!LI", PROTE"TED, PRI%ATE#
SAP AG TAW10 :-10
8/12/2019 TAW 10 (2-2) - Objects
87/303
2.##
SAP AG 2002
(5) lcl'bus
PrivatePublic
Protected
,an be addressed
-'rom outside- 'or
all clients
,an onl! be
addressed /ithin
the class
Inherited
tank
get_makeet_makeget_unt"la_attr"5uteet"mate_fuel
Inherited
6isibility of *rotected Components
In th!s ea$"le, lcl_?u', a subclass o% lcl_vehicle, can d!rectl& access the "rotected attr!bute
tank# I% the attr!bute was "r!ate, the subclasses would onl& be able to access tankus!n' non-
"r!ate $ethods#
SAP AG TAW10 :-11
8/12/2019 TAW 10 (2-2) - Objects
88/303
2.#2
SAP AG 2002
-ules for -edefining Methods
0nherited methods can be redefined in subclasses -edefined methods must be re&implemented in subclasses
)he signature of redefined methods cannot be changed
Static methods cannot be redefined
0n inheritance, static components are FsharedF:
A class shares its non&pri"ate static attributes ith all its subclasses
lcl'"ehicles
lcl'car lcl'truc( lcl'bus
et"mate_fuel
5he method is implemented in di!!erent wa#s
et"mate_fuel et"mate_fuel et"mate_fuel
In AAP 8bjects, &ou can not onl& add new co$"onents, but also "ro!de !nher!ted $ethods w!th
new !$"le$entat!ons# Th!s !s .nown as redefinition# >ou can onl& rede%!ne "ubl!c and "rotected
!nstance $ethods, other co$"onents stat!c $ethods, attr!butes and so on cannot be rede%!ned#
@han'es to $ethod "ara$eters s!'nature chan'es are not "oss!ble#
In 4, the rede%!n!t!on o% a $ethod !s re"resented b& l!st!n' the $ethod a'a!n !n the subclass#
4ethods and all other co$"onents that are !nher!ted but not rede%!ned are not l!sted !n the subclass,
as the!r e!stence there !s clear %ro$ the s"ec!al!*at!on relat!onsh!"#
>ou should not con%use rede%!n!t!on w!th oerload!n'# The latter descr!bes the ab!l!t& o% a class to
hae $ethods w!th the sa$e na$e but a d!%%erent s!'nature# Th!s !s not aa!lable !n AAP 8bjects#
There !s onl& one stat!c eent "er roll area# In th!s wa&, a class that de%!nes a "ubl!c or "rotected
stat!c attr!bute shares th!s attr!bute w!th all !ts subclasses# The s!'n!%!cant "o!nt here !s that subclassesdo not each rece!e the!r own co"& o% the stat!c attr!bute#
SAP AG TAW10 :-12
8/12/2019 TAW 10 (2-2) - Objects
89/303
2.#3
SAP AG 2002
-edefining Methods: 34ample
$?# et"mate_fuel.#AA: ttal_Fe"g!t ...> =u't an e*am+leM
ttal_Fe"g!t = max_arg GFe"g!t.
re_fuel = ttal_Fe"g!t *"m_"tane * fatr.
$'#$?#.
$?# et"mate_fuel.#AA: ttal_Fe"g!t ...> =u't an e*am+leM
ttal_Fe"g!t = max_aenger *average_Fe"g!t G Fe"g!t.
re_fuel = ttal_Fe"g!t *"m_"tane * fatr.
$'#$?#.
lcl'bus
& ma4'passengers
...
= constructor
= estimate'fuel
lcl'truc(
& ma4'cargo
...
= constructor
= estimate'fuel
In the aboe ea$"le, both rede%!ned $ethods calculate the return code !n d!%%erent wa&s# The
!$"ortant "o!nt !s that the se$ant!cs sta& the sa$e#
SAP AG TAW10 :-13
8/12/2019 TAW 10 (2-2) - Objects
90/303
2.#4
SAP AG 2002
Define an inheritance relationship beteen
classes
-edefine methods
5ou are no able to:
0nheritance: Unit Summary
SAP AG TAW10 :-1/
8/12/2019 TAW 10 (2-2) - Objects
91/303
2.#5 nheritance
'(ercises
)nit* nheritance
To+ic* Creatin Class ierarchies
At the conclus!on o% these eerc!ses, &ou w!ll be able to6
e%!ne subclasses
Dede%!ne su"erclass $ethods !n subclasses
,odel solution* SA+C901_&'?S_A&'_A
SA+C901_&'?S_A !nclude "ro'ra$
our +rora!* JC901_KK_A&'_A&;+LA'$JC901_KK_A&;+LA'$ !nclude "ro'ra$
1-1 4a.e both !nstance attr!butes o% the class lcl_airplane!s!ble to the!r subclasses
PDI=AT( S(@TI8H - PD8T(@T( S(@TI8H#
1-2 @reate the subclass lcl_passenger_plane%or the class lcl_airplane# Also, create
th!s subclass !n &our !nclude "ro'ra$#
1-2-1 The class !s to hae a "r!ate !nstance attr!bute ma!_seatsw!th the sa$e
t&"e as table %!eldsflight-seatsma!#
1-2-2 A "ubl!c constructor !s to be de%!ned and !$"le$ented !n the class# Th!s
constructor "ro!des all!nstance attr!butes !n the class w!th alues#
1-2-3 Dede%!ne the $ethod display_attributes o% the class lcl_airplane, so that,
us!n' the rede%!ned $ethod, the WDIT( state$ent d!s"la&s all!nstance
attr!butes#
1-3 @reate the subclass lcl_cargo_plane%or the class lcl_airplane# Also, create th!ssubclass !n &our !nclude "ro'ra$#
1-3-1 The class !s to hae a "r!ate !nstance attr!bute ma!_cargow!th the
sa$e t&"e as the table %!eldscplane-cargoma!#
1-3-2 A "ubl!c constructor !s to be de%!ned and !$"le$ented !n the class# Th!s
constructor "ro!des all!nstance attr!butes !n the class w!th alues#
1-3-3 Dede%!ne the $ethod display_attributes o% the class lcl_airplane, so that,
us!n' the rede%!ned $ethod, the WDIT( state$ent d!s"la&s all!nstance
attr!butes#
SAP AG TAW10 :-15
8/12/2019 TAW 10 (2-2) - Objects
92/303
8/12/2019 TAW 10 (2-2) - Objects
93/303
2.#6 nheritance olutions
)nit* nheritance
To+ic* Creatin Class ierarchies
>--------------------------------------------------------------------->
> Re+ort #AP!"B;3_IN/#_AIN_a >
>--------------------------------------------------------------------->
> the cla''e' lcl_+a''enger_+lane and lcl_cargo_+lane are >
> in'tantiated$ Inheritance i' 'ho5n >
>--------------------------------------------------------------------->
REPORT 'a+?cB;3_inh'_main_a$
IN"LDE )icon,$
IN"LDE 'a+?cB;3_inh'_a$
#AA: r_lane +$ ;$% ll_a"rlane6
r_arg te ref t ll_arg_lane6
r_aenger te ref t ll_aenger_lane6
lane_l"t +$ AL$ % ;$% ll_a"rlane.
#TART-O0-#ELE"TION$
>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
lcl_air+lane6,di'+la@_n_o_air+lane'8 9$
C;$A$ E$C r_aenger $B+;&'
"m_name = DL? $;L&'D
"m_lanete = D9-900D
"m_eat = 79.
C;$A$ E$C r_arg $B+;&'
"m_name = D4S ?$ruleD "m_lanete = D9-00D
"m_arg = 77.
r_arg->"la_attr"5ute( ).
r_aenger->"la_attr"5ute( ).
lcl_air+lane6,di'+la@_n_o_air+lane'8 9$
SAP AG TAW10 :-1:
8/12/2019 TAW 10 (2-2) - Objects
94/303
>--------------------------------------------------------------------->
> Include #AP!"B;3_IN/#_a >
>--------------------------------------------------------------------->
>------------------------------------------------------------------>
> "LA## lcl_air+lane DE0INITION >
>------------------------------------------------------------------>
>$$$
>--------------------------------------------------------------------->
> "LA## lcl_cargo_+lane DE0INITION
>--------------------------------------------------------------------->
>
>--------------------------------------------------------------------->
CLASS ll_arg_lane #$%&'&&' &'?$;&&' %; ll_a"rlane.
+4L&C S$C&'.
----------------------
$?#S: ntrutr &+;&' "m_name +$ tr"ng
"m_lanete +$ alane-lanete
"m_arg +$ lane-argmax.
$?#S: "la_attr"5ute ;$#$%&'&&'.
+;&3A$ S$C&'.
----------------------
#AA: max_arg +$ lane-argmax.
$'#CLASS. ll_arg_lane #$%&'&&'
>--------------------------------------------------------------------->
> "LA## lcl_cargo_+lane IPLEENTATION
>--------------------------------------------------------------------->
>
>--------------------------------------------------------------------->
CLASS ll_arg_lane &+L$$'A&'.
$?# ntrutr. CALL $?# uer->ntrutr( "m_name = "m_name "m_lanete = "m_lanete ).
max_arg = "m_arg.
$'#$?#. ntrutr
$?# "la_attr"5ute.
uer->"la_attr"5ute( ).
M;&$: / Dax Carg = D6 max_arg.
4L&'$.
$'#$?#. "la_attr"5ute
$'#CLASS. ll_arg_lane &+L$$'A&'
SAP AG TAW10 :-1;
8/12/2019 TAW 10 (2-2) - Objects
95/303
>--------------------------------------------------------------------->
> "LA## lcl_+a''enger_+lane DE0INITION
>--------------------------------------------------------------------->
>
>--------------------------------------------------------------------->
CLASS ll_aenger_lane #$%&'&&' &'?$;&&' %; ll_a"rlane..
+4L&C S$C&'.
$?#S: ntrutr &+;&' "m_name +$ tr"ng
"m_lanete +$ alane-lanete
"m_eat +$ fl"g!t-eatmax.
$?#S: "la_attr"5ute ;$#$%&'&&'.
+;&3A$ S$C&'.
#AA: max_eat +$ fl"g!t-eatmax.
$'#CLASS. ll_aenger_lane #$%&'&&'
>--------------------------------------------------------------------->
> "LA## lcl_+a''enger_+lane IPLEENTATION
>--------------------------------------------------------------------->
>
>--------------------------------------------------------------------->
CLASS ll_aenger_lane &+L$$'A&'.
$?# ntrutr.
CALL $?# uer->ntrutr( "m_name = "m_name "m_lanete = "m_
Recommended