© IBM Corporation 2013
© IBM Corporation 2013
ABAP Objects Advanced -Inheritance
ABAP Objects Advanced : Inheritance Topics
ABAP Objects Advanced : Inheritance Topics
ConceptConcept
Snta! and "isibilitSnta! and "isibilit
Abstract Classes and Met#ods Abstract Classes and Met#ods
$inal Classes and Met#ods$inal Classes and Met#ods
Static Co%ponents in In#eritanceStatic Co%ponents in In#eritance
Met#od &ede'initionMet#od &ede'inition
Constructor in In#eritanceConstructor in In#eritance
Object &e'erences in In#eritanceObject &e'erences in In#eritance
Inheritance : Concept
Inheritance : Concept
In#eritance is one o' tIn#eritance is one o' t#e %ost po)er'ul 'eature o' object #e %ost po)er'ul 'eature o' object oriented pro(ra%%in(*oriented pro(ra%%in(*
In#eritance is t#e process o' creatin( ne) classes+ called derived classes or SubIn#eritance is t#e process o' creatin( ne) classes+ called derived classes or Sub Classes or c#ild classes 'ro% e!istin( classes c
Classes or c#ild classes 'ro% e!istin( classes called base classes or Superalled base classes or Super Classes or parent classes*
Classes or parent classes*
,#e derived class in#erits all t#e capabilities o' t#e base class but ,#e derived class in#erits all t#e capabilities o' t#e base class but can addcan add e%bellis#%ents and re'ine%ents o' its
e%bellis#%ents and re'ine%ents o' its o)n* ,#e base class is unc#an(ed b to)n* ,#e base class is unc#an(ed b t#is#is process*
process*
Grand Parent Class Grand Parent Class
Parent Class Parent Class
C#ild Class C#ild Class
Sin(le In#eritance supported Sin(le In#eritance supported
b ABAP Objects b ABAP Objects
Parent Class
Parent Class Parent ClassParent Class
C#ild Class C#ild Class
Multiple In#eritance not
Multiple In#eritance not
supported b ABAP
supported b ABAP
Objects
Inheritance : Concept !Contd"#
Inheritance : Concept !Contd"#
,#e relations#ip bet)een t#e,#e relations#ip bet)een t#e base classes and t#e derived base classes and t#e derived classes can be
classes can be represented in anrepresented in an In#eritance tree* #ere base
In#eritance tree* #ere base
classes o' eac# derived class can classes o' eac# derived class can be retraced alon( an uni.ue pat# be retraced alon( an uni.ue pat# to a sin(le root node o' t#e tree* to a sin(le root node o' t#e tree*
A Sup A Super Class is a Generali/ationer Class is a Generali/ation o' its Sub Classes
o' its Sub Classes and on t#eand on t#e ot#er #and a Sub Class is a ot#er #and a Sub Class is a
Speciali/ation o' its Super Class* Speciali/ation o' its Super Class*
$%per C&ass $%per C&ass P%b&ic: P%b&ic: Protected: Protected: Private: Private: C&ass C1 C&ass C1 P%b&ic: P%b&ic: Protected: Protected: Private: Private: C&ass C2 C&ass C2 P%b&ic: P%b&ic: Protected: Protected: Private: Private: C&ass C3 C&ass C3 &oot node &oot node Generali/ed Generali/ed Class Class Speciali/ed Speciali/ed Class Class
More speciali/ed class More speciali/ed class ' ' e e n n e e r r a a & & i i ( ( a a t t i i o o n n
$ $ p p e e c c i i a a & & i i ( ( a a t t i
Inheritance:
Inheritance:
Adva
Adva
ntaes
ntaes
In#eritance per%its codeIn#eritance per%its code +e%sabi&it.+e%sabi&it.* &eusin( e!istin( code re%oves code* &eusin( e!istin( code re%oves code redundanc and saves ti%e %one*
redundanc and saves ti%e %one*
Co%%on co%ponents e!ist onl in Co%%on co%ponents e!ist onl in t#e Super Class and t#e Super Class and %aintained%aintained Centra&&.Centra&&.**
Once a base class is )ritten and tested+ it Once a base class is )ritten and tested+ it need not be touc#ed a(ain* ,#isneed not be touc#ed a(ain* ,#is increases pro(ra%
Inheritance : $.nta and isibi&it.
Inheritance : $.nta and isibi&it.
CA$$ s%perc&ass 456I7ITIO7"
CA$$ s%perc&ass 456I7ITIO7"
P8BIC $5CTIO7" P8BIC $5CTIO7" P%b&ic co9ponents P%b&ic co9ponents P+OT5CT54 $5CTIO7" P+OT5CT54 $5CTIO7" Protected co9ponents Protected co9ponents P+IAT5 $5CTIO7" P+IAT5 $5CTIO7" Private co9ponents Private co9ponents 574CA$$" 574CA$$"
CA$$ s%bc&ass 456I7ITIO7 I75+ITI7' 6+OM
CA$$ s%bc&ass 456I7ITIO7 I75+ITI7' 6+OM
s%perc&ass" s%perc&ass" P8BIC $5CTIO7" P8BIC $5CTIO7" P%b&ic co9ponents P%b&ic co9ponents P%b&ic co9ponents P%b&ic co9ponents !s%perc&ass# !s%perc&ass# P+OT5CT54 $5CTIO7" P+OT5CT54 $5CTIO7" Protected co9ponents Protected co9ponents Protected co9ponents Protected co9ponents !s%perc&ass# !s%perc&ass# P+IAT5 $5CTIO7" P+IAT5 $5CTIO7" Private section Private section
,#e public and protected,#e public and protected
co%ponents o' t#e Super Class are co%ponents o' t#e Super Class are visible in t#e Sub Class*
visible in t#e Sub Class*
Private section o' t#e Super Class isPrivate section o' t#e Super Class is not visible in t#e Sub Classes*
not visible in t#e Sub Classes*
Private co%ponents o' t#e SubPrivate co%ponents o' t#e Sub Class can #ave sa%e na%e as t#e Class can #ave sa%e na%e as t#e private co%ponent o' t#e Super private co%ponent o' t#e Super Class*
Class*
Public and Protected co%ponents o'Public and Protected co%ponents o' t#e Sub Class can not #ave t#e
t#e Sub Class can not #ave t#e
sa%e na%e as t#at #ave been used sa%e na%e as t#at #ave been used in Super Class*
Inheritance : Abstract c&asses and 9ethods
Inheritance : Abstract c&asses and 9ethods
CA$$ c&s%per 456I7ITIO7"
CA$$ c&s%per 456I7ITIO7"
P8BIC
P8BIC $5CTIO7"$5CTIO7"
M5TO4$:
M5TO4$: de9o1 de9o1 AB$T+AAB$T+ACT<CT<
de9o2"
de9o2"
574CA$$"
574CA$$"
CA$$ c&s%b 456I7ITIO7
CA$$ c&s%b 456I7ITIO7
I75+ITI7' 6+OM c&s%per"
I75+ITI7' 6+OM c&s%per"
P8BIC
P8BIC $5CTIO7"$5CTIO7"
M5TO4$
M5TO4$ de9o1 de9o1 +5456I7ITIO7"+5456I7ITIO7"
574CA$$"
574CA$$"
A class de'in A class de'ined ased as AB$T+ACTAB$T+ACT cannot be cannot be instantiated + t#at is one cannot use
instantiated + t#at is one cannot use C+5AT5C+5AT5 OB=5CT
OB=5CT )it# re'erence to t#e class* )it# re'erence to t#e class*
Abstract class can onl be accessed usin( its Abstract class can onl be accessed usin( its Static Co%ponents or its Sub Classes*
Static Co%ponents or its Sub Classes*
Abstract class serves as a te%plate 'or Sub Abstract class serves as a te%plate 'or Sub Classes
Classes
I' a class I' a class contains an abstract %et#od t#econtains an abstract %et#od t#e )#ole class beco%es abstract*
)#ole class beco%es abstract*
A %et#o A %et#od+ )#ic# is abstract+ s#ould bed+ )#ic# is abstract+ s#ould be rede'ined in derived class*
rede'ined in derived class*
An Ab An Abstract class can declare and i%ple%entstract class can declare and i%ple%ent non abstract %et#ods )#ic# its Sub Classes non abstract %et#ods )#ic# its Sub Classes can use )it# or )it#out rede'inin( t#e%*
can use )it# or )it#out rede'inin( t#e%*
+ede>inin the Abstract 9ethod o> +ede>inin the Abstract 9ethod o> $%per C&ass in the $%b C&ass
$%per C&ass in the $%b C&ass Abstract 9ethod
Inheritance : 6ina& c&asses and 9ethods
Inheritance : 6ina& c&asses and 9ethods
A A >ina&>ina& class can not be class can not be inheritedinherited 'urt#er*
'urt#er*
All Met#ods o' a All Met#ods o' a >ina&>ina& class areclass are in#erentl 'inal and %ust not be in#erentl 'inal and %ust not be declared as
declared as >ina&>ina& in t#e class in t#e class de'inition*
de'inition*
A A >ina&>ina& %et#od can not be%et#od can not be rede>inedrede>ined 'urt#er*
'urt#er*
I' a %et#od o' a non-'inal class is I' a %et#od o' a non-'inal class is 'inal'inal t#en t#at class can be in#erited but t#en t#at class can be in#erited but t#at %et#od cannot be rede'ined* t#at %et#od cannot be rede'ined*
CA$$
CA$$ >ina&c&ass 456>ina&c&ass 456I7ITIO7 I7ITIO7 6I7A"6I7A"
" " " " " " " "
" " " " " " " "
574CA$$"
574CA$$"
CA$$
CA$$ derivedc&ass derivedc&ass 456I7ITIO7456I7ITIO7
I75+ITI7' 6+OM >ina&c&ass "
I75+ITI7' 6+OM >ina&c&ass "
"
" "" "" " " " " " " " " " " " " ""
574CA$$"
574CA$$"
CA$$ s%perc&ass 456I7ITIO7 "
CA$$ s%perc&ass 456I7ITIO7 "
" " " " " " " "
" " " " " " " "
M5TO4$
M5TO4$ >ina&9ethod >ina&9ethod 6I7A"6I7A"
574CA$$"
574CA$$"
CA$$
CA$$ s%bc&ass s%bc&ass 456I7ITIO7 456I7ITIO7 I75+ITI7'I75+ITI7'
6+OM s%perc&ass "
6+OM s%perc&ass "
"
" "" "" " " " " " " " " " " " " ""
M5TO4$
M5TO4$ >ina&9ethod >ina&9ethod rede>inition"rede>inition"
574CA$$"
Inheritance : $tatic co9ponents
Inheritance : $tatic co9ponents
,#e,#e p%b&icp%b&ic and and protectedprotected static static
co%ponents 1%et#ods attributes3 o' co%ponents 1%et#ods attributes3 o' t#e Super Class are visible in t#e Sub t#e Super Class are visible in t#e Sub Class*
Class*
Public static co%ponents can bePublic static co%ponents can be accessed t#rou(# t#e class
accessed t#rou(# t#e class
co%ponent selector used )it# an co%ponent selector used )it# an class in t#e respective pat# o'
class in t#e respective pat# o' in#eritance tree*
in#eritance tree*
CA$$ basec&ass 456I7ITIO7"
CA$$ basec&ass 456I7ITIO7"
P8BIC $5CTIO7"
P8BIC $5CTIO7"
CA$$-4
CA$$-4AATTA A : : na9e na9e TP5 $T+I7'TP5 $T+I7'
A
A85 85 ABAP"ABAP"
DDD""
DDD""
574CA$$"
574CA$$"
CA$$
CA$$ derivedc&ass derivedc&ass 456I7ITIO7456I7ITIO7
I75+ITI7' 6+OM basec&ass"
I75+ITI7' 6+OM basec&ass"
" " "" "" " " " " " " " " " " " " "" 574CA$$" 574CA$$" $TA+T-O6-$55CTIO7" $TA+T-O6-$55CTIO7" basec&assEFna9e" basec&assEFna9e" derivedc&assEFna9e" derivedc&assEFna9e" $tatic co9ponent $tatic co9ponent
$tatic co9ponent is accessed $tatic co9ponent is accessed thro%h c&ass co9ponent
thro%h c&ass co9ponent
se&ector >ro9 the derived c&ass se&ector >ro9 the derived c&ass as Ge&& as >ro9 the base c&ass as Ge&& as >ro9 the base c&ass
Inheritance : Method +ede>inition
Inheritance : Method +ede>inition
+ede>inition+ede>inition is )#en t#e is )#en t#e
i%ple%entation o' an in#erited i%ple%entation o' an in#erited instance
instance %et#od is c#an(ed 'or t#e %et#od is c#an(ed 'or t#e Sub Class )it#out c#an(in( its
Sub Class )it#out c#an(in( its si(nature*
si(nature*
$tatic 9ethods cant be rede>ined$tatic 9ethods cant be rede>ined**
,#e para%eters re%ain sa%e in t#e,#e para%eters re%ain sa%e in t#e derived class as it )as in base class* derived class as it )as in base class*
In t#e rede'ined %et#od use tIn t#e rede'ined %et#od use t#e#e $8P5+
$8P5+ pseudo re'erence to access pseudo re'erence to access t#e ori(inal %et#od o' t#e base class* t#e ori(inal %et#od o' t#e base class*
CA$$ basec&ass 456I7ITIO7"
CA$$ basec&ass 456I7ITIO7"
P8BIC $5CTIO7"
P8BIC $5CTIO7"
M5TO4$:
M5TO4$: 9eth"9eth"
" " " " " " " "
" " " " " " " "
574CA$$"
574CA$$"
CA$$
CA$$ derivedc&ass derivedc&ass 456I7ITIO7 456I7ITIO7 I75+ITI7'I75+ITI7'
6+OM basec&ass "
6+OM basec&ass "
P8BIC
P8BIC $5CTIO7"$5CTIO7"
M5TO4$:
M5TO4$: 9eth 9eth +5456I7ITIO7"+5456I7ITIO7"
"
" "" "" " " " " " " " " " " " " ""
574CA$$"
574CA$$"
CA$$ derivedc&ass IMP5M57TATIO7 "
CA$$ derivedc&ass IMP5M57TATIO7 "
M5TO4$ 9eth"
M5TO4$ 9eth"
CA$$
CA$$ M5TO4 M5TO4 $8P5+-F9eth"$8P5+-F9eth"
DDD DDD 574M5TO4" 574M5TO4" 574CA$$" 574CA$$"
+ede>inin the base c&ass 9ethod in the +ede>inin the base c&ass 9ethod in the
derived c&ass derived c&ass
Ca&&in the $%per C&ass 9ethod %sin Ca&&in the $%per C&ass 9ethod %sin
$8P5+ He.Gord $8P5+ He.Gord
Inheritance : Instance Constr%ctors
Inheritance : Instance Constr%ctors
As all public and protected As all public and protected
co%ponents+ Sub Class in#erits t#e co%ponents+ Sub Class in#erits t#e constructor %et#od i' it is present in constructor %et#od i' it is present in t#e in#eritance tree*
t#e in#eritance tree*
I' an object o' Sub Class I' an object o' Sub Class is createdis created at t#is ti%e t#e in#erited instance at t#is ti%e t#e in#erited instance attributes and private attributes o' attributes and private attributes o' t#e Super Classes %ust also be t#e Super Classes %ust also be initiali/ed*
initiali/ed*
As t#e private attributes o' t#e As t#e private attributes o' t#e Super Class is not visible to Sub Super Class is not visible to Sub Class+ Sub Class cannot 'ull Class+ Sub Class cannot 'ull
initiali/e its Super Class* ,#ere'ore initiali/e its Super Class* ,#ere'ore to ensure co%plete initiali/ation o' to ensure co%plete initiali/ation o' Sub Class and its Super Classes+ Sub Class and its Super Classes+ constructor is called*
constructor is called*
CA$$ basec&ass 456I7ITIO7"
CA$$ basec&ass 456I7ITIO7"
P8BIC $5CTIO7"
P8BIC $5CTIO7"
M5TO4$: constr%ctor
M5TO4$: constr%ctor IMPO+TI7' IMPO+TI7' ar1 ar1 TP5 TP5 $T+I7'"$T+I7'"
P+IA
P+IAT5 T5 $5CTIO$5CTIO7"7"
4AT
4ATA: A: >&d >&d TP5 TP5 $T+I7$T+I7'"'"
" "
" " " " " " " " " " " "" "
574CA$$"
574CA$$"
CA$$
CA$$ derivedc&ass 456I7ITIO7 derivedc&ass 456I7ITIO7 I75+ITI7' 6+OMI75+ITI7' 6+OM
basec&ass "
basec&ass "
P8BIC
P8BIC $5CTIO7"$5CTIO7"
M5TO4$:
M5TO4$: constr%ctor constr%ctor IMPO+TI7' IMPO+TI7' ar1 ar1 TP5 TP5 $T+I7'$T+I7'
ar2
ar2 TP5 TP5 $T+I7'"$T+I7'"
P+IA
P+IAT5 T5 $5CTIO$5CTIO7"7"
4AT
4ATA: A: >&d >&d TP5 TP5 $T+I7$T+I7'"'"
" "
" " " " " " " " " " " "" "
574CA$$"
574CA$$"
CA$$ derivedc&ass
CA$$ derivedc&ass IMP5M57TAIMP5M57TATIO7 "TIO7 "
M5TO4$ constr%ctor"
M5TO4$ constr%ctor"
CA
CA M5TO4 $8P5+-Fconstr%ctor M5TO4 $8P5+-Fconstr%ctor
5PO+TI7'
5PO+TI7' ar1 ar1 E E ar1"ar1"
>&d E
>&d E ar2 "ar2 "
DDD DDD 574M5TO4" 574M5TO4" 574CA$$" 574CA$$"
$%per C&ass constr%ctor $%per C&ass constr%ctor
$%b C&ass constr%ctor $%b C&ass constr%ctor
Ca&&in $%per C&ass Ca&&in $%per C&ass
constr%ctor constr%ctor
Inheritance : Instance Constr%ctors !Contd"#
Inheritance : Instance Constr%ctors !Contd"#
#et#er e!pli#et#er e!plicit call is re.uired cit call is re.uired 'or t#e instance constructor 'or t#e instance constructor o' t#e Super Class o' t#e Super Class oror not + )#en )e instantiate an object o' Sub Class is depends on t#e 'ollo)in(
not + )#en )e instantiate an object o' Sub Class is depends on t#e 'ollo)in( conditions4 conditions4 5p&icit constr%ctor is 5p&icit constr%ctor is de>ined in $%per de>ined in $%per C&assJ C&assJ 5p&icit constr%ctor is 5p&icit constr%ctor is de>ined in $%b C&assJ de>ined in $%b C&assJ
5p&icit ca&& reK%iredJ
5p&icit ca&& reK%iredJ
5 5oo 55oo 5o5o 6 6eess 55oo 5o5o 5 5oo 66eess 6e6ess 6 6eess 66eess 6e6ess
Inheritance : $tatic Constr%ctors
Inheritance : $tatic Constr%ctors
,#e rede'inition o' static constructor,#e rede'inition o' static constructor )ors si%ilarl to instance
)ors si%ilarl to instance constructor*
constructor*
#en an static constructor is#en an static constructor is rede'ined t#en &898$I5I,IO5 rede'ined t#en &898$I5I,IO5 addition is not re.uired*
addition is not re.uired*
5o para%eter inter'ace is possible '5o para%eter inter'ace is possible 'oror static constructor 1 )it# or )it#out
static constructor 1 )it# or )it#out in#eritance3
in#eritance3
,#e runti%e environ%ent,#e runti%e environ%ent
auto%aticall ensures t#at t#e static auto%aticall ensures t#at t#e static constructors are called in t#e ri(#t constructors are called in t#e ri(#t order+
order+ so it is so it is not re.uired to call t#enot re.uired to call t#e static constructor o' t#e Super Class static constructor o' t#e Super Class e!plicitl*
e!plicitl*
CA$$ basec&ass
CA$$ basec&ass 456I7ITI456I7ITIO7"O7"
P8BIC $5CTIO7"
P8BIC $5CTIO7"
M5TO4$:
M5TO4$: c&assconsc&assconstr%ctor"tr%ctor"
P+IAT
P+IAT5 5 $5C$5CTIO7"TIO7"
4AT
4ATA: A: >&d >&d TP5 TP5 $T+I7$T+I7'"'"
"
" " " " " " " " " " " " " ""
574CA$$"
574CA$$"
CA$$
CA$$ derivedc&ass derivedc&ass 456I7ITIO7456I7ITIO7
I75+ITI
I75+ITI7' 6+OM 7' 6+OM basec&ass "basec&ass "
P8BIC
P8BIC $5CTIO7"$5CTIO7"
M5TO4$:
M5TO4$: c&assconstr%cc&assconstr%ctor tor ""
P+IAT
P+IAT5 5 $5CTI$5CTIO7"O7"
4AT
4ATA: A: >&d >&d TP5 TP5 $T+I7$T+I7'"'"
"
" " " " " " " " " " " " " ""
574CA$$"
574CA$$"
CA$$ derivedc
CA$$ derivedc&ass IMP&ass IMP5M57TA5M57TATIO7 "TIO7 "
M5TO4$ c&assconstr%ctor" M5TO4$ c&assconstr%ctor" >&d E >&d E I I a9 s%b a9 s%b "" DDD DDD 574M5TO4" 574M5TO4" 574CA$$" 574CA$$"
$%per C&ass constr%ctor $%per C&ass constr%ctor
$%b C&ass constr%ctor $%b C&ass constr%ctor
7o ca&& >or $%per C&ass 7o ca&& >or $%per C&ass constr%ctor
Inheritance : Object +e>erences
Inheritance : Object +e>erences
P%b: a1< a2 P%b: a1< a2 Prot: Prot: b1< b1< b2b2 Priv:c1<c2 Priv:c1<c2 C&ass C1 C&ass C1 P%b: a1< a2<a3<a P%b: a1< a2<a3<a Prot: Prot: b1<b2<b3<bb1<b2<b3<b Priv: c3<c Priv: c3<c
C&ass C2 Inheritin >ro9 C1
C&ass C2 Inheritin >ro9 C1
P%b: a1<
P%b: a1< a2<a3<a<a,<a/a2<a3<a<a,<a/
Prot: Prot: b1< b1< b2<b3<b<b,<b/b2<b3<b<b,<b/ Priv:c,<c/ Priv:c,<c/ 4ATA ore>1 TP5 +56 TO C1" 4ATA ore>1 TP5 +56 TO C1" C+5AT
C+5AT5 OB=5C5 OB=5CT ore>1"T ore>1"
4ATA ore>2 TP5 +56 TO C2" 4ATA ore>2 TP5 +56 TO C2"
C+5AT5 OB=5CT ore>2" C+5AT5 OB=5CT ore>2"
4ATA ore>3 TP5 +56 TO C3" 4ATA ore>3 TP5 +56 TO C3"
C+5AT
C+5AT5 OB=5C5 OB=5CT ore>3"T ore>3" C&ass C3 Inheritin >ro9 C2
Inheritance : Object +e>erences !Contd"#
Inheritance : Object +e>erences !Contd"#
9A,A 4 ore': ,6P8 &8$ ,O C:+ 9A,A 4 ore': ,6P8 &8$ ,O C:+
ore';
ore'; ,6P8 ,6P8 &8$ &8$ ,O ,O C;*C;*
C&8A,8 OBJ8C, ore';* C&8A,8 OBJ8C, ore';* ore': < ore';* ore': < ore';* ore': -=a:* ore': -=a:* ore': -=a>* ore': -=a>* OL OL 5rror 5rror P%b: a1< a2 P%b: a1< a2 Prot: Prot: b1< b1< b2b2 Priv:c1<c2 Priv:c1<c2 C&ass C1 C&ass C1 P%b: a1< a2<a3<a P%b: a1< a2<a3<a Prot: Prot: b1<b2<b3<bb1<b2<b3<b Priv: c3<c Priv: c3<c
C&ass C2 Inheritin >ro9 C1
C&ass C2 Inheritin >ro9 C1
P%b: a1<
P%b: a1< a2<a3<a<a,<a/a2<a3<a<a,<a/
Prot:
Prot: b1< b1< b2<b3<b<b,<b/b2<b3<b<b,<b/
Priv:c,<c/
Priv:c,<c/
C&ass C3 Inheritin >ro9 C2
C&ass C3 Inheritin >ro9 C2 &e'erence variables declared )it# re'erence to&e'erence variables declared )it# re'erence to a Super Class 1includin( a abstract class 3 can a Super Class 1includin( a abstract class 3 can point to an object o' a Sub Class
point to an object o' a Sub Class but canbut can on&.on&. access the co9ponents o> the object that access the co9ponents o> the object that is HnoGn to the
Inheritance : Object +e>erences !Contd"#
Inheritance : Object +e>erences !Contd"#
9A,A 4 ore': ,6P8 &8$ ,O C:+ 9A,A 4 ore': ,6P8 &8$ ,O C:+
ore';
ore'; ,6P8 ,6P8 &8$ &8$ ,O ,O C;*C;* C&8A,
C&8A,8 OBJ8C, ore': ,6P8 8 OBJ8C, ore': ,6P8 C;*C;* C&8A,
C&8A,8 OB8 OBJ8C, ore';*J8C, ore';*
ore': < ore';* ore': < ore';* ore': -=a:* ore': -=a:* ore': -=a>* ore': -=a>* OL OL OL OL P%b: a1< a2 P%b: a1< a2 Prot: Prot: b1< b1< b2b2 Priv:c1<c2 Priv:c1<c2 C&ass C1 C&ass C1 P%b: a1< a2<a3<a P%b: a1< a2<a3<a Prot: Prot: b1<b2<b3<bb1<b2<b3<b Priv: c3<c Priv: c3<c
C&ass C2 Inheritin >ro9 C1
C&ass C2 Inheritin >ro9 C1
P%b: a1<
P%b: a1< a2<a3<a<a,<a/a2<a3<a<a,<a/
Prot:
Prot: b1< b1< b2<b3<b<b,<b/b2<b3<b<b,<b/
Priv:c,<c/
Priv:c,<c/
C&ass C3 Inheritin >ro9 C2
C&ass C3 Inheritin >ro9 C2
4.na9ic T.pe 4.na9ic T.pe $tatic T.pe $tatic T.pe
$tatic d.na9ic T.pe >or +e>erence
$tatic d.na9ic T.pe >or +e>erence
,#e Static tpe o' ,#e Static tpe o' a re'erence variable4a re'erence variable4
Is deter%ined usin( ,6P8 &8$ ,OIs deter%ined usin( ,6P8 &8$ ,O
&e%ains constant t#rou(# t#e pro(ra% &e%ains constant t#rou(# t#e pro(ra% runrun
,#e 9na%ic tpe o' a re'erence variable4,#e 9na%ic tpe o' a re'erence variable4
Is deter%ined b assi(n%entIs deter%ined b assi(n%ent
Can c#an(e durin( pro(ra% runCan c#an(e durin( pro(ra% run
4ATA : ore>1 TP5 +56 TO C1< 4ATA : ore>1 TP5 +56 TO C1< ore>2 ore>2 TP5 TP5 +56 +56 TO TO C2<C2< ore>3 ore>3 TP5 TP5 +56 +56 TO TO C3"C3"
C+5AT5 OB=5CT ore>1 TP5 C2"
C+5AT5 OB=5CT ore>1 TP5 C2"
C+5A
C+5AT5 OB=5CT orT5 OB=5CT ore>3"e>3"
ore>1 E ore>3"
ore>1 E ore>3"
$tatic T.pe >or ore>1 is C1 $tatic T.pe >or ore>1 is C1
4.na9ic t.pe >or ore>1 is noG C2 4.na9ic t.pe >or ore>1 is noG C2
4.na9ic t.pe >or ore>1 is noG C3 4.na9ic t.pe >or ore>1 is noG C3
In assi(n%ents o' re'erenceIn assi(n%ents o' re'erence variables +
variables + static t.pestatic t.pe o' a o' a re'erence variable is al)as re'erence variable is al)as 9ore enera& than the
9ore enera& than the d.na9ic t.pe
Inheritance : 7arroGin Cast
Inheritance : 7arroGin Cast
4ATA : ore>1 TP5 +56 TO $%per C&ass<
4ATA : ore>1 TP5 +56 TO $%per C&ass<
ore>2
ore>2 TP5 TP5 +56 +56 TO TO $%b $%b C&ass"C&ass"
ore>1 E ore>2"
ore>1 E ore>2"
#en static tpe o' t#e tar(et variable#en static tpe o' t#e tar(et variable is %ore (eneral t#an t#e static tpe o' is %ore (eneral t#an t#e static tpe o' t#e source variable+ since t
t#e source variable+ since t#e dna%ic#e dna%ic tpe o' t#e source variable at run ti%e tpe o' t#e source variable at run ti%e can onl be %ore speciali/ed t#an its can onl be %ore speciali/ed t#an its static tpe+ t#is can be c
static tpe+ t#is can be c#eced durin(#eced durin( t#e snta! c#ec* ,#is is no)n as t#e snta! c#ec* ,#is is no)n as narroGin cast or %p cast"
narroGin cast or %p cast"
A tp A tpical use o' narro)inical use o' narro)in( cast is to( cast is to prepare 'or
prepare 'or eneric accesseneric access* A user* A user )#o is not at all interested in t#e 'iner )#o is not at all interested in t#e 'iner points o' t#e instances o' t#e
points o' t#e instances o' t#e SubSub Classes+ but )ants to access t#e Classes+ but )ants to access t#e (eneric co%ponents 'or all o' t#e%+ (eneric co%ponents 'or all o' t#e%+ could use t#e Super Class re'erence to could use t#e Super Class re'erence to access all o' t#e% in t#e sa%e )a* access all o' t#e% in t#e sa%e )a*
7arroGin Cast 7arroGin Cast
Inheritance : Nidenin Cast
Inheritance : Nidenin Cast
4ATA : ore>1 TP5 +56 TO $%per C&ass<
4ATA : ore>1 TP5 +56 TO $%per C&ass<
ore>2
ore>2 TP5 TP5 +56 +56 TO TO $%b $%b C&ass<C&ass<
ore>3
ore>3 TP5 TP5 +56 +56 TO TO $%b $%b C&ass"C&ass"
ore>2 E ore>1" ore>2 E ore>1" ore>1 E ore>2" ore>1 E ore>2" T+" T+" ore>3 JE ore>1" ore>3 JE ore>1" CA
CATC C$MOTC C$MO5CA$T5++O+"5CA$T5++O+"
+eact on the Cast 5rror
+eact on the Cast 5rror
574T+"
574T+"
#en static tpe o' t#e tar(et variable#en static tpe o' t#e tar(et variable is %ore speciali/ed t#an t#e static tpe is %ore speciali/ed t#an t#e static tpe o' t#e source
o' t#e source variable* ,#is is no)nvariable* ,#is is no)n as
as Gidenin cast or doGn cast"Gidenin cast or doGn cast"
,#e )idenin( cast in t#e later case,#e )idenin( cast in t#e later case does not cause an error because t#e does not cause an error because t#e re'erence ore': actuall points to an re'erence ore': actuall points to an instance in t#e Sub Class* 1ore': < instance in t#e Sub Class* 1ore': < ore';3
ore';3
A tp A tpical use 'or )idenin( ical use 'or )idenin( cast is )#encast is )#en speci>ic
speci>ic co%ponents o' instancesco%ponents o' instances need to be addressed and t#eir need to be addressed and t#eir
re'erences are ept in variables t#at re'erences are ept in variables t#at are tped on t#e Super Class*
are tped on t#e Super Class*
$.nta $.nta 5rror Occ%rs 5rror Occ%rs Nidenin Cast Nidenin Cast
Inheritance : Po&.9orphis9
Inheritance : Po&.9orphis9
CA$$ s%perc&ass 456I7ITIO7"
CA$$ s%perc&ass 456I7ITIO7"
" " " " " " " " " " " " " " " " " " " " M5TO4$ test9ethod" M5TO4$ test9ethod" 574CA$$" 574CA$$"
CA$$ s%bc&assone 456I7ITIO7 I75+ITI7'
CA$$ s%bc&assone 456I7ITIO7 I75+ITI7'
6+OM s%perc&ass"
6+OM s%perc&ass"
" " " " " " " " "
" " " " " " " " "
M5TO4$ test9ethod +5456I7TIO7"
M5TO4$ test9ethod +5456I7TIO7"
574CA$$"
574CA$$"
CA$$ s%bc&asstGo 456I7ITIO7 I75+ITI7'
CA$$ s%bc&asstGo 456I7ITIO7 I75+ITI7'
6+OM s%perc&ass"
6+OM s%perc&ass"
" " " " " " " " "
" " " " " " " " "
M5TO4$ test9ethod +5456I7TIO7"
M5TO4$ test9ethod +5456I7TIO7"
574CA$$"
574CA$$"
4ATA: s%probj t.pe re> to s%perc&ass<
4ATA: s%probj t.pe re> to s%perc&ass<
s%b1obj t.pe re> to s%bc&assone<
s%b1obj t.pe re> to s%bc&assone<
s%b2obj t.pe re> to s%bc&asstGo"
s%b2obj t.pe re> to s%bc&asstGo"
$TA+T-O6-$55CTIO7"
$TA+T-O6-$55CTIO7"
C+5AT
C+5AT5 OB=5CT 5 OB=5CT s%b1obj"s%b1obj"
C+5AT
C+5AT5 OB=5CT 5 OB=5CT s%b2obj"s%b2obj"
s%probj E s%b1obj"
s%probj E s%b1obj"
CA
CA M5TO4 M5TO4 s%probj-Ftest9ethod"s%probj-Ftest9ethod"
s%probj E s%b2obj"
s%probj E s%b2obj"
&e'erence variables declared )it# static&e'erence variables declared )it# static re'erence to a Super Class can
re'erence to a Super Class can dna%icall point to an object o' a
dna%icall point to an object o' a SubSub Class o' t#is Super Class and access Class o' t#is Super Class and access t#et#e co%ponents no)n to t#e Super Class* co%ponents no)n to t#e Super Class* Met#ods in#erited 'ro% Super Class %a Met#ods in#erited 'ro% Super Class %a be rede'ined in one or %ore o'
be rede'ined in one or %ore o' t#e Subt#e Sub Classes* ,#is is t#e basis o'
Classes* ,#is is t#e basis o' pol%orp#i
pol%orp#is% s% usin( in#eritance*usin( in#eritance*
Pol%orp#is% #ere %eans usin( t#ePol%orp#is% #ere %eans usin( t#e sa%e na%e via sa%e inter'ace to sa%e na%e via sa%e inter'ace to uni'or%l address
uni'or%l address di''erentldi''erentl
i%ple%ented %et#ods+ belon(in( to i%ple%ented %et#ods+ belon(in( to di''erent objects o' di''erent classes in di''erent objects o' di''erent classes in t#e in#eritance tree*
4e9onstration
4e9onstration
5ercise ,: 5ercise ,:
S#o)in( #o) In#eritance )ors )it# ABAP object*S#o)in( #o) In#eritance )ors )it# ABAP object*
Objectives:Objectives:
9e'ine Sub Classes9e'ine Sub Classes
4e9onstration
4e9onstration
5ercise /: 5ercise /: