03 ABAP Objects -Inheritance

Share Embed Donate


Short Description

03 ABAP Objects -Inheritance...

Description

IBM Global Business Services

ABAP Objects Advanced -Inheritance

 © IBM Corporation 2013

IBM Global Business Services

ABAP Objects Advanced : Inheritance Topics 

Concept



Snta! and "isibilit



 Abstract Classes and Met#ods



$inal Classes and Met#ods



Static Co%ponents in In#eritance



Met#od &ede'inition



Constructor in In#eritance



Object &e'erences in In#eritance



Pol%orp#is% t#rou(# In#eritance

IBM Global Business Services

Inheritance : Concept 

In#eritance is one o' t#e t #e %ost po)er'ul 'eature o' object oriented pro(ra%%in(*



In#eritance is t#e process o' creatin( ne) classes+ called derived classes or Sub Classes or c#ild classes 'ro% e!istin( classes called c alled base classes or Super Classes or parent classes*



,#e derived class in#erits all t#e capabilities o' t#e base class but can add e%bellis#%ents and re'ine%ents o' its o)n* ,#e base class is unc#an(ed b t#is t #is process* Grand Parent Class Parent Class C#ild Class

Sin(le In#eritance supported b ABAP Objects

Parent Class

Parent Class

C#ild Class Multiple In#eritance not supported b ABAP Objects

IBM Global Business Services

Inheritance : Concept !Contd"# &oot node

    n     o       i       t     a     (       i       &     a     r     e     n     e       '

$%per C&ass



,#e relations#ip bet)een t#e base classes and t#e derived classes can be represented in an In#eritance tree* #ere base classes o' eac# derived class can be retraced alon( an uni.ue pat# to a sin(le root node o' t#e tree*



 A Super Super Class is a Generali/ation o' its Sub Classes and on t#e ot#er #and a Sub Class is a Speciali/ation o' its Super Class*

C&ass C1 Generali/ed Class

P%b&ic:  

Protected:  Private:

C&ass C2

    n     o       i       t     a     (       i       &     a       i     c     e     p       $

Speciali/ed Class

P%b&ic:  

Protected:  Private:

)I$ A* +e&ationship

C&ass C3 P%b&ic:  

Protected:  Private:

More speciali/ed class

IBM Global Business Services

Inheritance: Adva Advantaes ntaes 

In#eritance per%its code +e%sabi&it.* &eusin( e!istin( code re%oves code redundanc and saves ti%e  %one*



Co%%on co%ponents e!ist onl in t#e Super Class and %aintained Centra&&.*



Once a base class is )ritten and tested+ it need not be touc#ed a(ain* ,#is increases pro(ra% +e&iabi&it.*

IBM Global Business Services

Inheritance : $.nta and isibi&it. CA$$ s%perc&ass 456I7ITIO7" P8BIC $5CTIO7"



,#e public and protected co%ponents o' t#e Super Class are visible in t#e Sub Class*



Private section o' t#e Super Class is not visible in t#e Sub Classes*



Private co%ponents o' t#e Sub Class can #ave sa%e na%e as t#e private co%ponent o' t#e Super Class*



Public and Protected co%ponents o' t#e Sub Class can not #ave t#e sa%e na%e as t#at #ave been used in Super Class*

P%b&ic co9ponents P+OT5CT54 $5CTIO7" Protected co9ponents P+IAT5 $5CTIO7" Private co9ponents 574CA$$" CA$$ s%bc&ass 456I7ITIO7 I75+ITI7' 6+OM s%perc&ass" P8BIC $5CTIO7" P%b&ic co9ponents P%b&ic co9ponents !s%perc&ass# P+OT5CT54 $5CTIO7" Protected co9ponents Protected co9ponents !s%perc&ass# P+IAT5 $5CTIO7" Private section 574CA$$"

IBM Global Business Services

Inheritance : Abstract c&asses and 9ethods Abstract 9ethod

 A class de'ined de'ined as AB$T+ACT cannot be instantiated + t#at is one cannot use C+5AT5 OB=5CT )it# re'erence to t#e class*



CA$$ c&s%per 456I7ITIO7" P8BIC $5CTIO7"



 Abstract class can onl be accessed usin( its Static Co%ponents or its Sub Classes*



 Abstract class serves as a te%plate 'or Sub Classes



I' a class contains an abstract %et#od t#e )#ole class beco%es abstract*

M5TO4$: de9o1 AB$T+A AB$T+ACT< CT<  

de9o2"

574CA$$" +ede>inin the Abstract 9ethod o> $%per C&ass in the $%b C&ass CA$$ c&s%b 456I7ITIO7 I75+ITI7' 6+OM c&s%per" P8BIC $5CTIO7" M5TO4$ de9o1 +5456I7ITIO7" 574CA$$"

 A %et#od+ %et#od+ )#ic# is abstract+ s#ould be rede'ined in derived class*



 An Abstract Abstract class can declare and i%ple%ent non abstract %et#ods )#ic# its Sub Classes can use )it# or )it#out rede'inin( t#e%*



IBM Global Business Services

Inheritance : 6ina& c&asses and 9ethods CA$$ >ina&c&ass 456I7ITIO7 456I7ITIO7 6I7A"



 A >ina& class can not be inherited 'urt#er*



 All Met#ods o' a >ina& class are in#erentl 'inal and %ust not be declared as >ina& in t#e class de'inition*



 A >ina& %et#od can not be rede>ined 'urt#er*

"""""""" 574CA$$" CA$$ derivedc&ass 456I7ITIO7 I75+ITI7' 6+OM >ina&c&ass " " "" " " " " " " " 574CA$$" CA$$ s%perc&ass 456I7ITIO7 " """""""" M5TO4$ >ina&9ethod 6I7A" 574CA$$" CA$$ s%bc&ass 456I7ITIO7 I75+ITI7' 6+OM s%perc&ass " " "" " " " " " " " M5TO4$ >ina&9ethod rede>inition" 574CA$$"



I' a %et#od o' a non-'inal class is 'inal t#en t#at class can be in#erited but t#at %et#od cannot be rede'ined*

IBM Global Business Services

Inheritance : $tatic co9ponents CA$$ basec&ass 456I7ITIO7" P8BIC $5CTIO7"



,#e p%b&ic and protected static co%ponents 1%et#ods  attributes3 o' t#e Super Class are visible in t#e Sub Class*



Public static co%ponents can be accessed t#rou(# t#e class co%ponent selector used )it# an class in t#e respective pat# o' in#eritance tree*

$tatic co9ponent

CA$$-4A CA$$-4 ATA : na9e TP5 $T+I7' A85 A 85 ABAP"   DDD"" 574CA$$" CA$$ derivedc&ass 456I7ITIO7 I75+ITI7' 6+OM basec&ass" " "" " " " " " " " 574CA$$" $TA+T-O6-$55CTIO7" basec&assEFna9e" derivedc&assEFna9e"

$tatic co9ponent is accessed thro%h c&ass co9ponent se&ector >ro9 the derived c&ass as Ge&& as >ro9 the base c&ass

IBM Global Business Services

Inheritance : Method +ede>inition CA$$ basec&ass 456I7ITIO7"



P8BIC $5CTIO7" M5TO4$: 9eth" """""""" 574CA$$" CA$$ derivedc&ass 456I7ITIO7 I75+ITI7' 6+OM basec&ass " P8BIC $5CTIO7"

 

M5TO4$: 9eth +5456I7ITIO7" " "" " " " " " " " 574CA$$"



+ede>inition is )#en t#e i%ple%entation o' an in#erited instance %et#od is c#an(ed 'or t#e Sub Class )it#out c#an(in( its si(nature* $tatic 9ethods cant be rede>ined * ,#e para%eters re%ain sa%e in t#e derived class as it )as in base class* In t#e rede'ined %et#od use t#e t #e $8P5+ pseudo re'erence to access t#e ori(inal %et#od o' t#e base class*

CA$$ derivedc&ass IMP5M57TATIO7 " M5TO4$ 9eth" CA$$ M5TO4 $8P5+-F9eth" DDD  574M5TO4" 574CA$$"

+ede>inin the base c&ass 9ethod in the derived c&ass Ca&&in the $%per C&ass 9ethod %sin $8P5+ He.Gord

IBM Global Business Services

Inheritance : Instance Constr%ctors CA$$ basec&ass 456I7ITIO7" P8BIC $5CTIO7" M5TO4$: constr%ctor IMPO+TI7' ar1 TP5 $T+I7'"



 As all public and protected co%ponents+ Sub Class in#erits t#e constructor %et#od i' it is present in t#e in#eritance tree*



I' an object o' Sub Class is created at t#is ti%e t#e in#erited instance attributes and private attributes o' t#e Super Classes %ust also be initiali/ed*



 As t#e private attributes o' t#e Super Class is not visible to Sub Class+ Sub Class cannot 'ull initiali/e its Super Class* ,#ere'ore to ensure co%plete initiali/ation o' Sub Class and its Super Classes+ constructor is called*

P+IAT5 P+IA T5 $5CTIO $5CTIO7" 7" 4ATA: 4AT A: >&d TP5 $T+I7 $T+I7'" '" " "" "" "" "

$%per C&ass constr%ctor 

574CA$$"

CA$$ derivedc&ass 456I7ITIO7 I75+ITI7' 6+OM basec&ass " P8BIC $5CTIO7" M5TO4$: constr%ctor IMPO+TI7' ar1 TP5 $T+I7' ar2 TP5 $T+I7'" P+IAT5 P+IA T5 $5CTIO $5CTIO7" 7" 4ATA: 4AT A: >&d TP5 $T+I7 $T+I7'" '"

$%b C&ass constr%ctor 

" "" "" "" " 574CA$$" CA$$ derivedc&ass IMP5M57TA IMP5M57TATIO7 TIO7 " M5TO4$ constr%ctor" CA M5TO4 $8P5+-Fconstr%ctor  5PO+TI7' ar1 E ar1" >&d E ar2 " DDD  574M5TO4" 574CA$$"

Ca&&in $%per C&ass constr%ctor 

IBM Global Business Services

Inheritance : Instance Constr%ctors !Contd"# 

#et#er e!plicit e!plicit call is re.uired 'or t#e instance constructor o' t#e Super Class or not + )#en )e instantiate an object o' Sub Class is depends on t#e 'ollo)in( conditions4

5p&icit constr%ctor is de>ined in $%per C&assJ

5p&icit constr%ctor is de>ined in $%b C&assJ

5p&icit ca&& reK%iredJ

5o

5o

5o

6es

5o

5o

5o

6es

6es

6es

6es

6es

IBM Global Business Services

Inheritance : $tatic Constr%ctors CA$$ basec&ass 456I7ITI 456I7ITIO7" O7" P8BIC $5CTIO7" M5TO4$: c&asscons c&assconstr%ctor" tr%ctor" P+IAT5 P+IAT 5 $5C $5CTIO7" TIO7" 4ATA: 4AT A: >&d TP5 $T+I7 $T+I7'" '" """""""" $%per C&ass constr%ctor  574CA$$" CA$$ derivedc&ass 456I7ITIO7 I75+ITI7' I75+ITI 7' 6+OM basec&ass " P8BIC $5CTIO7" M5TO4$: c&assconstr%c c&assconstr%ctor tor " P+IAT5 P+IAT 5 $5CTI $5CTIO7" O7" 4ATA: 4AT A: >&d TP5 $T+I7 $T+I7'" '" """""""" $%b C&ass constr%ctor  574CA$$" CA$$ derivedc&ass derivedc &ass IMP5M57TA IMP 5M57TATIO7 TIO7 " M5TO4$ c&assconstr%ctor" >&d E  I a9 s%b " 7o ca&& >or $%per C&ass DDD constr%ctor   574M5TO4" 574CA$$"



,#e rede'inition o' static constructor )ors si%ilarl to instance constructor*



#en an static constructor is rede'ined t#en &898$I5I,IO5 addition is not re.uired*



5o para%eter inter'ace is possible 'or ' or static constructor 1 )it# or )it#out in#eritance3



,#e runti%e environ%ent auto%aticall ensures t#at t#e static constructors are called in t#e ri(#t order+ so it is not re.uired to call t#e static constructor o' t#e Super Class e!plicitl*

IBM Global Business Services

Inheritance : Object +e>erences C&ass C1 P%b: a1< a2 Prot: b1< b2  Priv:c11 TP5 +56 TO C1" C+5AT5 C+5AT5 OB=5CT OB=5C T ore>1"

C&ass C2 Inheritin >ro9 C1 P%b: a1< a2ic co%ponents o' instances need to be addressed and t#eir re'erences are ept in variables t#at are tped on t#e Super Class*

IBM Global Business Services

Inheritance : Po&.9orphis9 CA$$ s%perc&ass 456I7ITIO7" """""""""" M5TO4$ test9ethod" 574CA$$" CA$$ s%bc&assone 456I7ITIO7 I75+ITI7' 6+OM s%perc&ass" """"""""" M5TO4$ test9ethod +5456I7TIO7" 574CA$$" CA$$ s%bc&asstGo 456I7ITIO7 I75+ITI7' 6+OM s%perc&ass" """"""""" M5TO4$ test9ethod +5456I7TIO7" 574CA$$" 4ATA: s%probj t.pe re> to s%perc&ass<  s%b1obj t.pe re> to s%bc&assone<  s%b2obj t.pe re> to s%bc&asstGo" $TA+T-O6-$55CTIO7" C+5AT5 C+5AT 5 OB=5CT s%b1obj" C+5AT5 C+5AT 5 OB=5CT s%b2obj" s%probj E s%b1obj" CA M5TO4 s%probj-Ftest9ethod" s%probj E s%b2obj" CA M5TO4 s%probj-Ftest9ethod"



&e'erence variables declared )it# static re'erence to a Super Class can dna%icall point to an object o' a Sub Class o' t#is Super Class and access t#e co%ponents no)n to t#e Super Class* Met#ods in#erited 'ro% Super Class %a be rede'ined in one or %ore o' t#e Sub Classes* ,#is is t#e basis o' pol%orp#is% pol%orp#is% usin( in#eritance*



Pol%orp#is% #ere %eans usin( t#e sa%e na%e via sa%e inter'ace to uni'or%l address di''erentl i%ple%ented %et#ods+ belon(in( to di''erent objects o' di''erent classes in t#e in#eritance tree*

IBM Global Business Services

4e9onstration 5ercise ,: S#o)in( #o) In#eritance )ors )it# ABAP object*



Objectives:



9e'ine Sub Classes



&ede'ine Super Class %et#ods in Sub Classes



IBM Global Business Services

4e9onstration 5ercise /: In t#is e!ercise ou )ill de%onstrate Po&.9orphis9 t#rou(# Inheritance*



View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF