131
22/12/2014 ABAP OO Concept Topicwise Example | maagnusabap http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 1/131 maagnusabap ABAP OO Concept Topicwise Example INDEX 1 Class. 5 1.1 Accessibility of different sections of a class. 5 1.2 Subclass cannot access the private component of superclass. 8

ABAP OO Concept Topicwis Example _ Maagnusabap

Embed Size (px)

DESCRIPTION

hi

Citation preview

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 1/131

    maagnusabap

    ABAPOOConceptTopicwiseExample

    INDEX

    1Class.5

    1.1Accessibilityofdifferentsectionsofaclass.5

    1.2Subclasscannotaccesstheprivatecomponentofsuperclass.8

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 2/131

    1.3Externaluserscannotaccessprotected/privatecomponentsofaclass.9

    1.4LocalClasscanunderstanddataandtypesintheglobalareaoftheprogram.10

    1.5Classcanbeinstantiatedwithinimplementationofanotherclass.12

    1.6DeferredDefinitionofaClass.13

    1.7Placetoputnondeclarativestatements.14

    1.8UseofFieldSymbolsinClass.15

    1.9UseofStaticAttributes.16

    1.10CreationofGlobalclassandusingitinalocalprogram..17

    2Methods.22

    2.1Methodwithoneimportparameter/onlyonenonoptionalparameter.22

    2.2Importparameterspassedbyref.cantbechangedinsidethemethod.23

    2.3UseofPREFERREDPARAMETERinamethod.24

    2.4UseofEXPORTandCHANGINGparametersofamethod.25

    2.5MethodusingInternalTableasoneoftheparameters.26

    2.6UseofRETURNINGparametersinmethod.27

    2.7DemoonStaticMethod.28

    2.8Staticmethodscanonlyusestaticattributes,instancemethodsuseboth.29

    2.9MethodRaisingExceptions.30

    2.10Methodcancallitself.31

    2.11UseofMEinmethods.32

    2.12PointerTables.33

    2.13DynamicMethodCalls.34

    2.14Useofparametertable.35

    2.15UseofExceptionTable.36

    3Constructors.37

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 3/131

    3.1InstanceConstructorsgetfiredatthetimeofclassinstantiation.37

    3.2InstanceConstructorscanhaveimportparameters.38

    3.3Constructorscannothaveanyexportparameters.39

    3.4InstanceConstructorscanraiseexceptions.40

    3.5Useofstaticconstructor.41

    3.6Staticconstructorcanbetriggeredatthebeginningofaprocessingblock(form/event/block/procedure)42

    3.7Static/Classconstructorscannothaveanyinterface.43

    4Inheritance.44

    4.1Subclasscanaccesspublic/protectedcomponentsofsuperclass.44

    4.2Subclasscanreimplementinheritedmethodsfromsuperclass.46

    4.3Objectscannotbecreatedfromanabstractclass.47

    4.4Abstractmethodscannotbeimplementedinabstractclass.48

    4.5Finalclassescannothaveanysubclass.49

    4.6Finalmethodscannotberedefinedinthesubclasses.50

    4.7Staticattributesexistonlyonceperinheritancetree.51

    4.8Constructorsofsuperclassflowsdownthechain.52

    4.9Subclasscanhaveenhancedconstructorthanitssuperclass.53

    4.10Staticconstructorofaclassiscalledonlyonceperprogram.55

    4.11StatictypeandDynamictypeofavariable.56

    4.12Statictypeshouldbemoregeneralthandynamictypeofareferencevariable.58

    4.13Methodofaparentclass,usedfromitssubclass,usesattributesoftheparentclassonly,ifthemethodisnotredefinedinsubclass.59

    4.14DemoonWideningCast.60

    5Interface.61

    5.1Simpleuseofaninterface.61

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 4/131

    5.2Interfacescanonlybeimplementedinthepublicsectionofaclass.62

    5.3Aclasswithaninterfaceshouldimplementallthemethodsofthatinterface.63

    5.4Valuesforinterfaceattributesareassignedatthetimeofinclusioninaclass.64

    5.5UseofFINALmethodsfromInterface.65

    5.6UseofAbstractmethodsfromInterface.66

    5.7UseofInterfaceReferenceVariable.67

    5.8UseofNestedInterface.69

    5.9UsingALIASES..70

    5.10PolymorphismviaInterfaces.71

    6Friendship.72

    6.1FriendshipbetweenClasses.72

    6.2Subclassesoffriendscanalsobecomefriends.73

    6.3Friendshipisonesided.74

    7Events.75

    7.1EventswithHandlerMethodinthesameclass.75

    7.2Eventwitheventhandlermethodindifferentclass.76

    7.3Morethanoneeventhandlermethodcanexistforsameevent.77

    7.4Useofstaticevent.79

    7.5Eventswithexportparameters.80

    8ClassBasedExceptions.81

    8.1UsingSAPprovidedexceptionclass.81

    8.2Whenbothsuperclassandsubclassareusedtotrackerror.82

    8.3PropagationofClassBasedexceptionsinprocedurestothecaller.83

    8.4ProgramcanraiseexceptionsbasedonSAPstandardexceptionclasses.84

    8.5Objectsarecreatedfromexceptionclasseswhenerroristrapped.85

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 5/131

    8.6DemoonLocallyDefinedExceptionClass.86

    8.7NestedTRYENDTRYblock.87

    8.8UseofCLEANUPsection.88

    9BADIs(BusinessAddIns)89

    9.1SingleImplementationofBADI.89

    9.2MultipleImplementation.93

    9.3SearchingforBADIinSAPTransactionandImplementingit.96

    9.4MenuEnhancements.98

    1Class

    1.1Accessibilityofdifferentsectionsofaclass

    Theme Fromthisprogram,onewilllearn:

    1. Howtodefine,implementandinstantiateaclass.2. Whatarethedifferentsectionsofvisibilityinaclass.3. Howtodefineinstanceattributesandgetthemaccessedby

    externalusers.

    Thefollowingprogramwillalsoshowthat:

    vDatadeclaredinpublicsectioncanbeaccessedbytheclassitself,byitssubclassesaswellasbyotherusersoutsidetheclass.

    vDatadeclaredintheprotectedsectioncanbeaccessedbytheclassitself,andalsobyitssubclassesbutnotbyexternalusersoutsidetheclass.

    vDatadeclaredintheprivatesectioncanbeaccessedbythe

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 6/131

    vDatadeclaredintheprivatesectioncanbeaccessedbytheclassonly,butnotbyitssubclassesandbyexternalusersoutsidetheclass.

    BriefDescription Thisprogramcontainsaclass:parentclasswithfollowingattributesindifferentsections:

    Commondatainpublicsection

    Protectdatainprivatesection

    Privatedatainprivatesection

    Themethodshowvalinclass:parentclassdisplaysvaluesofalltheattributes.

    Thisdemonstratesthatclasscanaccessallitsattributes.

    Classchildclassisasubclassofclassparentclass,whichhasamethod:subval.

    Itdisplaysthevalueforthedata:commondataandprotectdata.

    Then,itchangesthevaluesforbothanddisplaysthemagain.

    Thisdemonstratesthatsubclasscanaccess/changepublic/protectedattributesofsuperclass.

    IntheSTARTOFSELECTIONevent,object:parentisinstantiatedfromclass:parentclassandobject:childisinstantiatedfromclass:childclass.

    Then,themethodshowvalofparent(objectofparentclass)andmethodsubvalofchild(objectofchildclass)iscalled,whichdisplaysthevaluesofdifferentattributes.

    Then,thepublicattributeofobjectparentischangedandthechangedvalueisdisplayed.

    Thisdemonstratesthatexternaluserscanchange/displaypublicattributesofaclass.

    Dumpoftheprogram:

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 7/131

    REPORTYSUBDELLINESIZE120.

    CLASSparentclassDEFINITION.

    PUBLICSECTION.

    DATA:commondata(30)typecvalueAccessibletoall.

    METHODS:SHOWVAL.

    PROTECTEDSECTION.

    DATA:protectdata(40)typecvalueProtecteddata.

    privatesection.

    data:privatedata(30)typecvaluePrivatedata.

    ENDCLASS.

    CLASSparentclassIMPLEMENTATION.

    METHOD:SHOWVAL.

    write:/5Alldatafromparentclassshown:.

    write:/syuline.

    WRITE:/5COMMONDATA,

    /5PROTECTDATA,

    /5PRIVATEDATA.

    endmethod.

    endclass.

    CLASSchildclassDEFINITIONINHERITINGFROMparentclass.

    PUBLICSECTION.

    METHODS:subval.

    ENDCLASS.

    CLASSchildclassIMPLEMENTATION.

    method:subval.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 8/131

    skip1.

    write:/5Dataofparentshownfromchild.

    write:/5syuline.

    WRITE:/5COMMONDATA,

    /5PROTECTDATA.

    Commondata=Publicdatachangedinsubclass.

    Protectdata=Protecteddatachangedinsubclass.

    write:/5syuline.

    WRITE:/5COMMONDATA,

    /5PROTECTDATA.

    endmethod.

    endclass.

    STARTOFSELECTION.

    DATA:parenttypereftoparentclass,

    childtypereftochildclass.

    createobject:parent,

    child.

    callmethod:parent>showval,

    child>subval.

    skip2.

    parent>commondata=Userchangingpublicdata.

    write:/5parent>commondata.

    Output Alldatafromparentclassshown:

    Accessibletoall

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 9/131

    Protecteddata

    Privatedata

    Dataofparentshownfromchild

    Accessibletoall

    Protecteddata

    Publicdatachangedinsubclas

    Protecteddatachangedinsubclass

    Userchangingpublicdata

    1.2Subclasscannotaccesstheprivatecomponentofsuperclass

    Theme Theprogramdemonstratesthatsubclassescannotaccesstheprivatecomponentsofsuperclass.

    Programdescription

    Theprogramusedhereissimilartoabovewithchangeinthemethod:subvalofclass:childclass.Thismethodisnowattemptingtoaccesstheattribute:privatedata,whichisaprivateattributeofitssuperclass:parentclass.

    Oncompilation,theprogramwillgiveacompilationerror.

    Thisdemonstratesthatprivatecomponentsofsuperclasscannotbeaccessedbysubclasses.

    ProgramDump

    Takethefirstprogram.Onlychangethemethod:subvalofclass:childclassasfollows:

    method:subval.

    skip1.

    write:/5Alldatafromparentclassshownbysubclass.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 10/131

    write:/5syuline.

    WRITE:/5COMMONDATA,

    /5PROTECTDATA,

    /5privatedata.

    endmethod.

    Output Theprogramwillnotcompile.Itwillshowanerrormessage:

    1.3Externaluserscannotaccessprotected/privatecomponentsofaclass

    Theme Thisprogramwilldemonstratethatexternaluserscannotaccesstheprotectedandprivatecomponentsofaclass

    ProgramDescription

    Inthisprogram,classC1hasthreeattributesdeclaredindifferentsectionsasfollows:

    vCommondatainpublicsection

    vProtectdatainprivatesection

    vPrivatedatainprivatesection

    Inthemainprogram,anobject,OBJ1iscreatedfromclassC1andanincorrectattemptismadetodisplaytheprotectedandprivateattributeofclassC1usingitsobjectOBJ1.

    Compilationofthisprogramproducesanerror.

    Thisdemonstrates:protectedandprivatecomponentsofaclasscannotbeaccessedbyexternalusers.

    Dump REPORTYSUBDELLINESIZE120.

    CLASSc1DEFINITION.

    PUBLICSECTION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 11/131

    DATA:commondata(30)typecvalueAccessibletoall.

    PROTECTEDSECTION.

    DATA:protectdata(40)typecvalueProtecteddata.

    privatesection.

    data:privatedata(30)typecvaluePrivatedata.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    endclass.

    STARTOFSELECTION.

    DATA:obj1typereftoc1.

    createobject:obj1.

    write:/5obj1>protectdata,

    obj1>privatedata.

    Output Oncompilation,anerrorwillbegeneratedwhichwillprovethatprotectedandprivatecomponentsofaclasscannotbeaccessedbyexternalusers.

    1.4LocalClasscanunderstanddataandtypesintheglobalareaoftheprogram.

    Theme Thisprogramwilldemonstratethefollowing:

    vDifferentattributesofaclasscanbeconstructedutilizingthedataandtypesdeclaredoutsidetheclass,intheglobalareaoftheprogram.

    vDatadeclaredintheglobalareaoftheprogramcanbeuseddirectlyinaclass.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 12/131

    Programdescription

    Theglobalsectionofthisprogramcontainsatype:TYP_TABandanintegervariable,NUM1.

    ThesetypeanddataareusedwhiledefiningattributesL_NUM1(integer)andIT_TAB(internaltable)forclassC1.Also,theglobaldataL_NUMisuseddirectlyinsidetheprogram.

    Thisdemonstratesthetheme.

    Dump REPORTYSUBDEL1LINESIZE120.

    TYPES:BEGINOFTYP_TAB,

    NAME(15)TYPEC,

    AGETYPEI,

    ENDOFTYP_TAB.

    DATA:num1typeivalue5.

    CLASSc1DEFINITION.

    publicsection.

    methods:meth1.

    DATA:l_numlikenum1,

    it_tabtypestandardtableoftyp_tab,

    w_tablikelineofit_tab.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    method:meth1.

    data:l_cnum(2)typec.

    l_num=0.

    do5times.

    l_num=l_num+1.

    l_cnum=l_num.

    concatenateStudent

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 13/131

    l_cnum

    intow_tabname.

    w_tabage=num1*l_num.

    appendw_tabtoit_tab.

    clearw_tab.

    enddo.

    loopatit_tabintow_tab.

    write:/5w_tabname,

    w_tabage.

    endloop.

    endmethod.

    endclass.

    STARTOFSELECTION.

    DATA:obj1typereftoc1.

    createobject:obj1.

    callmethodobj1>meth1.

    Output Student15

    Student210

    Student315

    Student420

    Student525

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 14/131

    1.5Classcanbeinstantiatedwithinimplementationofanotherclass

    Theme Thisprogramwilldemonstratethatanobjectcanbecreatedfromaclass((whichwascreatedwithnoCREATEPRIVATE|PROTECTEDoptionatthetimeofitsdefinition)intheimplementationsectionofanotherclass.

    ProgramDescription

    ThisprogramcontainstwoclassesCLASS1andCLASS2.

    ClassCLASS1containsmethod:METHOD1whichdisplaysvalueofsomeintegervariable.

    ClassCLASS2containsmethod:METHOD2.Inthemethodimplementation,anobjectiscreatedfromclass:CLASS1andthenthatobjectisusedtocallmethodMETHOD1.

    Thisdemonstratesthatobjectcanbecreatedfromaclass(CLASS1)withinimplementationsectionofanotherclass(CLASS2).

    REPORTYSUBOOPS17.

    classclass1definition.

    publicsection.

    methods:method1.

    endclass.

    classclass2definition.

    publicsection.

    methods:method2.

    endclass.

    classclass1implementation.

    method:method1.

    data:i_numtypeivalue2.

    write:/5i_num.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 15/131

    endmethod.

    endclass.

    classclass2implementation.

    method:method2.

    data:obj1typereftoclass1.

    createobjectobj1.

    callmethodobj1>method1.

    endmethod.

    endclass.

    startofselection.

    data:my_objtypereftoclass2.

    createobject:my_obj.

    callmethodmy_obj>method2.

    Output 2

    1.6DeferredDefinitionofaClass

    Theme Thisprogramwilldemonstratehowonecanrefertoaclasswithoutdefiningtheclassbeforethatpoint.But,theclasshastobedefinedlateron.

    Programdescription

    Inthisprogram,classC1hasaninterfacereferenceO2declaredwithreferencetoclassC2.But,beforethat,classC2isnotdefined.Itisdefinedlaterwithasinglepublicattribute,NUM.

    Thisdemonstratesthetheme.

    Inthemainsectionoftheprogram,object:OBJ1iscreatedfromclassC1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 16/131

    Then,anobjectiscreatedfromthereferencevariableO2inclassC1.Finally,theattributenumofthatobjectisdisplayed.

    Dump reportysubdel1.

    CLASSC2DEFINITIONDEFERRED.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    DATAO2TYPEREFTOC2.

    ENDCLASS.

    CLASSC2DEFINITION.

    publicsection.

    data:numtypeivalue5.

    ENDCLASS.

    startofselection.

    data:obj1typereftoC1.

    CREATEOBJECTobj1.

    createobjectobj1>o2.

    write:/5obj1>o2>num.

    Output 5

    1.7Placetoputnondeclarativestatements

    Theme Foraclass,theIMPLEMENTATIONsectioncanimmediatelyfollowtheclassDEFINITIONsection.Ifthisisso,thenallthenondeclarativestatements(viz.,processingstatementsoutsideanyclassdefinition/implementation)shouldbeplacedunderaprocessingblock,suchas

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 17/131

    STARTOFSELECTION.

    Programdescription

    ThisprogramcontainsaclassC1withamethodM1.

    Inversion1,theIMPLEMENTATIONpartoftheclassfollowstheclassdefinitionsection.But,thenondeclarativestatementsarenotplacedunderanyblock.Thiscreatesacompilationerror.

    Inversion2,thenondeclarativestatementsarenotplacedundertheblockSTARTOFSELECTION.Itgetscorrectlycompiled.

    Thisdemonstratesthetheme.

    REPORTYSUBDEL.

    classc1definition.

    publicsection.

    methods:m1.

    endclass.

    classc1implementation.

    method:m1.

    write:/5Iammethodm1.

    endmethod.

    endclass.

    STARTOFSELECTION.

    data:obj1typereftoc1.

    createobjectobj1.

    callmethodobj1>m1.

    REPORTYSUBDEL.

    classc1definition.

    publicsection.

    methods:m1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 18/131

    endclass.

    classc1implementation.

    method:m1.

    write:/5Iammethodm1.

    endmethod.

    endclass.

    data:obj1typereftoc1.

    createobjectobj1.

    callmethodobj1>m1.

    Version1:IncorrectVersion2:Correct

    Output Version1createscompilationerror.

    Version2getscorrectlycompiled.

    1.8UseofFieldSymbolsinClass

    Theme FieldSymbolscanbeusedtocontainvalueofanyvariableinaclass.

    ProgramDescription

    Theprogramusesafieldsymbol,.Ithandlesthevaluesofinstanceattribute,inumandstaticattribute,onum.

    Dump REPORTYSUB_ASSIGN_FS.

    FIELDSYMBOLS:TYPEANY.

    classc1definition.

    publicsection.

    *Instanceattribute:inumdeclaredbelow

    data:inumtypeivalue5.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 19/131

    *staticattributeonumdeclaredbelow

    classdata:onumtypeivalue10.

    endclass.

    classc1implementation.

    endclass.

    startofselection.

    data:oref1typereftoc1.

    createobjectoref1.

    *Assigninginstanceattributetofieldsymbol

    assignoref1>inumto.

    write:/5.

    *Assigningstaticattributetofieldsymbol

    assignoref1>onumto.

    write:/5.

    assignc1=>onumto.

    write:/5.

    Output 5

    10

    10

    1.9UseofStaticAttributes

    Theme Thisprogramwilldemonstratethat:Staticsttributesofaclassareretainedthroughouttheentireruntime.Alltheobjectswithina

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 20/131

    classcanaccessitsstaticattributes.

    ProgramDescription

    TheprogramcontainsaclassC1withstaticattribute:NUM.Themethod:M1incrementsthestaticattributeby1anddisplaysthevalueeachtimeitiscalled.

    InthemainSTARTOFSELECTIONportion,twoobjects:OBJ1andOBJ2arecreatedfromclassC1.

    First,staticattribute:NUMischangedandaccessedoutsidetheclassusingtheclasscomponentselector,=>.

    Then,bothobjectsOBJ1andOBJ2areusedtocallmethod:M1whichshowsthenewvalueofstaticattribute:NUM.

    ThatthevalueofthestaticattributegetsincrementedeachtimewhenthemethodM1ofdifferentobjectsiscalledshowsthatthisvariableisabletoretainitsvaluethroughtheentireruntime.

    Dump reportysubdel.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    CLASSDATA:NUMTYPEI.

    METHODS:M1.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHODm1.

    num=num+1.

    write:/5num.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    c1=>num=3.

    write:/5c1=>num.

    DATA:OREF1TYPEREFTOC1,

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 21/131

    OREF2TYPEREFTOC1.

    CREATEOBJECT:OREF1,

    OREF2.

    CALLMETHODOREF1>M1.

    CALLMETHODOREF2>M1.

    Output 3

    4

    5

    1.10CreationofGlobalclassandusingitinalocalprogram

    Theme Thisexamplewillshowyouhowtocreateaclassgloballyanduseitinyourlocalprogram

    ProgramDescr.

    Thereisademandtocreateaglobalclasswhichwillfurnishalistofmaterialsalongwiththeirdescriptionsbasedonthematerialtype.

    GlobalclassZGET_MATERIALSwillbecreated.MethodLIST_MATERIALSwillbelongtothisclasswhichwilltakematerialtypeasaninputandwillfurnishalistofmaterialcodesandtheirdescriptionsbelongingtothatmaterialtype.

    Steps Followthestepsoutlinedbelowtoperformthetask

    Step1.CreatetheclassfromSE24:

    vGototransactionSE24.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 22/131

    vEnterthenameoftheglobalclassyouwanttocreate,withYorZatthebeginning.

    vPressCreatepushbutton.

    vAdialogwindowshownabovewillappear.Checktheradiobutton:Class.

    vPressEnter.

    vAnotherdialogwindowshownabovewillappear.Enterthedescriptionfortheclass.

    vSelectfromtheInstantiationlistboxwhetheryouwanttocreatetheclassasPUBLIC/PROTECTED/PRIVATE/ABSTRACT.

    vChecktheradiobuttonforUsualABAPClass.

    vCheckthecheckboxforFinal.

    vPressSavepushbutton.

    EnterthepackagenameorsaveitasLocalobject.

    Step2:Createthemethod:LIST_MATERIALS

    vGotothetabpage:Methods.

    vEnterthedetailsforthemethodmentionname,typeofmethod(instance/static),inwhichvisibilitysectionthemethodwillresideandashortdescriptionofthemethod.

    vCheckoff/uncheckthecheckboxtoensurethatthemethodwillbeimplemented.

    ClickthepushbuttonforParameterstonavigatetothescreentoenterparametersforthemethod.

    Therewillbeoneimportingparameter:L_MTARTandoneexportinginternaltable:MATERIAL_LIST.Createentriesforthemasshownabove.

    Clickthepushbutton:ExceptionstomakeentryforExceptionstoberaisedbythemethod.

    Enterthenameanddescriptionfortheexception.

    Then,checkandactivatetheclass.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 23/131

    Step3:Writecodeformethodimplementation

    Clickonthepushbutton:Code(bluecoloredbutton)toimplementthemethod.

    AnABAPEditorwillopenup.Writethelogicforcodeimplementation.

    Then,checkandactivatethecode.

    Yourjobofcreatingaglobalclassiscomplete!!!

    Step4:Usetheglobalclasscreatedbyyouinalocalprogram

    REPORTYSUBDEL.

    TYPES:BEGINOFtyp_mat,

    matnrLIKEmaramatnr,

    maktgLIKEmaktmaktg,

    ENDOFtyp_mat.

    DATA:it_matTYPESTANDARDTABLEOFtyp_mat,

    x_matLIKELINEOFit_mat.

    PARAMETERS:p_mtartLIKEmaramtartOBLIGATORY.

    STARTOFSELECTION.

    *Createobjectfromtheglobalclass

    DATA:orefTYPEREFTOzget_materials.

    CREATEOBJECToref.

    *Callthemethodtogetlistofmaterialcodeandname

    CALLMETHODoref>list_materials

    EXPORTINGl_mtart=p_mtart

    IMPORTINGmaterial_list=it_mat

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 24/131

    EXCEPTIONS

    material_not_found=2.

    ifsysubrcne0.

    write:/5Materialnotfound.

    else.

    *Displaythelist

    loopatit_matintox_mat.

    write:/5x_matmatnr,

    x_matmaktg.

    endloop.

    endif.

    Output Compileandruntheprogram.Therewillbeaparameterformaterialtypeintheselectionscreen.Enteravalidvalueandgetthelistofmaterialcodesanddescriptions.

    2Methods

    2.1Methodwithoneimportparameter/onlyonenonoptionalparameter

    Theme Thisprogramwilldemonstratedifferentwaysofcallingamethodwhichhasonlyoneimportparameter.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 25/131

    Thisstrategyisalsovalidforcaseswhereamethodhasmorethanoneimportparameters,butonlyoneofthembeingnonoptional.

    Program Thisprogramhasaclass,C1withamethod:meth1.Thismethodhasonlyoneimportparameter(input1).Lookatthemethodimplementationfordetails.Themainpurposeofthisprogramistodemonstratethedifferentwaysofcallingamethodwithsingleimportparameter.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    DATA:NUMTYPEIVALUE5.

    METHODS:METH1IMPORTINGINPUT1TYPEI.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    num=NUM*INPUT1.

    WRITE:/5NUM.

    num=5.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOC1.

    CREATEOBJECT:OREF1.

    *Differentwaysofcallingthemethodwithoneimportparameter

    CALLMETHODOREF1>METH1EXPORTINGINPUT1=4.

    CALLMETHODOREF1>METH1(INPUT1=5).

    CALLMETHODOREF1>METH1(6).

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 26/131

    Output 20

    25

    30

    2.2Importparameterspassedbyref.cantbechangedinsidethemethod.

    Theme Parameterscanbepassedtoamethodasimportparametersintwofashion:

    vByreference

    vByvalue.

    Parameterspassedbyvaluecanbechangedinternallyinamethod.But,parameterspassedbyreferencecannotbechangedinthemethod.

    Programdescription

    ThisprogramcontainsaclassC1withamethodMETH1.Thismethodcontainstwoinputparameters:

    INPUT1:passedbyreference

    INPUT2:passedbyvalue.

    ThemethodMETH1attemptstochangeINPUT1.Oncompilation,anerrorisdisplayed.Thisestablishesthatinputparameterspassedbyreferencecannotbechangedwithinthemethod.

    Dump REPORTYSUBDEL.

    DATA:numTYPEI.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:METH1IMPORTINGINPUT1TYPEI

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 27/131

    value(input2)typei.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    Input1=4.

    write:/5input1.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOC1.

    CREATEOBJECT:OREF1.

    num=3.

    CALLMETHODOREF1>METH1EXPORTINGINPUT1=4

    input2=num.

    Output Oncompilation,anerrormessageisgenerated.

    Now,insteadofchanginginput1,changetheimportparameterinput2(passedbyvalue)withinthemethod.Theprogramwillgetsuccessfullycompiledandexecuted.

    2.3UseofPREFERREDPARAMETERinamethod

    Theme IftherearemorethanoneOPTIONALimportparametersinamethodandnononoptionalimportparameterswithoutvalues,onecantypeintheclausePREFERREDPARAMETERafterthe

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 28/131

    listofimportparameterstospecifywhichoftheoptionalparameterswillgetmorepreferencecomparedtootherswhenthemethodwillbecalledusingsyntax:CALLMETHODobjref>meth().

    Inotherwords,itdecideswhichoftheoptionalparameterswillbeassignedthevalueVAL.

    ProgramDescription

    ThisprogramcontainsaclassC1containingmethodMETH1whichhastwooptionalparameters,INPUT1andINPUT2.Outofthem,parameterINPUT2isdeclaredaspreferredparameter.Themethodsimplydisplaysthevalueoftwoimportparameters.

    Noticethelastlineoftheprogramandseetheoutput.TheoutputwillestablishthatthepreferredparameterINPUT2getsthevaluepassedtothemethodwhenitiscalledusingthesyntax:

    CALLMETHODobjref>meth().

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:METH1IMPORTINGINPUT1TYPEIoptional

    input2TYPEIOPTIONAL

    PREFERREDPARAMETERINPUT2.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    write:/5input1,

    /5input2.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOC1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 29/131

    CREATEOBJECT:OREF1.

    CALLMETHOD:OREF1>METH1(input1=5input2=3).

    skip2.

    write:/5Nextcall.

    callmethodoref1>meth1(10).

    Output 5

    3

    Nextcall

    0

    10

    2.4UseofEXPORTandCHANGINGparametersofamethod

    Theme ThisprogramwilldemonstratetheuseofEXPORTINGandCHANGINGparametersofamethod.

    Programdescription

    TheprogramcontainsamethodTAX_CALCbelongingtotheclassCTAX.ItreceivesGRADEasIMPORTINGparameterandSALARYasCHANGINGparameter.Basedonthegrade,theEXPORTINGparameterITAXiscalculatedandtheCHANGINGparameter,SALARYismodifiedbydeductingtaxfromit.

    REPORTYSUBDEL.

    DATA:w_taxtypepdecimals2,

    w_salarytypepdecimals2.

    CLASSCTAXDEFINITION.

    PUBLICSECTION.

    METHODS:TAX_CALCIMPORTINGgradeTYPEC

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 30/131

    EXPORTINGitaxTYPEP

    CHANGINGsalaryTYPEP.

    ENDCLASS.

    CLASSCTAXIMPLEMENTATION.

    METHOD:TAX_CALC.

    CASEgrade.

    WHENA01.

    itax=salary*0.2.

    WHENA02.

    itax=salary*0.1.

    WHENOTHERS.

    itax=salary*0.15.

    ENDCASE.

    salary=salaryitax.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOCTAX.

    CREATEOBJECT:OREF1.

    w_salary=30000.

    w_tax=0.

    write:/5Beforemethodcall,salaryandtaxare,

    w_salary,

    w_tax.

    CALLMETHODOREF1>TAX_CALCEXPORTINGgrade=A01

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 31/131

    IMPORTINGitax=w_tax

    CHANGINGsalary=w_salary.

    write:/5Aftermethodcall,salaryandtaxare,

    w_salary,

    w_tax.

    Output Beforemethodcall,salaryandtaxare30,000.000.00

    Aftermethodcall,salaryandtaxare24,000.006,000.00

    2.5MethodusingInternalTableasoneoftheparameters

    Theme Thisprogramdemonstrateshowaninternaltablecanbeusedasoneoftheinterfaceparametersofamethod.

    ProgramDescription

    Theprogramcontainsamethod:GETMARAinclass:GET_MATERIALS.Itacceptsmaterialgroup,MATGRasimportparameteranddetailsoutthedetailsofthematerialsbelongingtothatmaterialgroupintoI_TAB,whichisaninternaltableusedasEXPORTINGparameterforthemethod.

    REPORTYSUBOOPS5.

    types:beginoftyp_tab,

    matnrlikemaramatnr,

    meinslikemarameins,

    endoftyp_tab.

    data:itabtypestandardtableoftyp_tab,

    x_tabLIKELINEOFITAB.

    CLASSget_materialsDEFINITION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 32/131

    PUBLICSECTION.

    METHODS:getmaraIMPORTINGmatgrTYPEC

    EXPORTINGl_tabTYPEANYTABLE.

    endclass.

    CLASSget_materialsIMPLEMENTATION.

    METHOD:getmara.

    SELECTmatnrmeinsINTOTABLEl_tab

    FROMMARA

    WHEREMATKL=matgr.

    ENDMETHOD.

    ENDCLASS.

    PARAMETERS:p_matkllikemaramatkl.

    STARTOFSELECTION.

    DATA:w_matTYPEREFTOget_materials.

    CREATEOBJECT:w_mat.

    CALLMETHODw_mat>getmaraEXPORTINGmatgr=p_matkl

    IMPORTINGl_tab=itab.

    LOOPATITABINTOX_TAB.

    WRITE:/5X_TABMATNR,X_TABMEINS.

    ENDLOOP.

    Output One/morethanonerecordswithmaterialnumberandbasicunit,dependingonthematerialgroupenteredintheselectionscreen.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 33/131

    2.6UseofRETURNINGparametersinmethod

    Theme Togetsomevaluesfromamethod,onecanusetheEXPORTING,CHANGINGorRETURNINGparameters.

    IfoneusesRETURNINGparameters,thefollowingrestrictionsapply:(1)NoEXPORTING/CHANGINGparameterscanbeusedforthemethod.

    (2)OnlyoneRETURNINGparametercanbeused.

    (3)RETURNINGparametersareonlypassedbyvalue.

    ThisprogramdemonstratestheuseofRETURNINGparametersandthevariouswaystocallamethodwithRETURNINGparametertogetthevalueintosomevariable.

    ProgramDescription

    MethodM1inclassC1havetwoinputparameters(INPUT1andINPUT2),whichareusedtoderivevalueforRETURNINGparameter,RESULT.

    Theprogramdemonstratesvarioussyntaxesthatcanbeusedtocallamethodofthiskind.

    reportysubdel1messageid00.

    data:w_numtypei.

    classc1definition.

    publicsection.

    methods:m1importinginput1typei

    input2typei

    returningvalue(result)typei.

    endclass.

    classc1implementation.

    method:m1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 34/131

    result=input1*2+input2.

    endmethod.

    endclass.

    startofselection.

    data:obj1typereftoc1.

    createobjectobj1.

    *Syntax1

    callmethodobj1>m1EXPORTINGinput1=5

    input2=4

    RECEIVINGresult=w_num.

    write:/5w_num.

    *Syntax2

    w_num=obj1>m1(input1=10input2=20).

    write:/5w_num.

    *Syntax3

    moveobj1>m1(input1=2input2=3)tow_num.

    write:/5w_num.

    Output 14

    40

    7

    2.7DemoonStaticMethod

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 35/131

    Theme Thisprogramwillshowhowtodeclareanddefineastaticmethodandhowitcanbecalledusingclasscomponentselector.

    Inthefollowingprogram,method:TESTMETHODisdefinedasstaticmethodandiscalledlaterusingclasscomponentselector,=>.

    Dump REPORTYSUBOOPS19.

    data:numtypei.

    classtestclassdefinition.

    publicsection.

    classmethods:testmethod.

    endclass.

    classtestclassimplementation.

    method:testmethod.

    num=5.

    write:/5num.

    endmethod.

    endclass.

    startofselection.

    callmethodtestclass=>testmethod.

    Output 5

    2.8Staticmethodscanonlyusestaticattributes,instancemethodsuseboth

    Theme Staticmethodsofaclasscanonlyusestaticattributesofthatclass.Itcannotuseinstanceattributes.But,instancemethodscanuseboth.

    Program ThefollowingprogramcontainsaclassC1whichcontainsthe

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 36/131

    Description following:

    Component Type Static/Instance

    stnum Data static

    Instnum Data Instance

    Stmeth Method Static

    Instmeth Method InstanceBoththestaticandinstancemethodsareattemptingtodisplayvaluesofthestaticandinstanceattributes:STNUMandINSTNUM.

    Oncompilation,anerrorwillbegeneratedwhichwilldemonstratethatstaticmethodSTMETHcannotworkwithinstanceattribute,INSTNUM.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    CLASSDATA:STNUMTYPEIVALUE5.

    DATA:INSTNUMTYPEIVALUE6.

    CLASSMETHODS:STMETH.

    METHODS:INSTMETH.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:STMETH.

    WRITE:/5STNUM.

    WRITE:/5INSTNUM.

    ENDMETHOD.

    METHODINSTMETH.

    WRITE:/5STNUM.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 37/131

    WRITE:/5INSTNUM.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOC1.

    CALLMETHODc1=>stmeth.

    CREATEOBJECTOREF1.

    CALLMETHODoref1>instmeth.

    Output Oncompilation,yougetthefollowingerror:

    Removethelineinboldintheprogramandcompile.Itwillgetsuccessfullycompiledandexecuted.

    2.9MethodRaisingExceptions

    Theme Methodscanraiseexceptionslikefunctionmoduleswhichcanbehandledaftercallingthemethod,dependingonvarioussysubrcvalues.Thisprogramwilldemonstratethat.

    Programdescription

    Theprogramprovidestheuseraselectionscreenwheretheuserentersanumericvalue.Iftheuserentryis=5.Else,fivetimesofthevalueenteredisdisplayedbytheprogramonexecution.

    TheclassC1inthisprogramcontainsmethodM1whichimportsvalueforNUM1,andreturnsfivetimesofitthroughtheexportparameter,NUM2.

    However,ifthevaluepassedtoNUM1islesserthan5,itraisesanexceptionE1withsomeerrormessage.

    ThemethodM1iscalledaftercreatinganobjectfromtheclass.User

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 38/131

    ThemethodM1iscalledaftercreatinganobjectfromtheclass.Userenteredvalueintheparameterfield:P_NOispassedtoimportingparameterNUM1.

    Dump reportysubdel1messageid00.

    classc1definition.

    publicsection.

    methods:m1importingnum1typei

    exportingnum2typei

    exceptionse1.

    endclass.

    classc1implementation.

    method:m1.

    ifnum1lt5.

    messagei398(00)withShouldbe>=5raisinge1.

    else.

    num2=num1*5.

    endif.

    endmethod.

    endclass.

    parameters:p_notypei.

    startofselection.

    data:obj1typereftoc1.

    createobjectobj1.

    callmethodobj1>m1exportingnum1=p_no

    importingnum2=p_no

    exceptionse1=1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 39/131

    IFsysubrc0.

    MESSAGEIDSYMSGIDTYPESYMSGTYNUMBERSYMSGNO

    WITHSYMSGV1SYMSGV2SYMSGV3SYMSGV4.

    ELSE.

    write:/5p_no.

    ENDIF.

    Output Theprogramprovidestheuseraselectionscreenwheretheuserentersanumericvalue.Iftheuserentryis=5.Else,fivetimesofthevalueenteredisdisplayedbytheprogramonexecution.

    2.10Methodcancallitself

    Theme Methodofaclasscancallitself.But,pleasedonotforgettospecifysomeexitpointtothemethod.Else,itwillcreateaninfiniteloop.

    ProgramDescr.

    TheprogramfollowingcontainsamethodM1inaclassC1.Itincreasesthevalueofstaticvariable,STATNUMby10anddisplaysit.Then,ifthevalueofSTATNUMis

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 40/131

    ifstatnumgt100.

    exit.

    endif.

    write:/5statnum.

    callmethodm1.

    endmethod.

    endclass.

    startofselection.

    data:obj1typereftoc1.

    createobjectobj1.

    callmethodobj1>m1.

    Output 10

    20

    .

    100

    2.11UseofMEinmethods

    Theme Amethodcanhaveavariabledefinedwithinithavingthesamenameasoneoftheattributesoftheclasstowhichthemethodbelongsto.

    Toclearlyidentifytheclasslevelattribute,theselectorMEisused.

    ProgramDescription

    ClassTESTCLASScontainsmethodTESTMETHOD.ThereisavariableI_NUMdeclaredaspublicattributeintheclassaswellasin

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 41/131

    theimplementationpartofthemethod.

    ToaccessthevariableI_NUMattheclasslevelwithinthemethod,theselectorMEisused.Pleaseseetheouputsofthisprogramforbetterunderstanding.

    Dump REPORTYSUBOOPS17.

    classtestclassdefinition.

    publicsection.

    data:i_numtypeivalue5.

    methods:testmethod.

    endclass.

    classtestclassimplementation.

    method:testmethod.

    data:i_numtypeivalue2.

    write:/5me>i_num,accessvariableoftheclass

    /5i_num.accessvariableofthemethod

    endmethod.

    endclass.

    startofselection.

    data:i_numtypei.

    data:my_objtypereftotestclass.

    createobject:my_obj.

    callmethodmy_obj>testmethod.

    Output 5

    2

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 42/131

    2.12PointerTables

    Theme Thisprogramwilldemonstratetheuseofpointertables

    ProgramDescription

    Theprogrambelowusespointertable:MYOBJ_TAB.

    REPORTYSUBOOPS19.

    classtestclassdefinition.

    publicsection.

    methods:testmethod.

    classdata:numtypei.

    endclass.

    classtestclassimplementation.

    method:testmethod.

    num=num+5.

    write:/5num.

    endmethod.

    endclass.

    startofselection.

    data:myobjtypereftotestclass,

    myobj_tabtypetableofreftotestclass.

    do5times.

    createobjectmyobj.

    appendmyobjtomyobj_tab.

    enddo.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 43/131

    loopatmyobj_tabintomyobj.

    callmethod:myobj>testmethod.

    endloop.

    Output 5

    10

    15

    20

    25

    2.13DynamicMethodCalls

    Theme Onecancallmethodsdynamically.Followingrestrictionsapply:

    vNameofthemethodcanbedynamicforstatic/instancemethod.

    vNameoftheclasswhilecallingstaticmethodcanbedynamic.

    vBoththenameofthestaticmethodandtheclasscontainingitcanbedynamic.

    Whiledoingso,itisbettertouseuppercasetoassignthenameoftheclass/methodstothevariables(whichwillbeusedfordynamicassignment)

    ProgramDescription

    ThefollowingprogramcontainsclassC1withstaticmethod(STATM)anddynamicmethod(INSTM).Theprogramutilizesallthesyntaxestocallthesestatic/instancemethodsdynamically.

    Dump REPORTYSUBOOPS19.

    data:f(6)typec,

    g(10)typec.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 44/131

    classc1definition.

    publicsection.

    classmethods:statm.

    methods:instm.

    endclass.

    classc1implementation.

    method:statm.

    write:/5Iamstaticmethod.

    endmethod.

    method:instm.

    write:/5Iaminstantmethod.

    endmethod.

    endclass.

    startofselection.

    data:oreftypereftoc1.

    createobjectoref.

    *Nameofinstancemethodcanbedynamic

    f=INSTM.callmethodoref>(f).

    *Nameofstaticmethodcanbedynamic

    f=STATM.callmethodoref>(f).

    *Nameoftheclasscanbedynamicforstaticmethodcall

    f=C1.callmethod(f)=>statm.

    *Nameofthemethodcanbedynamicforstaticmethodcall

    f=STATM.callmethodc1=>(f).

    *Bothcanbedynamicforstaticmethodcall

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 45/131

    g=C1.callmethod(g)=>(f).

    Output Iaminstantmethod

    Iamstaticmethod

    Iamstaticmethod

    Iamstaticmethod

    Iamstaticmethod

    2.14Useofparametertable

    Theme Fordynamicmethodcall,onecanusetheconceptofPARAMETERTABLEtoincludereferencesofallinterfaceparametersofthemethod(insteadofmentioningthemseparately).

    Prog.

    Descr.

    TheprogramcontainsclassC1withamethodM1.ThismethodhasoneIMPORTINGparameter,P1andoneEXPORTINGparameterP3.

    P3iscalculatedwithinthemethod.

    Here,parametertablePTABisusedtodynamicallycallthemethodM1.

    Pleasenotethat,parametertablesworkonlywhenamethodiscalleddynamically(i.e.today,thenameofthemethod/classisdeterminedatruntime).

    Dump REPORTYSUBOOPS24.

    DATA:i_resulttypei,

    i_numtypeivalue5.

    DATAf(2)TYPEcVALUEM1.

    CLASScl_abap_objectdescrDEFINITIONLOAD.

    DEFINE:poptable.

    ptab_linename=&1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 46/131

    ptab_linekind=CL_ABAP_OBJECTDESCR=>&2.

    GETREFERENCEOF&3INTOptab_linevalue.

    INSERTptab_lineINTOTABLEptab.

    IFsysubrcne0.

    EXIT.

    ENDIF.

    ENDOFDEFINITION.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    METHODSm1IMPORTINGp1TYPEi

    exportingp3typei.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHODm1.

    p3=p1+200.

    ENDMETHOD.

    ENDCLASS.

    DATArTYPEREFTOc1.

    DATA:ptabTYPEabap_parmbind_tab,

    ptab_lineLIKELINEOFptab.

    STARTOFSELECTION.

    poptable:P1EXPORTINGi_num,

    P3IMPORTINGi_result.

    CREATEOBJECTrTYPEc1.

    CALLMETHODr>(f)PARAMETERTABLEptab.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 47/131

    write:/5i_result.

    Output 205

    2.15UseofExceptionTable

    Theme Insteadofdealingwitheachandeveryexceptionandassigningittodifferentsysubrcvalues,onecanuseexceptiontabletohandletheexceptionswhenamethodiscalleddynamically.

    TheclassC1containsmethodM1whichraisesanexception.ExceptiontableETABisusedtohandletheexception.

    REPORTYSUBOOPS25.

    CLASScl_abap_objectdescrDEFINITIONLOAD.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    METHODSm1EXCEPTIONSexc.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHODm1.

    RAISEexc.

    ENDMETHOD.

    ENDCLASS.

    DATArTYPEREFTOobject.

    DATAf(3)TYPEcVALUEM1.

    DATA:etabTYPEabap_excpbind_tab,

    etab_lineLIKELINEOFetab.

    STARTOFSELECTION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 48/131

    etab_linename=EXC.

    etab_linevalue=4.

    INSERTetab_lineINTOTABLEetab.

    IFsysubrcne0.

    EXIT.

    ENDIF.

    CREATEOBJECTrTYPEc1.

    CALLMETHODr>(f)EXCEPTIONTABLEetab.

    WRITEsysubrc.

    Output 4

    3Constructors

    3.1InstanceConstructorsgetfiredatthetimeofclassinstantiation

    Theme Thissimpleprogramwillshowyouthatinstanceconstructormethodsofaclassgettriggeredwhenanobjectiscreatedfromtheclass.

    ProgramDescription

    ThisprogramcontainsaclassC1withaconstructormethodwhichwritesoutsomethingtoindicatethatitistriggered.IntheSTARTOFSELECTIONblock,theclassC1isinstantiated,whichtriggerstheinstanceconstructormethod(asisevidentbytheoutputasreport).

    Thisestablishesthetheme.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 49/131

    Dump REPORTYSUBOOPS1.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:CONSTRUCTOR.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHODconstructor.

    WRITE:/5Iamconstructor.

    skip2.

    ENDMETHOD.

    ENDCLASS.

    ***************mainprogram**************

    STARTOFSELECTION.

    DATA:obj1TYPEREFTOc1.

    CREATEOBJECT:obj1.

    Output Iamconstructor

    3.2InstanceConstructorscanhaveimportparameters

    Theme Instanceconstructorscanhaveimportparameters.ValuestothemarepassedatthetimeofCREATEOBJECTstatementtocreateobjectfromtheclasscontainingtheconstructor.

    ProgramDescription

    TheprogramcontainsaclassC1whichhasoneinstanceconstructorwithoneimportparameter.TheconstructorgetsfiredatthetimeofCREATEOBJECTstatement.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 50/131

    Dump REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    METHODS:CONSTRUCTORimportingtodaytyped.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHODconstructor.

    Write:/5Todayis:,todaydd/mm/yyyy.

    ENDMETHOD.

    ENDCLASS.

    ***************mainprogram**************

    STARTOFSELECTION.

    DATA:obj1TYPEREFTOc1.

    CREATEOBJECT:obj1exportingtoday=sydatum.

    Output Todayis08.04.2004

    3.3Constructorscannothaveanyexportparameters

    Theme Thisprogramwilldemonstratethatconstructormethodscannothaveanyexportparameters

    ProgramDescription

    Thisprogramattemptstocreateaconstructorwithexportparameter,whichistrappedandresistedatthetimeofcompilation.

    Thisestablishesthetheme.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 51/131

    Dump REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    METHODS:CONSTRUCTORexportingnametypec.

    ENDCLASS.

    Output Compilationerrrorisreported.

    3.4InstanceConstructorscanraiseexceptions

    Theme InstanceConstructormethodscanraiseexceptions

    ProgramDescriptions

    ThisprogramcontainsaclassC1whichcontainsaninstanceconstructormethod.Itacceptsanimportparameter,NUM.Ifitislesserthan7,anexceptionisraised,whichisproperlyhandledbyspecifyingsomesysubrcvalueatthetimeofCREATEOBJECTstatementandlaterhandledproperly.

    Dump REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    METHODS:CONSTRUCTORimportingnumtypei

    exceptionse1.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHODconstructor.

    ifnumlt7.

    raisee1.

    endif.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 52/131

    ENDMETHOD.

    ENDCLASS.

    ***************mainprogram**************

    STARTOFSELECTION.

    DATA:obj1TYPEREFTOc1.

    CREATEOBJECT:obj1exportingnum=5

    exceptionse1=2.

    ifsysubrc=2.

    write:/5Exceptionsraised.

    endif.

    Output ExceptionsRaised

    3.5Useofstaticconstructor.

    Theme Therearetwoprogramsoverherewhichwillshowyousimpleexampleonastaticconstructor.Youwilllearnhowtodeclareastaticconstructor.Static/classconstructorsgettriggeredbeforeanyofthefollowingevents:

    vGeneratinganinstanceofaclassusingCREATEOBJECTobj(),whereobjhasthedatatypeREFTOclass.

    vCallingastaticmethodusing[CALLMETHOD]class=>meth().

    vRegisteringastaticeventhandlermethodusingSETHANDLERclass=>methforobj().

    vRegisteringaneventhandlermethodforastaticevent()oftheclassclass.

    vAddressingastaticattribute()withclass=>a.

    Thesetwoprogramswillshowyouthataclassconstructorgetsfired

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 53/131

    Thesetwoprogramswillshowyouthataclassconstructorgetsfiredbeforeanyofitsstaticcomponentsareaccessed,oranobjectiscreatedfromtheclass.

    Dump REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    CLASSDATA:NUMTYPEIVALUE5.

    CLASSMETHODS:CLASS_CONSTRUCTOR.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHODCLASS_CONSTRUCTOR.

    WRITE:/5Iamclassconstructor.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:OREFTYPEREFTOC1.

    CREATEOBJECTOREF.

    REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    CLASSDATA:NUMTYPEIVALUE5.

    CLASSMETHODS:CLASS_CONSTRUCTOR.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 54/131

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHODCLASS_CONSTRUCTOR.

    WRITE:/5Iamclassconstructor.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    WRITE:/5C1=>NUM.

    ConstructoristriggeredwhenastaticConstructorisfiredwhenanobjectis

    attributeisaccessedcreatedfromtheclass

    Output Forthefirstprogram(ontheLHS):

    Iamclassconstructor

    5

    Forthesecondprogram(ontheRHS):

    Iamclassconstructor

    3.6Staticconstructorcanbetriggeredatthebeginningofaprocessingblock(form/event/block/procedure)

    Theme IntheSTARTOFSELECTIONblockofthisprogram,staticattributeofaclasscontainingclassconstructorwillbeaccessed.Thiswilldemonstratethatthethefirstthingwhichwillgetexecutedinthe

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 55/131

    STARTOFSELECTIONblockistheclassconstructormethod,irrespectiveofthepointatwhichthestaticattributeisaccessed.

    ProgramDescription

    TheprogramcontainsaclassC1withastaticconstructor,whichprintsthestatement:Iamclassconstructor.

    Thisclassalsocontainsastaticattribute,numofvalue=5.

    TheSTARTOFSELECTIONblockinthisprogramcontainsthefollowingstatements:

    Awritestatementwhichwillprint:Hello

    Acalltoaccessthestaticattribute(NUM)ofclassC1.

    Onexecutionofthisprogram,wewillgetthefollowingoutputinthelist:

    Iamclassconstructor

    Hello

    5

    insteadoftheexpectedoutput:

    Hello

    Iamclassconstructor

    5

    Thisdemonstratesthetheme.

    Dump REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    CLASSDATA:NUMTYPEIVALUE5.

    CLASSMETHODS:CLASS_CONSTRUCTOR.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHODCLASS_CONSTRUCTOR.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 56/131

    WRITE:/5Iamclassconstructor.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    write:/5Hello.

    write:/5c1=>num.

    Output Iamclassconstructor

    Hello

    5

    3.7Static/Classconstructorscannothaveanyinterface

    Theme Thisprogramwillshowyouthatstaticconstructorsofaclasscannothaveanyinterfaceparametersandexceptions

    ProgramDescription

    Inthisprogram,theclassC1containsaclassconstructorwhichishavinganimportparameter,NUM.Theprogramcouldnotbesuccessfullycompiledduetosuchattempt.

    Dump REPORTYSUBOOPS2.

    CLASSc1DEFINITION.

    PUBLICSECTION.

    CLASSMETHODS:CLASS_CONSTRUCTORIMPORTINGNUMTYPEC.

    ENDCLASS.

    Output Compilationoftheprogranwillfailwithanerrormessage:

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 57/131

    ThemethodCLASS_CONSTRUCTORmaynothaveparametersorEXCEPTIONS.

    4Inheritance

    4.1Subclasscanaccesspublic/protectedcomponentsofsuperclass

    Theme Thisprogramwilldemonstrate:

    vHowtocreateasubclassfromasuperclass.

    vSubclasscanaccesspublic/protectedcomponents(methods,attributesetc)ofsuperclass.

    Prog.Descr.

    ThisprogramcontainssuperclassC1anditssubclassC2.ClassC1hasthefollowingcomponents:

    Component Nature SectionofExistence

    Significance/action

    NUM Attribute public Value=6

    METH1 Method Public DisplaysvalueofNUM

    METH2 Method Protected Displays:Iammeth2

    NUM2 Attribute Protected Value=7SubclassofC1isC2whichcontainsthefollowingnewcomponents:

    Component Nature SectionofExistence

    Significance/action

    M1 Method public Callsmethodmeth1.Callsmethodmeth2Displaysvalueofvariablenum2.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 58/131

    IntheSTARTOFSELECTIONblock,anobjectOREFiscreatedfromC2andthemethodM1iscalled.TheoutputoftheprogramshowsthatmethodM1ofclassC2callsmethodMETH1,thenMETH2andfinallydisplaysvariableNUM2.

    ThisdemonstratesthatsubclassC2hasaccesstothepublicandprotectedmethodsandattributesofsuperclassC1andtrulyestablishesthetheme.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:METH1.

    DATA:NUMTYPEIVALUE6.

    PROTECTEDSECTION.

    DATA:num2typeivalue7.

    METHODSMETH2.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    WRITE:/5num.

    endmethod.

    METHOD:METH2.

    WRITE:/5Iammeth2.

    ENDMETHOD.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    PUBLICSECTION.

    METHODS:M1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 59/131

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    METHODM1.

    CALLMETHOD:meth1,meth2.

    write:/5num2.

    endmethod.

    endclass.

    STARTOFSELECTION.

    DATA:OREFTYPEREFTOC2.

    CREATEOBJECTOREF.

    CALLMETHOD:OREF>M1.

    Output 6

    Iammeth2

    7

    4.2Subclasscanreimplementinheritedmethodsfromsuperclass

    Theme Subclasscanreimplementtheinheritedpublicandprotectedmethodsfromsuperclass.

    ProgramDescr.

    ClassC1containsmethodMETH1(public)andMETH2(protected),bothofwhicharemodifiedandreimplementedinitssubclassC2.ObjectsarecreatedoutofbothclassesandthemethodMETH1forbothobjectsarecalled.

    Outputoftheprogramdemonstratesdifferentbehaviourformethod

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 60/131

    OutputoftheprogramdemonstratesdifferentbehaviourformethodMETH1ofclassC1andC2.

    Thisdemonstratesthetheme.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:METH1.

    PROTECTEDSECTION.

    METHODSMETH2.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    WRITE:/5Iammeth1inclassC1.

    CALLMETHODMETH2.

    ENDMETHOD.

    METHOD:METH2.

    WRITE:/5Iammeth2inclassC1.

    ENDMETHOD.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    PUBLICSECTION.

    METHODS:METH1redefinition.

    PROTECTEDSECTION.

    METHODS:METH2redefinition.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 61/131

    METHODMETH1.

    WRITE:/5Iammeth1inclassC2.

    callmethodmeth2.

    endmethod.

    METHOD:METH2.

    WRITE:/5Iammeth2inclassC2.

    ENDMETHOD.

    endclass.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOC1,

    OREF2TYPEREFTOC2.

    CREATEOBJECT:OREF1,OREF2.

    CALLMETHOD:OREF1>METH1,

    OREF2>METH1.

    Output Iammeth1inclassC1

    Iammeth2inclassC1

    Iammeth1inclassC2

    Iammeth2inclassC2

    4.3Objectscannotbecreatedfromanabstractclass.

    Theme Objectscannotbecreatedfromanabstractclass.Onlythesubclassesofsuchclasscanbeinstantiated.

    Program ThisprogramcontainsanabstractclassC1anditssubclassC2.Object

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 62/131

    Description cannotbecreatedfromclassC1,butpossiblefromclassC2.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITIONABSTRACT.

    PUBLICSECTION.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    endclass.

    STARTOFSELECTION.

    data:OREF1TYPEREFTOC1,

    OREF2TYPEREFTOC2.

    CREATEOBJECToref2.

    REPORTYSUBDEL.

    CLASSC1DEFINITIONABSTRACT.

    PUBLICSECTION.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    ENDCLASS.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 63/131

    CLASSC2DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    endclass.

    STARTOFSELECTION.

    data:OREF1TYPEREFTOC1,

    OREF2TYPEREFTOC2.

    CREATEOBJECToref1.

    InstantiationofabstractclassInstantiationofsubclassofanabstractclass

    Output Instantiationofabstractclasswillberesistedwitherrormessageatthetimeofcompilation.

    Instantiationofsubclassofanabstractclasswillbeallowed.

    4.4Abstractmethodscannotbeimplementedinabstractclass

    Theme Abstractmethodscannotbeimplementedinthatclass.Ithastobeimplementedinoneofitssubclass.Toimplementanabstractmethodinasubclass,oneneedtoredefinethissubclassusingtheREDEFINITIONaddition.

    ProgramDescr.

    ThisprogramcontainsanabstractclassC1withabstractmethodMETH1,whichisimplementedinthesameclass.Theprogramwillnotbecompiledduetothis.

    Theprogramisthenmodifiedandtheabstractmethodisimplementedin

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 64/131

    TheprogramisthenmodifiedandtheabstractmethodisimplementedinclassC2,subclassofC1.Now,theprogramgetssuccessfullycompiled.

    DumpIncorrect Correct

    REPORTYSUBDEL.

    CLASSC1DEFINITIONABSTRACT.

    PUBLICSECTION.

    METHODS:METH1ABSTRACT.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHOD:METH1.

    WRITE:/5Iammethod:METH1.

    ENDMETHOD.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    endclass.

    STARTOFSELECTION.

    data:OREF2TYPEREFTOC2.

    CREATEOBJECToref2.

    REPORTYSUBDEL.

    CLASSC1DEFINITION

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 65/131

    CLASSC1DEFINITIONABSTRACT.

    PUBLICSECTION.

    METHODS:METH1ABSTRACT.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    publicsection.

    methods:meth1redefinition.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    METHOD:METH1.

    WRITE:/5Iammethod:METH1.

    ENDMETHOD.

    endclass.

    STARTOFSELECTION.

    data:OREF2TYPEREFTOC2.

    CREATEOBJECToref2.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 66/131

    4.5Finalclassescannothaveanysubclass

    Theme SubclassescannotbeinheritedfromafinalClass.Theycanonlybeinstantiated.

    ProgramDescrip.

    ThisprogramcontainsafinalclassC1andasubclassC2.Thisisnotallowedandisresistedatthetimeofcompilation.

    Hence,thethemeisproperlyestablished.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITIONFINAL.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    endclass.

    STARTOFSELECTION.

    data:OREF2TYPEREFTOC2.

    CREATEOBJECToref2.

    Output Compilationerrorisgenerated:

    ThefinalclassC1cannothaveanysubclasses.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 67/131

    4.6Finalmethodscannotberedefinedinthesubclasses

    Theme Finalmethodinaclasscanonlybedefinedinthatclass.Itcannotberedefinedinanyofitssubclasses.

    ProgramDescr.

    ThisprogramcontainsaclassC1whichhasafinalmethod:METH1.

    ClassC2issubclassofC1andtriestoredefinethefinalmethod.

    Compilationerrorisgenarated,resistingsuccessfulcompilation.

    Thisdemonstratesthetheme.

    Dump REPORTYSUBDEL.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:METH1FINAL.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    methodmeth1.

    write:/5Iammethodmeth1.

    endmethod.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    PUBLICSECTION.

    methods:meth1redefinition.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 68/131

    method:meth1.

    write:/5Iammeth1,modifiedinclassC2.

    Endmethod.

    endclass.

    STARTOFSELECTION.

    data:OREF2TYPEREFTOC2.

    CREATEOBJECToref2.

    Output Programisnotsuccessfullycompiled.Thecompilationerrormessageisasfollows:

    ThefinalmethodMETH1cannotberedefined.

    4.7Staticattributesexistonlyonceperinheritancetree

    Theme Staticattributesonlyexistonceineachinheritancetree.Onecanchangethemfromoutsidetheclassusingtheclasscomponentselectorwithanyclassname,orwithinanyclassinwhichtheyareshared.Theyarevisibleinallclassesintheinheritancetree.

    ProgramDescr.

    ClassC1containsstaticattribute,NUM.

    ClassC2andC3aresubclassesofclassC1.

    IntheSTARTOFSELECTIONblock,thestaticattribute,NUMischangedusingreferenceofclassC3.

    ItgetschangedwithreferencetoclassC2also.

    Hence,staticattribute,NUM,changedviaclassC3isalsochangedwithrespecttoC2also.Thisdemonstratesthetheme.

    Dump REPORTYSUBDEL.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 69/131

    CLASSC1DEFINITION.

    PUBLICSECTION.

    classdata:numtypei.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    endclass.

    CLASSC3DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    STARTOFSELECTION.

    C3=>NUM=10.

    WRITE:/5C2=>NUM.

    Output 10

    4.8Constructorsofsuperclassflowsdownthechain

    Theme Constructorofsuperclassisinheritedbythesubclassalso.

    ProgramDescr.

    ClassC1containsaconstructormethod,butitssubclassC2doesnotexplicitlyhaveitsown.

    WhenobjectfromclassC2iscreated,theconstructorofclassC1istriggered.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 70/131

    Thisestablishesthetheme.

    Dump REPORTYSUBOOPS18.

    CLASSC1DEFINITION.

    PUBLICSECTION.

    METHODS:CONSTRUCTOR.

    ENDCLASS.

    CLASSC1IMPLEMENTATION.

    METHODconstructor.

    WRITE:/5IamC1.

    skip.

    ENDMETHOD.

    ENDCLASS.

    CLASSC2DEFINITIONINHERITINGFROMC1.

    ENDCLASS.

    CLASSC2IMPLEMENTATION.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:objtypereftoC2.

    CREATEOBJECT:obj.

    Output IamC1.

    4.9Subclasscanhaveenhancedconstructorthanitssuperclass.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 71/131

    Theme Asubclasscanmodifytheconstructormethodandaddsomeextrafunctionalities.Intheinstanceconstructormethodofthechildclass,theoneforthesuperclassshouldbecalledfirstusing:

    CALLMETHODsuper>CONSTRUCTORstatementandthenadditionalstatementscanbeadded.

    Pl.notethatREDEFINITIONstatementisnotrequiredtoenhanceconstructorsforasubclass.

    ProgramDescrip.

    Thisprogramcontainsthreeclasses:

    vGRANDFATHERatthetopofthenodewithitsownconstructormethod

    vFATHER,subclassofGRANDFATHERwithenhancedconstructor

    vSON,subclassofFATHERwithitsownenhancedconstructor.

    AnobjectiscreatedfinallyfromtheclassSON,whichtriggerstheconstructormethodsintheorder:GRANDFATHERFATHERSON.

    REPORTYSUBOOPS18.

    CLASSgrandfatherDEFINITION.

    PUBLICSECTION.

    METHODS:CONSTRUCTOR.

    ENDCLASS.

    CLASSgrandfatherIMPLEMENTATION.

    METHODconstructor.

    WRITE:/5Iamgrandfather.

    skip.

    ENDMETHOD.

    ENDCLASS.

    CLASSfatherDEFINITIONINHERITINGFROMGRANDFATHER.

    publicsection.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 72/131

    METHODS:CONSTRUCTOR.

    ENDCLASS.

    CLASSfatherIMPLEMENTATION.

    METHODconstructor.

    callmethodsuper>constructor.

    WRITE:/5Iamfather.

    skip.

    ENDMETHOD.

    ENDCLASS.

    CLASSsonDEFINITIONINHERITINGFROMFATHER.

    publicsection.

    METHODS:CONSTRUCTOR.

    ENDCLASS.

    CLASSsonIMPLEMENTATION.

    METHODconstructor.

    callmethodsuper>constructor.

    WRITE:/5Iamson.

    skip.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:mysontypereftoson.

    CREATEOBJECT:myson.

    Output Iamgrandfather

    Iamfather

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 73/131

    Iamson

    4.10Staticconstructorofaclassiscalledonlyonceperprogram.

    Theme Thefirstwhenasubclassinaprogramisaccessed,itsstaticconstructorisexecuted.But,beforeitcanbeexecuted,thestaticconstructorsofallofitssuperclassesmustalreadyhavebeenexecuted.Astaticconstructormayonlybecalledonceperprogram.Therefore,whenonefirstaddressasubclass,thesystemlooksforthenexthighestsuperclasswhosestaticconstructorhasnotyetbeenexecuted.Itexecutesthestaticconstructorofthatclass,followedbythoseofallclassesbetweenthatclassandthesubclassthatisaddressed.

    ProgramDescr.

    Thisprogramcontainsthreeclasses:

    vFATHERwithitsownstaticconstructormethod

    vSON,subclassofFATHERwithitsownstaticconstructormethod.

    AnobjectiscreatedfinallyfromtheclassSON,whichtriggerstheconstructormethodsintheorder:FATHERSON.

    Now,anobjectiscreatedfromtheclassFATHER.But,thatdidnottriggerconstructorofclassFATHER,becausethathadalreadybeentriggeredbytheprogramwhenanobjectwascreatedfromtheclassSON.

    Thisestablishesthetheme.

    Dump REPORTYSUBOOPS18.

    CLASSfatherDEFINITION.

    publicsection.

    classMETHODS:class_CONSTRUCTOR.

    ENDCLASS.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 74/131

    CLASSfatherIMPLEMENTATION.

    METHODclass_constructor.

    WRITE:/5Iamfather.

    skip.

    ENDMETHOD.

    ENDCLASS.

    CLASSsonDEFINITIONINHERITINGFROMFATHER.

    publicsection.

    classMETHODS:class_CONSTRUCTOR.

    ENDCLASS.

    CLASSsonIMPLEMENTATION.

    METHODclass_constructor.

    WRITE:/5Iamson.

    skip.

    ENDMETHOD.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:mysontypereftoson.

    CREATEOBJECT:myson.

    data:myfathertypereftofather.

    createobject:myfather.

    Output Iamfather

    Iamson

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 75/131

    4.11StatictypeandDynamictypeofavariable

    Theme Statictypeofareferencevariablecanpointtoasuperclass;whereasitsdynamictypecanpointtooneofitssubclasses.

    Thisprogramwillshowyouvariouswaystodothat.

    ProgramDescrip.

    ThisprogramcontainsclassC1withmethodM1.ClassC2isasubclassofC1andcontainsredefinedimplementationofmethodM1.

    Referencevariablesarecreatedintheprogramasfollows:

    ReferenceVariable

    Statictype

    Dynamictype

    Dynamictypeassignedby

    OREF1 C1 C1 CREATEobjectoref1.

    OREF11 C1 C2 CREATEOBJECToref11TYPEC2.

    OREF111 C1 C2 CREATEOBJECTOREF111.

    OREF111=OREF2.

    OREF2 C2 C2 CREATEOBJECToref2.Finally,methodM1iccalledusingallobjects.Theobservationsareasfollows:

    Methodcall

    Callsmethod Reason

    OREF1>M1 M1ofclassC1.

    BothStatic&DynamictypeofOREF1referstoC1.

    OREF11>M1

    M1ofclassC2.

    StatictypeofOREF11referstoC1,dynamictypetoC2.

    OREF111>M1

    M1ofclassC2.

    StatictypeofOREF111referstoC1,dynamictypetoC2.

    OREF2>M1 M1ofclassC2 BothStatic&DynamictypeofOREF2referstoC2.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 76/131

    Dump REPORTYSUBOOPS18.

    classc1definition.

    publicsection.

    methods:m1.

    endclass.

    classc1implementation.

    methodm1.

    write:/5Iamm1ofc1.

    endmethod.

    endclass.

    classc2definitioninheritingfromc1.

    publicsection.

    methods:m1redefinition.

    endclass.

    classc2implementation.

    methodm1.

    write:/5Iamm1ofc2.

    endmethod.

    endclass.

    STARTOFSELECTION.

    DATA:OREF1TYPEREFTOC1,

    OREF11TYPEREFTOC1,

    OREF111TYPEREFTOC1,

    OREF2TYPEREFTOC2.

    CREATEOBJECT:OREF1,

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 77/131

    OREF11TYPEC2,

    OREF111,

    OREF2.

    OREF111=OREF2.

    CALLMETHOD:OREF1>M1,Output:Iamm1ofc1

    OREF11>M1,Output:Iamm1ofc2

    OREF111>M1,Output:Iamm1ofc2

    OREF2>M1.Output:Iamm1ofc2

    Output Iamm1ofc1

    Iamm1ofc2

    Iamm1ofc2

    Iamm1ofc2

    4.12Statictypeshouldbemoregeneralthandynamictypeofareferencevariable

    Theme Statictypeofareferencevariablecanrefertoasuperclass,whereasitsdynamictypecanrefertoasubclassofthesuperclass.Inthatcase,thereferencevariablewillidentifyallthecommoncomponentsofthesuperclassandsubclass.Itwillnotbeabletoidentifyanynewcomponentsinthesubclass,whicharenotpresentinitssuperclass.

    ProgramDescr.

    ThisprogramcontainsclassC1anditssubclassC2.

    ClassC1containsmethodM1,whichisalsoredefinedinclassC2.

    ClassC2containsanewmethodM2.

    AreferencevariableOREF11iscreatedwithstatictypeofC1anddynamictypeofC2.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 78/131

    MethodM2isattemptedtobecalledusingOREF11.

    Thisproducescompilationerror,establishingthetheme.

    Dump REPORTYSUBOOPS18.

    classc1definition.

    publicsection.

    methods:m1.

    endclass.

    classc1implementation.

    methodm1.

    write:/5Iamm1ofc1.

    endmethod.

    endclass.

    classc2definitioninheritingfromc1.

    publicsection.

    methods:m1redefinition.

    methods:m2.

    endclass.

    classc2implementation.

    methodm1.

    write:/5Iamm1ofc2.

    endmethod.

    methodm2.

    write:/5Iamm2.

    endmethod.

    endclass.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 79/131

    STARTOFSELECTION.

    DATA:OREF11TYPEREFTOC1.

    CREATEOBJECT:OREF11TYPEC2.

    CALLMETHOD:OREF11>M2.

    Output CompilationerrorfailstoidentifymethodM2inthelaststatementoftheprogram.

    4.13Methodofaparentclass,usedfromitssubclass,usesattributesoftheparentclassonly,ifthemethodisnotredefinedinsubclass.

    Theme Aslongasamethod(usingprivateattributes)inheritedfromasuperclassisnotredefined,itstillusestheprivateattributesofthesuperclass,notthoseofthesubclass,evenifthesubclasshasprivateattributesofthesamename.

    ProgramDescrip.

    ClassC1containsamethodM1inthepublicsectionandaprivatevariable,NUMofvalue=5.MethodM1inclassC1displaysthevalueofprivatevariable,NUM.

    ClassC2isasubclassofclassC1.ItdoesnotredefinemethodM1.But,ithasalsoaprivatevariable,NUMwithvalue=6.

    AnobjectiscreatedfromclassC2andthemethodM1iscalled.

    TheoutputshowsthatthevariableNUM(asdisplayedbymethodM1)hasbeentakenfromclassC1,notC2.

    Thisestablishesthetheme.

    Dump reportysubdel.

    CLASSc1DEFINITION.

    PUBLICSECTION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 80/131

    METHODS:m1.

    PRIVATESECTION.

    DATA:numTYPEIVALUE5.

    ENDCLASS.

    CLASSc1IMPLEMENTATION.

    METHOD:m1.

    write:/5num.

    ENDMETHOD.

    ENDCLASS.

    CLASSc2DEFINITIONINHERITINGFROMc1.

    PUBLICSECTION.

    DATA:numTYPEIVALUE6.

    ENDCLASS.

    CLASSc2IMPLEMENTATION.

    ENDCLASS.

    STARTOFSELECTION.

    DATA:oref2TYPEREFTOc2.

    CREATEOBJECT:oref2.

    CALLMETHODoref2>m1.

    Output 5

    4.14DemoonWideningCast

    Theme Thisprogramwillshowtheuseofwideningcastoperator

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 81/131

    ProgramDescription

    ClassC1issuperclassofC2.ObjectofclassC2isassignedtheobjectofclassC1usingwideningcastoperator.Thishelpstoavoidthecompilationerror,butgenaratestheerrorcx_sy_move_cast_errortrappedatruntime,whichishandledandreportedproperlyintheprogram.

    Dump REPORTYSUBDEL.

    classc1definition.

    publicsection.

    data:numtypeivalue5.

    endclass.

    classc1implementation.

    endclass.

    classc2definitioninheritingfromc1.

    publicsection.

    endclass.

    classc2implementation.

    endclass.

    startofselection.

    data:obj1typereftoc1,

    obj2typereftoc2.

    createobject:obj1,

    obj2.

    TRY.

    obj2?=obj1.

    CATCHcx_sy_move_cast_error.

    write:/5cx_sy_move_cast_errortrapped.

    ENDTRY.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 82/131

    Output cx_sy_move_cast_errortrapped

    5Interface

    5.1Simpleuseofaninterface

    Theme Thisprogramwillshowsimpleuseofaninterfacewithitsowndataandmethodsandhowitisimplementedinaclass.Itwillalsoshowthattherecanbemethodsofsamenameforaninterfaceandtheclassimplementingtheinterface.

    ProgramDesc

    ThisprogramcontainsaninterfaceI1withattribute:NUManmethod:METH1.

    Thisinterfaceisimplementedinaclass:C1whichalsohasitsownmethodMETH1.

    AnobjectOREFiscreatedfromclassC1andboththemethodsMETH1,oneforclassandanotherforinterfaceiscalledusingtheobject.

    Dump reportysubdel.

    interfacei1.

    data:numtypei.

    methods:meth1.

    endinterface.

    classc1definition.

    publicsection.

    methods:meth1.classC1sownmethod

    interfaces:i1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 83/131

    endclass.

    classc1implementation.

    method:meth1.

    write:/5Iammeth1inc1.

    endmethod.

    methodi1~meth1.

    write:/5Iammeth1fromi1.

    endmethod.

    endclass.

    startofselection.

    data:oreftypereftoc1.

    createobjectoref.

    write:/5oref>i1~num.

    callmethodoref>meth1.

    callmethodoref>i1~meth1.

    Output 0

    Iammeth1inc1

    Iammeth1fromi1

    5.2Interfacescanonlybeimplementedinthepublicsectionofaclass

    Theme Thisprogramwillshowyouthatclassesimplementinganinterfacecanonlycontainthefeaturesoftheinterfaceinitspublicsection.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 84/131

    ProgramDescription

    Inthisprogram,classC1istryingtoaccommodateinterfaceI1initsPRIVATESECTION.

    Thiscreatesacompilationerror,establishingthetheme.

    Dump reportysubdel.

    interfacei1.

    methods:meth1.

    endinterface.

    classc1definition.

    protectedsection.

    interfaces:i1.

    endclass.

    Output Compilationerrorwitherrormessage:

    INTERFACESmayonlybeimplementedinthepublicsection.

    5.3Aclasswithaninterfaceshouldimplementallthemethodsofthatinterface

    Theme Thisprogramwillshowthataclasscontaininganinterfaceshouldimplementallthemethodsoftheinterfaceinitsimplementationsection.

    ProgramDescrip

    ClassC1implementsinterfaceI1,whichhasgottwomethods,METH1andMETH2.But,intheIMPLEMENTATIONsectionofclassC1,onlyMETH1isimplemented.

    Thisprogramwillcreateacompilationerror,establishingthetheme.

    Dump reportysubdel.

    interfacei1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 85/131

    methods:meth1,

    meth2.

    endinterface.

    classc1definition.

    publicsection.

    interfaces:i1.

    endclass.

    classc1implementation.

    methodi1~meth1.

    write:/5Iammeth1fromi1.

    endmethod.

    endclass.

    startofselection.

    data:oreftypereftoc1.

    createobjectoref.

    callmethodoref>i1~meth1.

    Output Compilationerrorwitherrormessage:

    ImplementationmissingformethodI1~METH2

    5.4Valuesforinterfaceattributesareassignedatthetimeofinclusioninaclass

    Theme Onecannotspecifyvaluesforattributeswhiledeclaringtheminaninterfacelikethefollowingfashion:DATA:TYPEVALUE

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 86/131

    .

    Insteadofdoingthat,onehastospecifythevaluesfordifferentattributesofinterfaceatthepointwheretheinterfaceisdeclaredinthepublicsectionofaclass.

    ProgramDescr.

    InterfaceI1containstwonumericattributes,NUM1andNUM2.

    Inversion1oftheprogram,attemptismadetospecifythevalueswhiledefiningthoseattributesintheinterface.Thisversiondoesnotgetsuccessfullycompiledandthusestablishesthetheme.

    Inversion2,valuesforinterfaceattributesarespecifiedatthetimewhenI1isincludedinthepublicsectionofclassC1.Thisversiongetssuccessfullycompiledandproducesaresult.

    reportysubdel.

    interfacei1.

    data:num1typei,

    num2typei.

    endinterface.

    classc1definition.

    publicsection.

    interfaces:i1DATAVALUESnum1=5num2=6.

    methodsm1.

    endclass.

    classc1implementation.

    methodm1.

    write:/5i1~num1,

    i1~num2.

    endmethod.

    endclass.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 87/131

    startofselection.

    data:oreftypereftoc1.

    createobjectoref.

    callmethodoref>m1.

    reportysubdel.

    interfacei1.

    data:num1typeivalue5,

    num2typeivalue6.

    endinterface.

    classc1definition.

    publicsection.

    interfaces:i1.

    methodsm1.

    endclass.

    classc1implementation.

    methodm1.

    write:/5i1~num1,

    i1~num2.

    endmethod.

    endclass.

    startofselection.

    data:oreftypereftoc1.

    createobjectoref.

    callmethodoref>m1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 88/131

    Version1Version2

    Output Outputofversion1:

    Compilationerrorwitherrormessage:Withinaninterface,youcannotuseVALUEwithattributes(exceptconstants)

    Outputofversion2:

    56

    5.5UseofFINALmethodsfromInterface

    Theme Thisprogramwilldemonstratehowtocreatefinalmethodinaclassfromoneofthemethodsofaninterface.

    ProgramDescription

    InterfaceI1containstwomethods:M1andM2.

    I1isincludedandincorporatedinclass:C1withM2asafinalmethod.BoththemethodsareimplementedinclassC1.

    ClassC2isasubclassofclassC1.Itredefinesmethod:I1~M1andreimplementsit,butitdoesnotdothatforI1~M2asthatisdeclaredasfinalmethod.

    IntheSTARTOFSELECTIONblock,objectOREF1iscreatedfromclassC1andOREF2fromclassC2andboththemethodsM1andM2arecalledusingboththeobjects.

    Dump reportysubdel.

    interfacei1.

    methods:m1,

    m2.

    endinterface.

    classc1definition.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 89/131

    publicsection.

    interfaces:I1finalmethodsm2.

    endclass.

    classc1implementation.

    methodi1~m1.

    write:/5Iamm1inc1.

    endmethod.

    methodi1~m2.

    write:/5Iamm2inc1.

    endmethod.

    endclass.

    classc2definitioninheritingfromc1.

    publicsection.

    methods:i1~m1redefinition.

    endclass.

    classc2implementation.

    method:i1~m1.

    write:/5Iamm1inc2.

    endmethod.

    endclass.

    startofselection.

    data:oref1typereftoc1,

    oref2typereftoc2.

    createobject:oref1,oref2.

    callmethod:oref1>i1~m1,Output:Iamm1inc1

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 90/131

    oref2>i1~m1,Output:Iamm1inc2

    oref1>i1~m2,Output:Iamm2inc1

    oref2>i1~m2.Output:Iamm2inc1

    Output Iamm1inc1

    Iamm1inc2

    Iamm2inc1

    Iamm2inc1

    5.6UseofAbstractmethodsfromInterface

    Theme Thisprogramwilldemonstratethewaybywhichamethodfromaninterfacecanbeusedasanabstractmethodforaclass.

    Abstractmethodscanonlybeimplementedbythesubclasses.

    Aclasscontaininganabstractmethodshouldbeabstractitself.

    ProgramDesc.

    vThisprogramcontainsaninterfaceI1withtwomethods,M1andM2.

    vClassC1includesandimplementsmethodsofinterfaceI1,declaringmethodM2asanabstractmethod.Hence,classC1wasalsodeclaredasanabstractclass.

    vClassC1implementsmethodI1~M1,butnotI1~M2asthisisanabstractmethod.

    vClassC2isasubclassofC1,whichdefinesI1~M2.

    vIntheSTARTOFSELECTIONblock,objectOREF2iscreatedfromclassC2(classC1cannotbeinstantiated,asthisisanabstractclass)andboththemethods:I1~M1andI1~M2arecalled.

    Dump reportysubdel.

    interfacei1.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 91/131

    methods:m1,

    m2.

    endinterface.

    classc1definitionabstract.

    publicsection.

    interfaces:I1abstractmethodsm2.

    endclass.

    classc1implementation.

    methodi1~m1.

    write:/5Iamm1inc1.

    endmethod.

    endclass.

    classc2definitioninheritingfromc1.

    publicsection.

    methods:i1~m2redefinition.

    endclass.

    classc2implementation.

    method:i1~m2.

    write:/5Iamm2inc2.

    endmethod.

    endclass.

    startofselection.

    data:oref2typereftoc2.

    createobject:oref2.

    callmethod:oref2>i1~m1,

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 92/131

    oref2>i1~m2.

    Output Iamm1inc1

    Iamm2inc2

    5.7UseofInterfaceReferenceVariable

    Theme Thisprogramwillshowtheuseofinterfacereferencevariableandhowitcanbeusedtoaccessthecomponentsofaninterfaceinaclass(implementingthatinterface).Useofinterfacereferencevariablepavesthewayforpolymorphismviainterface.

    ProgramDescrip.

    InterfaceI1,included/implementedinclassC1hasthefollowingcomponents:

    component nature

    C_name Constantwithvalue=ABAP

    inum Instanceattributewithvalue=5

    cnum Staticattributeofvalue6

    M1 Instancemethod

    M2 StaticmethodClassC1implementsallthemethodsinitsIMPLEMENTATIONsection.

    IntheSTARTOFSELECTIONblock,allthedifferentattributesandmethodsarecalledusingclassreferencevariableOREFfirst.

    Then,interfacereferencevariableIREFisusedtodosimilarjobs,aftertheassignmentIREF=OREF.

    Dump reportysubdel.

    interfacei1.

    constants:c_name(4)typecvalueABAP.

    data:inumtypei.

    classdata:cnumtypei.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 93/131

    methods:m1.

    classmethods:m2.

    endinterface.

    classc1definition.

    publicsection.

    interfaces:I1datavaluesinum=5cnum=6.

    endclass.

    classc1implementation.

    methodi1~m1.

    write:/5Iamm1inc1.

    endmethod.

    methodi1~m2.

    write:/5Iamclassmethodm2inc1.

    endmethod.

    endclass.

    startofselection.

    data:ireftypereftoi1,

    oreftypereftoc1.

    createobject:oref.

    write:/5oref>i1~inum,

    oref>i1~cnum,

    c1=>i1~cnum.

    callmethod:oref>i1~m1,

    oref>i1~m2,

    c1=>i1~m2.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 94/131

    write:/5syuline.

    iref=oref.

    write:/5iref>inum,

    iref>cnum,

    i1=>c_name.

    callmethod:iref>m1,

    iref>m2.

    Output 566

    Iamm1inc1

    Iamclassmethodm2inc1

    Iamclassmethodm2inc1

    56ABAP

    Iamm1inc1

    Iamclassmethodm2inc1

    5.8UseofNestedInterface

    Theme Thisprogramwilldemonstratehowaninterfacecanbeincludedinanotherinterface.Andthefinalinterfacecontainingalltheinterfacescanbeusedinsideaclass.

    ProgramDescr.

    vInterfaceI1containsmethodM1.

    vInterfaceI2includesinterfaceI1.However,italsocontainstwomethodsM1andM2ofitsown.

    vClassC1containsinterfaceI2andimplementsmethods:I1~M1,I2~M1andI2~M2.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 95/131

    vObjectiscreatedfromclassC1andallthemethodsarecalled.

    reportysubdel.

    interfacei1.

    methodsm1.

    endinterface.

    interfacei2.

    methods:m1,m2.

    interfacesi1.

    endinterface.

    classc1definition.

    publicsection.

    interfaces:i2.

    endclass.

    classc1implementation.

    method:i1~m1.

    write:/5Iamm1fromi1.

    endmethod.

    method:i2~m1.

    write:/5Iamm1fromi2.

    endmethod.

    method:i2~m2.

    write:/5Iamm2fromi2.

    endmethod.

    endclass.

    STARTOFSELECTION.

  • 22/12/2014 ABAPOOConceptTopicwiseExample|maagnusabap

    http://maagnusabap.wordpress.com/2012/04/23/abapooconcepttopicwiseexample/ 96/131

    data:oreftypereftoc1.

    createobjectoref.

    callmethod:oref>i1~m1,Output:Iamm1fromi1

    oref>i2~m1,Output:Iamm1fromi2

    oref>i2~m2.Output:Iamm1fromi2

    Output Iamm1fromi1

    Iamm1fromi2

    Iamm2fromi2

    5.9UsingALIASES

    Theme ThefullnameofacomponentwhichanInterfaceaddstoaclassoranotherinterfaceisintf~comp.Aliasnamescanbesubstitutedforthisnamewhen

    definingcompoundinterfaces,or

    declaringInterfacesinaclass

    ProgramDescr.

    InterfaceI1containsmethodM1.

    InterfaceI2containsmethod:M1andM2and