ooabap notes with programs

Upload: vbvrk

Post on 14-Apr-2018

243 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/30/2019 Ooabap Notes With Programs

    1/93

    1 | P a g e S a n t o s h P

    VINAYAKA

    Features Of OOPS: -

    1) Encapsulation2) Data Abstraction3) Inheritance4) PolymorphismApplication Of OOPS: -

    1) BAPIS2) BADIS3) Enhancement Frame Work4) Webdynpro5) HR-ABAP6) CRM-Technical7) SRM8) EP9) BSPClass: - A class is a user defined data type which is the collection of different type of components.

    A class only provides a template its doesnt allocate a memory.Object: - An instance of a class is called as an object.

    Whenever we instance a class memory will be allocated.Access Specifies (Visibility Section): -

    Types Of Class: -

    1) Local Class Local class to program (SE38-ABAP Editor).2) Global Class Class Builder Tool (SE24).Syntax for creating local classes: -

    1) Definition of class Declaration of components.2)

    Implementation of class

    Implementation of method.

  • 7/30/2019 Ooabap Notes With Programs

    2/93

    2 | P a g e S a n t o s h P

    Definition of class

    Class definition.

    Declaration of components.

    Endclass.

    Implementation class

    Class implementation.

    Implementation of methods.

    Endclass.

    Object Creation: -

    1)

    Create reference for the class

    Syntax: -

    Data type ref to

    Note: - Whenever an Object is created memory will be allocated for the attributes of the class and

    attribute gets initialized to default values.

    Access specifiers in ABAP: -

    1) Public Section2) Protected Section3) Private SectionComponents of ABAP classes: -

    Attributes Methods Events Interface aliases

    Types Constants Data Special Normal Instance Static

    Instance Instance

    Constructor

    Instance Static

    Static StaticConstructor

  • 7/30/2019 Ooabap Notes With Programs

    3/93

    3 | P a g e S a n t o s h P

    1) OOPS Concepts2) ALV Reporting Using Class3) ALV Reporting Using Function ModulesProcedure for creating global classes: -

    1) Define and implement the class in class builder tool (SE24).2) Access the components of the global class in the repository objects (Executable Programs,

    Include Program, and Subroutine Pool). By instantiating the class.

    Z915AM_OOPS1: (LOCAL CLASS)

    REPORT Z915AM_OOPS1.

    CLASS EMP DEFINITION.

    PUBLICSECTION.

    DATA EMPNO TYPEI. "instance attributeDATA ENAME(20) TYPEC. "instance attribute

    ENDCLASS. "emp DEFINITION

    DATA KTYPEREFTO EMP.CREATE OBJECT K.

    WRITE :/ K->EMPNO,

    K->ENAME.

    K->EMPNO = 1.

    K->ENAME = 'abc'.

    WRITE:/ K->EMPNO,

    K->ENAME.

    OUTPUT:

    At any point of time they object store one set of values.

    Note: - Only public component can be accessed outside of the class.

  • 7/30/2019 Ooabap Notes With Programs

    4/93

    4 | P a g e S a n t o s h P

    Z915AM_CLASS1: (GLOBAL CLASS) SE24:

    Z915AM_OOPS2:

    REPORT Z915AM_OOPS2.

    datamtyperefto z915am_class1.

    CREATE OBJECT m.

    write :/ m->empno,

    m->ename.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    5/93

    5 | P a g e S a n t o s h P

    Interacting with methods in local classes: -

    1) Declare the method prototype in the class definition.Syntax: -

    Method/Class-Method [parameters].

    2) Implement the method in the class implementation.Syntax: -

    Method .

    Statements.

    Endmethod.

    3) Call the method.Syntax: -

    Call method [parameters]

    Instance method methods

    Static method class-methodsIt is recommend to use in the SAP attributes/data members/Instance variables protected/private.

    Methods/member function public.

    Note: - Whenever a report program contains class implementation and explicitly we need handlethe event start-of-selection to indicate the starting point of program.

    Z915AM_OOPS3:

    REPORT Z915AM_OOPS3.

    class abc definition.publicsection.

    methods : m1,m2.

    protectedsection.data : empno typei,

    ename(20) typec.endclass.

    class abc implementation.method m1.

    empno = 1.ename = 'xyz'.

    endmethod.

    method m2.write :/ empno,

    ename.endmethod.

    endclass.

    start-of-selection.

    data k typerefto abc.create object k.

    callmethod k->m2.

  • 7/30/2019 Ooabap Notes With Programs

    6/93

    6 | P a g e S a n t o s h P

    k->m1( ).k->m2( ).OUTPUT:

    Method with parameters: -

    Methods: m1 importing X type i,

    Y(20) type c.

    Methods returning values: -

    Returning Keyword: -

    In case of other object oriented language a method can return exactly one value whichdone by using return keyword.

    In case of ABAP a method can return any number of values by declaring those manynumber o exporting (or) importing (or) changing parameters.

    To receive a method a return exactly one value we use returning parameters. It the method contains returning parameters it cannot contains exporting (or) changing

    parameters.

    A method can contains only one returning parameter. Returning parameter are always passed by value.

    Z915AM_OOPS4:

    REPORT Z915AM_OOPS4.

    class abc definition.

    publicsection.

    methods : m1 importingxtypeioptional

    y typecoptional,m2.

  • 7/30/2019 Ooabap Notes With Programs

    7/93

    7 | P a g e S a n t o s h P

    protectedsection.data : empno typei,

    ename(20) typec.endclass.

    class abc implementation.method m1.

    empno = x.

    ename = y.

    endmethod.

    method m2.

    write :/ empno,

    ename.

    endmethod.

    endclass.

    start-of-selection.

    data ktyperefto abc.

    create object k.

    parameters : p_x typei,

    p_y(20) typec.

    callmethod k->m1

    exporting

    x = p_x

    y = p_y.

    callmethod k->m2.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    8/93

    8 | P a g e S a n t o s h P

    Z915AM_CLASS2:

    method M1.

    empno = x.

    ename = y.

    call method m2.

    endmethod.

    method M2.write :/ empno,

    ename.

    endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    9/93

    9 | P a g e S a n t o s h P

    Z915AM_OOPS5:

    REPORT Z915AM_OOPS5.

    data ktyperefto z915am_class2.

    CREATE OBJECT k.

    parameters : p_x typei,

    p_y(20) typec.

    CALLMETHOD k->m1

    EXPORTING

    X = p_x

    Y = p_y.

    OUTPUT:

    Z915AM_OOPS6:

    REPORT Z915AM_OOPS6.class abc definition.

    publicsection.

    methods m1 importingxtypei

    y typei

    exportingmtypei

    n typei.

    endclass.

    class abc implementation.method m1.

    m = x + y.

    n = x - y.endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    10/93

    10 | P a g e S a n t o s h P

    endclass.

    start-of-selection.data ktyperefto abc.

    create object k.

    data : lv_r1 typei,

    lv_r2 typei.

    callmethod k->m1

    exporting

    x = 200

    y = 100

    importing

    m = lv_r1

    n = lv_r2.

    write :/ lv_r1,lv_r2.OUTPUT:

    Z915AM_OOPS7:REPORT Z915AM_OOPS7.

    class abc definition.

    publicsection.methods m1 importingxtypei

    y typei

    returning value(z) typei.

    endclass.

    class abc implementation.

    method m1.

    z = x + y.

    endmethod.

    endclass.

    start-of-selection.

    data ob typerefto abc.create object ob.

    parameters : p_x typei,

  • 7/30/2019 Ooabap Notes With Programs

    11/93

    11 | P a g e S a n t o s h P

    p_y typei.

    data lv_z typei.

    lv_z = ob->m1( x = p_x y = p_y ).

    write :/ lv_z.

    OUTPUT:

    Exception handling in methods: -

    An exception is runtime error which is raised during the program execution if theexception is not handling the program will be terminated.

    Exception handling is process of handling in runtime error and containing programexecution.

    The exceptions are provided by SAP as part of standard exceptions class these exceptionsare triggered by SAP itself as a developer we need to handle these exceptions by using try

    and catch block.

    Inside try block we need to declare this statement where the possible exception occurs. It the exception is raised in try blocked SAP creates the appropriate exception class object

    and the control is transfer to catch block.

    Inside the catch block we need to handle the exception by writing the appropriateexception handling statements.

    All the exception classes provided by SAP start with the naming standardCX_SY_...........

  • 7/30/2019 Ooabap Notes With Programs

    12/93

    12 | P a g e S a n t o s h P

    Note: - As part of catch block declaration we need to specify the exception class which isreasonable for rising exception if the developer is not sure of the exception class we can use the

    exception class CX_ROOT.

    CX_ROOT is a super class for all the exception classes and it can handle any kind ofexception.

    Z915AM_OOPS8:REPORT Z915AM_OOPS8.

    class abc definition.

    publicsection.

    methods m1 importingxtypei

    y typei

    exportingztypei.

    endclass.

    class abc implementation.method m1.

    try.z = x / y.

    * catch cx_sy_zerodivide.catch cx_root.

    write :/ 'Cannot divide by zero'.

    endtry.

    endmethod.

    endclass.

    start-of-selection.

    data ob typerefto abc.

    create object ob.

    data lv_z typei.

    callmethod ob->m1

    exporting

    x = 10

    y = 0importing

    z = lv_z.

    write :/ 'Diviison is ',lv_z.

    write :/ 'end of program'.

  • 7/30/2019 Ooabap Notes With Programs

    13/93

    13 | P a g e S a n t o s h P

    OUTPUT:

    Z915AM_OOPS9:

    REPORT Z915AM_OOPS9.

    DATA KTYPEREFTO CX_SY_ZERODIVIDE.

    DATA STRTYPE STRING.

    DATA : PRGNAME TYPE SY-REPID,

    INCNAME TYPE SY-REPID,POS TYPEI.

    CLASS ABC DEFINITION.

    PUBLICSECTION.METHODS M1 IMPORTINGXTYPEI

    Y TYPEIEXPORTINGZTYPEI.

    ENDCLASS. "abc DEFINITION

    CLASS ABC IMPLEMENTATION.

    METHOD M1.

    TRY.

    Z = X / Y.

    CATCH CX_SY_ZERODIVIDE INTO K.

    CALLMETHOD K->IF_MESSAGE~GET_TEXT

    RECEIVING

    RESULT = STR.

    WRITE :/ 'short text is ',STR.

    CLEARSTR.

    CALLMETHOD K->IF_MESSAGE~GET_LONGTEXTRECEIVING

    RESULT = STR.

    WRITE :/ 'Long text is ',STR.

    CALLMETHOD K->GET_SOURCE_POSITIONIMPORTING

  • 7/30/2019 Ooabap Notes With Programs

    14/93

    14 | P a g e S a n t o s h P

    PROGRAM_NAME = PRGNAMEINCLUDE_NAME = INCNAME

    SOURCE_LINE = POS.

    WRITE :/ 'Program name :',PRGNAME,

    / 'Include name :',INCNAME,/ 'line no :',POS.

    ENDTRY.

    ENDMETHOD. "m1

    ENDCLASS. "abc IMPLEMENTATION

    START-OF-SELECTION.

    DATA OB TYPEREFTO ABC.

    CREATE OBJECT OB.

    DATA LV_Z TYPEI.

    CALLMETHOD OB->M1

    EXPORTINGX = 10

    Y = 0IMPORTING

    Z = LV_Z.

    WRITE :/ 'Diviison is ',LV_Z.

    WRITE :/ 'end of program'.

    OUTPUT:

    CX_SY_ZERODIVIDE:

  • 7/30/2019 Ooabap Notes With Programs

    15/93

    15 | P a g e S a n t o s h P

    Capturing system defined exception message: -

    Procedure for handling standing exception: -

    These exception are declared and raised SAP as a developer we need to handle try and catch.

    User defines exception: -

    These exceptions are declaring and handle are raised developers itself.

    Procedure for handling user-define exception in local classes: -

    1) Declare the user define exception as part of method declaration.Syntax: -

    Exception

    2) Raise the exception at appropriate place in the method implementationRaise

    3) Handle the exception while calling the method by checking sy-subrc status.

    Z915AM_OOPS10:REPORT Z915AM_OOPS10.

    class abc definition.

    publicsection.methods m1 importingxtypei

    y typei

    exportingztypei

    exceptions divideerror.

    endclass.

    class abc implementation.

    method m1.

    ify eq0.

    raise divideerror.

    else.z = x / y.

    endif.endmethod.

    endclass.

    start-of-selection.

    data ob typerefto abc.

    create object ob.

    data lv_z typei.

    callmethod ob->m1exporting

  • 7/30/2019 Ooabap Notes With Programs

    16/93

    16 | P a g e S a n t o s h P

    x = 10y = 0

    importingz = lv_z

    exceptions

    divideerror = 1others = 2.

    ifsy-subrc eq0.

    write :/ 'division is ',lv_z.

    elseifsy-subrc eq1.

    write :/ 'Cannot divide by zero'.

    elseifsy-subrc eq2.

    write :/ 'Unknown error'.

    endif.

    OUTPUT:

    Z915AM_CLASS3:

    method M1.

    if y eq 0.

    raise divideerror.

  • 7/30/2019 Ooabap Notes With Programs

    17/93

    17 | P a g e S a n t o s h P

    else.z = x / y.

    endif.endmethod.

    Z915AM_OOPS11:REPORT Z915AM_OOPS11.

    data lv_z typei.

    data ob typerefto z915am_class3.

    create object ob.

    CALLMETHOD ob->m1

    EXPORTING

    x = 10

    y = 0

    IMPORTINGZ = lv_z

    EXCEPTIONSDIVIDEERROR = 11

    others = 22.

    ifsy-subrc eq0.

    write :/ lv_z.

    elseifsy-subrc eq11.

    message'Cannot divide by zero'type'I'.elseifsy-subrc eq22.

    message'Unknown error'type'I'.

    endif.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    18/93

    18 | P a g e S a n t o s h P

    Instance attribute: -

    These attribute are specific to an object and they are declared by using the keyword datain local classes.

    For each instance attribute separate memory will be allocated they can be an accessedonly by using the object of the class.Static attribute: -

    They are not specific to any object and they are declared by using the keywordCLASS_DATA in local classes.

    For the static attribute memory will be allocated only when the first object is created theremaining objects points to be same memory location they are also called as class

    variables.

    They can be accessed either by using the object are by using the class name.Instance methods: -

    In local classes they are declared by using the keyword methods they can be accessed only byusing the object. They can access both instance and static attributes.

    Static methods: -

    In local class they are declaring the keyword CLASS-METHOD in the local classes they

    can be accessed either by using object are by using class name. They access only static attributes.

    Z915AM_OOPS12:REPORT Z915AM_OOPS12.

    class abc definition.

    publicsection.

    dataxtypei. "instance attribute

    class-data y typei. "static attribute

    endclass.

    start-of-selection.

    data ob1 typerefto abc.create object ob1.

    write :/ 'Values of object ob1'.

    write :/ ob1->x,

    ob1->y,

    abc=>y.

    ob1->x = 10.

    ob1->y = 20.

    write :/ 'Values of object ob1'.write :/ ob1->x,

  • 7/30/2019 Ooabap Notes With Programs

    19/93

    19 | P a g e S a n t o s h P

    ob1->y.

    data ob2 typerefto abc.create object ob2.

    write :/ 'Values of object ob2'.write :/ ob2->x,

    ob2->y.

    OUTPUT:

    Z915AM_OOPS13:

    REPORT Z915AM_OOPS13.

    class abc definition.publicsection.

    methods m1.

    class-methods m2.

    protectedsection.

    dataxtypei.

    class-data y typei.

    endclass.

    class abc implementation.method m1.

    x = 10.

    y = 20.

    WRITE : x.

    endmethod.

    method m2.y = 20.

    * x = 10.

    write: y.

    endmethod.

    endclass.

  • 7/30/2019 Ooabap Notes With Programs

    20/93

    20 | P a g e S a n t o s h P

    start-of-selection.

    callmethod abc=>m2.abc=>m2( ).

    *abc=>m1( ).

    OUTPUT:

    Constructors: -A constructor is a special method used for initialized for attributes of class it special because

    it cannot be called explicitly it will be called implicitly.

    It is always declare in public section. It never returns any values.

    1) Instance2) StaticInstance Constructors: - It is declared by using keyword constructor it is executed automatically

    whenever we create new instance of a class. It is specific to object.

    It can contain only importing parameters and exceptions.Static Constructors: - It is declared by using the keyword CLASS_CONSTRUCTORS it is

    executed automatically whenever a class is loaded a class will be loaded in cases.

    1) When we accesses the static components of the class using the class name before creating anyobjects.2) When we create the first object of the class.

    3) It is not specific to any object it cannot contains any parameters and exception.Note: -

    Instance constructor is executed only once in the life time of object. Static constructor is executed only once in a life time of class.

  • 7/30/2019 Ooabap Notes With Programs

    21/93

    21 | P a g e S a n t o s h P

    Z915AM_OOPS14:REPORT Z915AM_OOPS14.

    class abc definition.

    publicsection.

    methods : constructor,m2.

    protectedsection.

    data : empno typei, "INATANCE ATTRI"

    ename(20) typec.

    endclass.

    class abc implementation.

    method constructor.

    empno = 1.

    ename = 'abc'.

    endmethod.

    method m2.

    write :/ empno,ename.endmethod.

    endclass.

    start-of-selection.

    * data ob type ref to abc.

    * create object ob.

    **call method ob->constructor. " u cannot specify constructor directly.

    *call method ob->m2.

    *call method ob->m2.

    data ob1 typerefto abc.

    create object ob1.

    callmethod ob1->m2.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    22/93

    22 | P a g e S a n t o s h P

    Z915AM_OOPS15:REPORT Z915AM_OOPS15.

    class abc definition.

    publicsection.

    methods : constructorimportingxtypeioptionaly typecoptional,

    display.

    protectedsection.

    data : empno typei,

    ename(20) typec.

    endclass.

    class abc implementation.

    method constructor.

    empno = x.

    ename = y.endmethod.

    method display.

    write :/ empno,ename.endmethod.

    endclass.

    start-of-selection.

    parameters : p_x typei,

    p_y(20) typec.

    data ob typerefto abc.

    create object ob

    exporting

    x = p_x

    y = p_y.

    callmethod ob->display.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    23/93

    23 | P a g e S a n t o s h P

    Z915AM_OOPS16:REPORT Z915AM_OOPS16.

    class abc definition.

    publicsection.

    methods constructor.class-methods class_constructor.

    endclass.

    class abc implementation.

    method constructor.

    write :/ 'inside instance const'.

    endmethod.

    method class_constructor.

    write :/ 'inside static const'.

    endmethod.

    endclass.

    start-of-selection.

    data ob1 typerefto abc.create object ob1.

    write :/ 'Second object.........'.

    data ob2 typerefto abc.

    create object ob2.OUTPUT:

    Note: - If a class contains both instance and static constructor and when we create the first object.

    1st

    the static constructor is executed and next instance constructor for rest of the objects only

    instance constructor get executed.

  • 7/30/2019 Ooabap Notes With Programs

    24/93

    24 | P a g e S a n t o s h P

    Z915AM_OOPS15:REPORT Z915AM_OOPS15.

    class abc definition.

    publicsection.

    methods : constructorimportingxtypeioptionaly typecoptional,

    display.

    protectedsection.

    data : empno typei,

    ename(20) typec.

    endclass.

    class abc implementation.

    method constructor.

    empno = x.

    ename = y.

    endmethod.

    method display.write :/ empno,ename.

    endmethod.endclass.

    start-of-selection.

    parameters : p_x typei,p_y(20) typec.

    data ob typerefto abc.

    create object ob

    exporting

    x = p_x

    y = p_y.

    callmethod ob->display.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    25/93

    25 | P a g e S a n t o s h P

    Z915AM_OOPS16:REPORT Z915AM_OOPS16.

    class abc definition.

    publicsection.

    methods constructor.

    class-methods class_constructor.

    endclass.

    class abc implementation.method constructor.

    write :/ 'inside instance const'.endmethod.

    method class_constructor.

    write :/ 'inside static const'.

    endmethod.

    endclass.

    start-of-selection.

    data ob1 typerefto abc.

    create object ob1.

    write :/ 'Second object.........'.data ob2 typerefto abc.

    create object ob2.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    26/93

    26 | P a g e S a n t o s h P

    Z915AM_OOPS17:

    REPORT Z915AM_OOPS17.

    class abc definition.

    publicsection.

    methods constructor.

    class-methods class_constructor.

    class-dataxtypei.

    endclass.

    class abc implementation.

    method constructor.

    write :/ 'inside instance const'.

    endmethod.

    method class_constructor.write :/ 'inside static const'.

    endmethod.

    endclass.start-of-selection.

    *abc=>x = 10.

    data ob1 typerefto abc.

    create object ob1.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    27/93

    27 | P a g e S a n t o s h P

    NORMAL METHOD SPECIAL METHOD CONSTRUCTOR

    It can be declared in any of the sections. Only in public section.

    It has to be called explicitly. It called implicitly.

    A method can have any type o parameters. Instance constructor can have importing

    parameters and static constructor cannot have

    any parameters.

    Methods can return values. It cannot return values.

    It can be called any know of times in the

    lifetime of an object.

    Instance constructor will be called only once

    in the lifetime of every object where as static

    constructor will be called only once in the

    lifetime of class.

    CREATING T-CODE FOR METHOD:

  • 7/30/2019 Ooabap Notes With Programs

    28/93

    28 | P a g e S a n t o s h P

    Z915AM_OOPS18:REPORT Z915AM_OOPS18.

    CLASS ABC DEFINITION.PUBLICSECTION.

    METHODS M1.PROTECTEDSECTION.

    DATAXTYPEI.

    ENDCLASS. "abc DEFINITION

    CLASS ABC IMPLEMENTATION.

    METHOD M1.

    BREAK-POINT.

    LEAVETO LIST-PROCESSING.

    WRITE :/ 'inside method m1'.

    LEAVESCREEN.

    ENDMETHOD. "m1

    ENDCLASS. "abc IMPLEMENTATION

    CLASS PQRDEFINITION.PROTECTEDSECTION.

    DATA : XTYPEI,Y TYPEI.

    ENDCLASS. "pqr DEFINITION

    (SETTING BREAK POINT IN PORGRAM FOR OUR UNDERSTANDING)

    EXECUTING T-CODE: ZT35

  • 7/30/2019 Ooabap Notes With Programs

    29/93

    29 | P a g e S a n t o s h P

    OUTPUT:

    User defined exception: -Raising: - It a method is a capable of raising the exception that enable to handle the exception then

    we need to use the keyword raising as part of method declaration in this case the caller of themethod as to take the responsibility of handling the exception.

    Z915AM_OOPS19:REPORT Z915AM_OOPS19.

    class abc definition.

    publicsection.

    methods m1 importingxtypei

    y typei

    exportingztypei

    raising cx_sy_zerodivide.

    endclass.

    class abc implementation.method m1.

    z = x / y.

    endmethod.endclass.

    start-of-selection.

    data ob typerefto abc.create object ob.

    data rtypei.

    try.callmethod ob->m1

    exporting

    x = 10

    y = 0

    importing

    z = r.

    catch cx_sy_zerodivide.

    message'Cannot divide by zero'type'I'.

    endtry.

    write :/ 'diviison is ',r.

  • 7/30/2019 Ooabap Notes With Programs

    30/93

    30 | P a g e S a n t o s h P

    OUTPUT:

    PRESS ENTER:

    Z915AM_OOPS20:REPORT Z915AM_OOPS20.

    parameters : p_x typei,

    p_y typei.

    dataztypei.try.

    perform abc using p_x p_y changingz.

    catch cx_sy_zerodivide.

    write :/ 'Cannot divide by zero'.

    endtry.

    write :/ 'Diviison is ',z.

    form abc usingm n changing r

    raising cx_sy_zerodivide.

    r = m / n.

    endform.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    31/93

    31 | P a g e S a n t o s h P

    Friend classes: - By default outside the class a object can access only public components of the

    class directly.

    By using friend classes to enable the object to access any components of the class directlyirresponsibility of the visibility for this consider the following session.

    Consider two independent classes A and B.

    If class A considers class B as friend by inside class B methods we can instantiate class Aand use the instance we can access all the component of class A directly irrespective of

    the visibility.

    Z915AM_OOPS21:REPORT Z915AM_OOPS21.

    class pqrdefinition deferred.

    class abc definition friends pqr.

    publicsection.

    methods m1.protectedsection.

    methods m2.

    privatesection.

    methods m3.

    endclass.

    class abc implementation.

    method m1.

    write :/ 'inside public method m1'.endmethod.

    method m2.

    write :/ 'inside protected method m2'.endmethod.

    method m3.

    write :/ 'inside private method m3'.

    endmethod.

    endclass.

    class pqrdefinition.

    publicsection.methods m4.

  • 7/30/2019 Ooabap Notes With Programs

    32/93

    32 | P a g e S a n t o s h P

    endclass.

    class pqrimplementation.method m4.

    write :/ 'inside m4'.

    data ob typerefto abc.create object ob.

    callmethod : ob->m1.

    callmethod : ob->m2.

    callmethod : ob->m3.

    endmethod.

    endclass.

    start-of-selection.

    data ktyperefto pqr.

    create object k.

    callmethod k->m4.

    OUTPUT:

    In the above case the class B should be forward declared by using the keyword deferred. Deferred keyword indicates to SAP that the class definition has been delayed and it has been

    declared same where else in the program.

    Z915AM_CLASS4: (GLOBAL FRIEND CLASS)

  • 7/30/2019 Ooabap Notes With Programs

    33/93

    33 | P a g e S a n t o s h P

    method M1.

    write :/ 'inside method m1'.

    endmethod.

    method M2.write :/ 'inside method m2'.

    endmethod.

    method M3.

    write :/ 'inside method m3'.

    endmethod.

    Z915AM_CLASS5:

  • 7/30/2019 Ooabap Notes With Programs

    34/93

    34 | P a g e S a n t o s h P

    method M4.

    write :/ 'inside m4'.

    data ob type ref to z915am_class4.create object ob.

    call method : ob->m1,ob->m2,

    ob->m3.endmethod.

    Z915AM_OOPS22:

    REPORT Z915AM_OOPS22.

    data ob typerefto z915am_class5.

    create object ob.

    callmethod ob->m4.

    OUTPUT:

    Inheritance: -It is the process o acquiring the properties of other entity (class). The advantage of

    inheritance is reusability. They are three types of inheritance.

    1) Single2) Multiple3) Multilevel

    The class which gives the properties is called as super class are base class and the classwhich takes the properties is called as subclass (or) derived class.

  • 7/30/2019 Ooabap Notes With Programs

    35/93

    35 | P a g e S a n t o s h P

    Only public and protected components can be inherited. In local classes we need to use the keyword inheriting from for achieving inheritance.

    1)

    Single inheritance: -

    A class derived from single super class.

    2) Multiple inheritance: -A class derived from more than one super class.

    Note: - In ABAP we cannot implement multiple inheritance directly we can implemented indirectlythrough the concept of interface.

    3) Multilevel inheritance: -A class derived from another derived class.

    Single inheritance

    Multilevel inheritance Multiple inheritance

    Z915AM_OOPS24:REPORT Z915AM_OOPS24.

    class cycle definition.

    publicsection.methods : setcycle,

    display.protectedsection.

    data : wheels typei,

    brakes typei,

    colour(20) typec.

    endclass.

    class cycle implementation.

    method setcycle.

    wheels = 2.brakes = 2.

    CLASS A

    CLASS B

    CLASS A

    CLASS C

    CLASS A CLASS B

    CLASS C

    CLASS B

  • 7/30/2019 Ooabap Notes With Programs

    36/93

    36 | P a g e S a n t o s h P

    colour = 'green'.endmethod.

    method display.

    write :/ wheels,brakes,colour.

    endmethod.endclass.

    class scooterdefinitioninheritingfrom cycle.

    publicsection.

    methods setscooter.

    data enginemodel typei.

    endclass.

    class scooterimplementation.

    method setscooter.

    wheels = 2.brakes = 4.

    colour = 'red'.endmethod.

    endclass.

    class cardefinitioninheritingfrom scooter.

    publicsection.

    methods setcar.

    endclass.

    class carimplementation.

    method setcar.

    wheels = 4.

    brakes = 5.

    colour = 'blue'.

    endmethod.

    endclass.

    start-of-selection.

    data ob1 typerefto cycle.

    create object ob1.

    write :/ 'CYCLE class..........'.callmethod : ob1->setcycle,

    ob1->display.

    data ob2 typerefto scooter.

    create object ob2.

    write :/ 'SCOOTER class.........'.

    callmethod : ob2->setscooter,

    ob2->display.

    data ob3 typerefto car.

  • 7/30/2019 Ooabap Notes With Programs

    37/93

    37 | P a g e S a n t o s h P

    create object ob3.

    write :/ 'CAR class.........'.callmethod : ob3->setcar,

    ob3->display.

    OUTPUT:

    Z915AMCYCLE:

    method SETCYCLE.wheels = 2.

    brakes = 2.

    colour = 'blue'.

    endmethod.

    method DISPLAY.

    write :/ wheels,brakes,colour.

    endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    38/93

    38 | P a g e S a n t o s h P

    Z915AMSCOOTER:

    method SETSCOOTER.

    wheels = 2.

    brakes = 4.

    colour = 'red'.

    endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    39/93

    39 | P a g e S a n t o s h P

    Z915AMCAR:

    method SETCAR.wheels = 4.

    brakes = 5.colour = 'cyan'.

    endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    40/93

    40 | P a g e S a n t o s h P

    Z915AM_OOPS25:

    REPORT Z915AM_OOPS25.

    data ob1 typerefto z915amcycle.

    create object ob1.

    data ob2 typerefto z915amscooter.

    create object ob2.

    data ob3 typerefto z915amcar.

    create object ob3.

    callmethod : ob1->setcycle,

    ob1->display,

    ob2->setscooter,

    ob2->display,

    ob3->setcar,

    ob3->display.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    41/93

    41 | P a g e S a n t o s h P

    Z915AM_OOPS26:REPORT Z915AM_OOPS26.

    class abc definition final.

    publicsection.

    dataxtypei.endclass.

    class pqrdefinitioninheritingfrom abc.

    endclass.

    OUTPUT:

    Polymorphism: -

    PolyMany

    Morph Forms

    Ism behavior

    Examples: - Method overloadingMethod overriding

    Method overloading: -

    If a class contains two method with the same name but different signature it is called as

    method overloading.

    ABAP doesnt support method overloading.Z915AM_OOPS27:

    REPORT Z915AM_OOPS27.

    class abc definition.

    publicsection.

    * methods m1.

    methods m1 importingxtypei.

    endclass.

    CLASS ABC IMPLEMENTATION.

    METHOD M1.

  • 7/30/2019 Ooabap Notes With Programs

    42/93

    42 | P a g e S a n t o s h P

    ENDMETHOD.ENDCLASS.

    method overloading

    Z915AM_OOPS28:

    REPORT Z915AM_OOPS28.

    CLASS ABC DEFINITION.

    PUBLICSECTION.

    METHODS M1.

    ENDCLASS. "abc DEFINITION

    CLASS ABC IMPLEMENTATION.

    METHOD M1.WRITE :/ 'inside m1 of super class'.

    ENDMETHOD. "m1ENDCLASS. "abc IMPLEMENTATION

    CLASS PQRDEFINITIONINHERITINGFROM ABC.

    PUBLICSECTION.

    METHODS M1 REDEFINITION.

    ENDCLASS. "pqr DEFINITION

    CLASS PQRIMPLEMENTATION.

    METHOD M1.

    WRITE :/ 'inside m1 of sub class'.CALLMETHOD SUPER->M1.

    ENDMETHOD. "m1

    ENDCLASS. "pqr IMPLEMENTATION

    START-OF-SELECTION.

    DATA OB TYPEREFTO PQR.CREATE OBJECT OB.

    CALLMETHOD OB->m1.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    43/93

    43 | P a g e S a n t o s h P

    Method overriding: -

    If a sub class overwrites a super class method is called as method overriding.

    Whenever a sub class wants to override the super class method a sub class wants todeclare the super class method in the subclass by using REDEFINITION keyword.

    While redefined the methods we cannot change the visibility of the method. Whenever a subclass overrides the super class method it is always recommended to call

    the super class method version in the subclass by using super keyword.

    Super keyword is used for referring to super class components from the sub class. To redefine a global method put cursor on the method click on redefine.

    Z915AM_CLASS6:

    methodM1.

    write :/ 'inside method m1 of super class'.endmethod.

    Z915AM_CLASS7:

  • 7/30/2019 Ooabap Notes With Programs

    44/93

    44 | P a g e S a n t o s h P

    method M1.write :/ 'inside m1 of subclass'.

    CALLMETHOD SUPER->M1.

    endmethod.

    Z915AM_OOPS29:

    REPORT Z915AM_OOPS29.

    data ob typerefto z915am_class7.

    create object ob.

    callmethod ob->m1.

    OUTPUT:

    Final keyword: -

    Final keyword can be used at two levels.

    1) Class level2) Method levelThe class created as final cannot be inherited.

    A method created as final can be inherited but cannot redefine.Z915AM_OOPS30:

    REPORT Z915AM_OOPS30.

    CLASS ABC DEFINITION.

    PUBLICSECTION.

    METHODS M1 FINAL.

    ENDCLASS. "abc DEFINITION

  • 7/30/2019 Ooabap Notes With Programs

    45/93

    45 | P a g e S a n t o s h P

    CLASS ABC IMPLEMENTATION.METHOD M1.

    WRITE :/ 'inside m1 of super class'.ENDMETHOD. "m1

    ENDCLASS. "abc IMPLEMENTATION

    CLASS PQRDEFINITIONINHERITINGFROM ABC.

    PUBLICSECTION.

    METHODS M2.

    ENDCLASS. "pqr DEFINITION

    CLASS PQRIMPLEMENTATION.

    METHOD M2.

    WRITE :/ 'inside m2 of sub class'.

    CALLMETHOD M1.

    ENDMETHOD. "m2

    ENDCLASS. "pqr IMPLEMENTATION

    START-OF-SELECTION.DATA OB TYPEREFTO PQR.

    CREATE OBJECT OB.

    CALLMETHOD OB->M2.

    OUTPUT:

    Z915AM_OOPS31:

    REPORT Z915AM_OOPS31.

    class abc definition.

    publicsection.

    methods m1 final.

    endclass.

    class abc implementation.

    method m1.write :/ 'inside m1 of super class'.

    endmethod.

    endclass.

  • 7/30/2019 Ooabap Notes With Programs

    46/93

    46 | P a g e S a n t o s h P

    class pqrdefinitioninheritingfrom abc.

    publicsection.methods m2.

    methods m1 redefinition.

    endclass.

    class pqrimplementation.

    method m2.

    write :/ 'inside m2'.

    callmethod m1.

    endmethod.

    endclass.

    start-of-selection.

    data ob typerefto pqr.

    create object ob.

    callmethod ob->m2.

    OUTPUT:

    Z915AM_OOPS32:

    REPORT Z915AM_OOPS32.

    class abc definition.

    publicsection.

    methods constructor.

    class-methods class_constructor.

    endclass.

    class abc implementation.

    method constructor.

    write :/ 'inside instance const. of super class'.

    endmethod.

    method class_constructor.

    write :/ 'inside static const. of super class'.endmethod.

    endclass.

    class pqrdefinitioninheritingfrom abc.

    publicsection.

  • 7/30/2019 Ooabap Notes With Programs

    47/93

    47 | P a g e S a n t o s h P

    * methods constructor.class-methods class_constructor.

    endclass.

    class pqrimplementation.

    * method constructor.* write :/ 'inside instance const. of super class'.

    * endmethod.

    method class_constructor.

    write :/ 'inside static const. of sub class'.

    endmethod.

    endclass.

    start-of-selection.

    data ob1 typerefto pqr.

    create object ob1.

    OUTPUT:

    Hierarchy of constructor execution: -

    When a super class contains static and instance constructor and id sub class contains only the static

    constructor and if sub class contains only the static constructor in this case if we instantiate the subclass then the static constructor are executed from super class to sub class and then the instantiate

    constructor of the super class will be executed.

    Z915AM_OOPS33:

    REPORT Z915AM_OOPS33.

    class abc definition.

    publicsection.

    methods constructor.

    class-methods class_constructor.

    endclass.

    class abc implementation.

    method constructor.write :/ 'inside instance const. of super class'.

  • 7/30/2019 Ooabap Notes With Programs

    48/93

    48 | P a g e S a n t o s h P

    endmethod.

    method class_constructor.write :/ 'inside static const. of super class'.

    endmethod.

    endclass.

    class pqrdefinitioninheritingfrom abc.

    publicsection.

    methods constructor.

    class-methods class_constructor.

    endclass.

    class pqrimplementation.

    method constructor.

    write :/ 'inside instance const. of sub class'.

    callmethod super->constructor.endmethod.

    method class_constructor.

    write :/ 'inside static const. of sub class'.endmethod.

    endclass.

    start-of-selection.

    data ob1 typerefto pqr.

    create object ob1.

    OUTPUT:

    Note: - If the super class and sub class contains respective instance constructor it must for subclass

    instance constructor to call the super class instance constructor this is done by using super keyword.

    This is the only place where the constructor can be are must be called explicitly.

    Note: - If the super class and sub class contains respective static and instance constructor and if

    instantiate the sub class first the static constructor are executed to super class to subclass and whenthe instantiate constructor will executed subclass to super class.

  • 7/30/2019 Ooabap Notes With Programs

    49/93

    49 | P a g e S a n t o s h P

    Me keyword: - Me keyword refer to current object execution it is used to differentiate bothattribute and method parameters whenever attribute and parameter names are same.

    Z915AM_OOPS34:

    REPORT Z915AM_OOPS34.

    class abc definition.

    publicsection.

    methods m1 importingxtypei

    n typei

    exportingztypei.protectedsection.

    data : xtypei,y typei.

    endclass.

    class abc implementation.

    method m1.

    me->x = x.

    y = n.

    z = x + y.

    endmethod.

    endclass.

    start-of-selection.

    data ob typerefto abc.

    create object ob.

    data rtypei.

    callmethod ob->m1exporting

    x = 10n = 20

    importing

    z = r.write :/ 'sum is ',r.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    50/93

    50 | P a g e S a n t o s h P

    Visibility of component level: -

    1) Public section2) Protected section3) Private sectionVisibility of class level: -

    1) Public2) Protected3) Private4) AbstractPublic: - The default visibility of a class is public.

    Public classes can be instantiated. Public classes can be inherited. The sub classes inherited the public class is also created as public by default.

    Protected classes: - Protected classes can be inherited but cannot be instantiated outside the class

    but it can be instantiated within the sub class method.

    The sub class inheriting the protected class is also created as protected by default to createthe sub class as explicit public class we need to use extension create public as part of sub

    class definition.

    Z915AM_OOPS36:

    REPORT Z915AM_OOPS36.

    class abc definitioncreateprotected.

    endclass.

    *class pqr definition inheriting from abc.

    class pqrdefinitioncreatepublicinheritingfrom abc.

    publicsection.

    methods m1.endclass.

    class pqrimplementation.

    method m1.data ob typerefto abc.

    create object ob.endmethod.

    endclass.

    start-of-selection.

    data ktyperefto pqr.

    create object k.callmethod k->m1.

  • 7/30/2019 Ooabap Notes With Programs

    51/93

    51 | P a g e S a n t o s h P

    OUTPUT:

    protected classes.

    Private classes: -

    Private classes cannot be instantiated. Private classes can be inherited. The sub class inherited the private class is also created as private class by default. This sub class cannot be created as explicitly public class this can be mode possible if the

    super class considers the subclass as friend.

    Z915AM_OOPS37:

    REPORT Z915AM_OOPS37.

    class abc definitioncreateprivate.

    endclass.

    *class pqr definition inheriting from abc.class pqrdefinitioncreatepublic

    inheritingfrom abc.

    publicsection.

    methods m1.

    endclass.

    class pqrimplementation.

    method m1.data ob typerefto abc.

    create object ob.

    endmethod.

    endclass.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    52/93

    52 | P a g e S a n t o s h P

    Z915AM_OOPS38:

    REPORT Z915AM_OOPS38.

    class pqrdefinition deferred.

    class abc definitioncreateprivate

    friends pqr.

    endclass.

    class pqrdefinitioncreatepublicinheritingfrom abc.

    endclass.

    data ob typerefto pqr.

    create object ob.

    OUTPUT:

    second usage of friend keyword

    Abstract class: -

    It is a class which contains at least one abstract method. Abstract method is a methodwhich is just declared but not implemented in local class they are declared by using the

    keyword abstract.

    It a class contains at least one abstract method then the entity class should be declared asabstract.

    Abstract methods are always declared in public (or) protected section. We cannot instantiate the abstract classes because they are not fully implemented. The class which ever inheritance the abstract class can implement the abstract method of

    the abstract class otherwise the subclass will declared as abstract.

    Abstract methods are also called as non-concerted methods. We declared method as abstract when we are not sure about the implementation but we

    are sure that the other classes as to use the same methods.

    Z915AM_OOPS39:

    REPORT Z915AM_OOPS39.

    CLASS RESTAURANT DEFINITION ABSTRACT.

    PUBLICSECTION.

    METHODS : SET,

    DISPLAY,

    PAYMENT ABSTRACT.

    PROTECTEDSECTION.

    DATA : TABLENO TYPEI,

    STEWARD(20) TYPEC.

    ENDCLASS. "restaurant DEFINITION

  • 7/30/2019 Ooabap Notes With Programs

    53/93

    53 | P a g e S a n t o s h P

    CLASS RESTAURANT IMPLEMENTATION.METHODSET.

    TABLENO = 123.STEWARD = 'abc'.

    ENDMETHOD. "set

    METHOD DISPLAY.

    WRITE :/ TABLENO,STEWARD.

    ENDMETHOD. "display

    ENDCLASS. "restaurant IMPLEMENTATION

    CLASS CHEQUE DEFINITION

    INHERITINGFROM RESTAURANT.

    PUBLICSECTION.

    METHODS PAYMENT REDEFINITION.

    PROTECTEDSECTION.

    DATA : CQNO TYPEI,BANK(20) TYPEC,

    AMT TYPEI.ENDCLASS. "cheque DEFINITIO

    CLASS CHEQUE IMPLEMENTATION.

    METHOD PAYMENT.

    CQNO = 3444.

    BANK = 'xyz'.

    AMT = 333.

    WRITE :/ CQNO,BANK,AMT.

    ENDMETHOD. "payment

    ENDCLASS. "cheque IMPLEMENTATION

    CLASS CREDITCARD DEFINITION

    INHERITINGFROM RESTAURANT.

    PUBLICSECTION.

    METHODS PAYMENT REDEFINITION.

    PROTECTEDSECTION.

    DATA : CCNO TYPEI,

    AMOUNT TYPEI,

    BANKNAME(20) TYPEC.ENDCLASS. "creditcard DEFINITIO

    CLASS CREDITCARD IMPLEMENTATION.METHOD PAYMENT.

    CCNO = 455.

    AMOUNT = 432.

    BANKNAME = 'abc'.

    WRITE :/ CCNO,AMOUNT,BANKNAME.

    ENDMETHOD. "payment

    ENDCLASS. "creditcard IMPLEMENTATION

    START-OF-SELECTION.

  • 7/30/2019 Ooabap Notes With Programs

    54/93

    54 | P a g e S a n t o s h P

    DATA RTYPEREFTO RESTAURANT.

    DATA CQ TYPEREFTO CHEQUE.CREATE OBJECT CQ.

    WRITE :/ 'CHEQUE class'.

    CALLMETHOD : CQ->SET,CQ->DISPLAY,

    CQ->PAYMENT.

    DATA CC TYPEREFTO CREDITCARD.

    CREATE OBJECT CC.

    WRITE :/ 'CREDIT CARD class'.

    CALLMETHOD : CC->SET,

    CC->DISPLAY,

    CC->PAYMENT.

    WRITE :/ 'CHEQUE ----> RESTAURANT'.R = CQ.

    CALLMETHOD : R->SET,R->DISPLAY,

    R->PAYMENT.

    WRITE :/ 'CREDITCARD ----> RESTAURANT'.

    R = CC.

    CALLMETHOD : R->SET,

    R->DISPLAY,

    R->PAYMENT.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    55/93

    55 | P a g e S a n t o s h P

    Z915AM_REST:

    methodSET.

    tableno = 1.

    steward = 'abc'.

    endmethod.

    method DISPLAY.write :/ tableno,steward.

    endmethod.

    Z915AM_CHEQUE:

  • 7/30/2019 Ooabap Notes With Programs

    56/93

    56 | P a g e S a n t o s h P

    method PAYMENT.cqno = 123.

    bank = 'yes'.amount = 455.

    write :/ cqno,bank,amount.

    endmethod.

    Z915AM_OOPS40:

    REPORT Z915AM_OOPS40.

    data ob typerefto z915am_cheque.

    create object ob.

    CALLMETHOD OB->SET.CALLMETHOD OB->DISPLAY.

    CALLMETHOD OB->PAYMENT.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    57/93

    57 | P a g e S a n t o s h P

    Interfaces: - It is pure abstract class i.e. by default all methods of interface are abstract. By usinginterfaces we can implement multiple inheritances.

    By default the visibility of the interface components are public. Interface methods contain only declaration but not implementation the implementation

    must be provided in the corresponding class. The class whichever implements theinterface is called as implementation class and this class should implement all the

    methods of the interface otherwise this class should be declaration as abstract.

    A local class whichever wants to implement the interface must declared the interface inthe class definition by using interface keyword.

    Syntax: - interface .

    A class can implement any number of interfaces which is nothing but multiple inheritance

    whenever the interface component are referred the outside of the interface they must be prefixed

    with the name of the interface.

    Interface is always implemented in public section. We cannot the instantiate the interfaces because it is not implemented.

    Syntax for local interfaces: -

    Interface .

    Declaration of components

    End interface.

    Z915AM_OOPS41:

    REPORT Z915AM_OOPS41.

    INTERFACE RECTANGLE.

    CONSTANTS : LENGTH TYPEIVALUE10,BREADTH TYPEIVALUE5.

    METHODS : AREA,PERIMETER.

    ENDINTERFACE. "rectangle

    INTERFACE SQUARE.

    CONSTANTS SIDE TYPEIVALUE5.METHODS : AREA,

    PERIMETER.ENDINTERFACE. "square

    CLASS ABC DEFINITION.

    PUBLICSECTION.

    DATA RES TYPEI.

    INTERFACES RECTANGLE.

    INTERFACES SQUARE.

    ENDCLASS. "abc DEFINITION

  • 7/30/2019 Ooabap Notes With Programs

    58/93

    58 | P a g e S a n t o s h P

    CLASS ABC IMPLEMENTATION.METHOD RECTANGLE~AREA.

    RES = RECTANGLE~LENGTH * RECTANGLE~BREADTH.WRITE :/ 'Area of rectangle is :',RES.

    ENDMETHOD. "rectangle~area

    METHOD RECTANGLE~PERIMETER.

    RES = 2 * ( RECTANGLE~LENGTH + RECTANGLE~BREADTH ).

    WRITE :/ 'perimeter of rectangle is :',RES.

    ENDMETHOD. "rectangle~perimeter

    METHOD SQUARE~AREA.

    RES = SQUARE~SIDE * SQUARE~SIDE.

    WRITE :/ 'Area of square is :',RES.

    ENDMETHOD. "square~area

    METHOD SQUARE~PERIMETER.RES = 4 * SQUARE~SIDE.

    WRITE :/ 'perimeter of square is :',RES.ENDMETHOD. "square~perimeter

    ENDCLASS. "abc IMPLEMENTATION

    START-OF-SELECTION.

    DATA RTYPEREFTO RECTANGLE.

    DATA S TYPEREFTO SQUARE.

    DATA KTYPEREFTO ABC.

    CREATE OBJECT K.

    CALLMETHOD : K->RECTANGLE~AREA,

    K->RECTANGLE~PERIMETER,

    K->SQUARE~AREA,

    K->SQUARE~PERIMETER.

    WRITE :/ 'RECTANGLE ---> ABC'.

    R = K.

    CALLMETHOD : R->AREA,R->PERIMETER.

    WRITE :/ 'SQUARE ---> ABC'.S = K.

    CALLMETHOD : S->AREA,

    S->PERIMETER.

  • 7/30/2019 Ooabap Notes With Programs

    59/93

    59 | P a g e S a n t o s h P

    OUTPUT:

    Aliases: - aliases are the alternative names provided to the interface components i.e. whenever the

    interface components is referred outside the interface declaration it must be prefixed with the name

    of interface we can avoid the lengthy naming standard by declaring the aliases by the interface

    components these aliases must be declared in the definition of a class whichever the implementing

    the interface. By using aliases we can also change the visibility of the interface components.

    ABSTRACT CLASSES INTERFACES

    Can contain both abstract and non-abstract

    methods.

    Can contain only abstract methods.

    Explicitly we need to use abstract keyword. By default all methods are abstract.

    Abstract methods can be declared in public or

    protected section.

    All components of interface by default are

    public.

    A class can inherit only one abstract class. A class can implement any know of interfaces.

    Abstract class components are directly referred

    in subclass.

    Interface components must be prefixed with the

    name of the interface.

  • 7/30/2019 Ooabap Notes With Programs

    60/93

    60 | P a g e S a n t o s h P

    Z915AM_OOPS42:

    REPORT Z915AM_OOPS42.

    INTERFACE ABC.

    METHODS : M1,

    M2.

    ENDINTERFACE. "abc

    CLASS PQRDEFINITION.PUBLICSECTION.

    INTERFACES ABC.ALIASES : A1 FORABC~M1.

    PROTECTEDSECTION.

    ALIASES A2 FORABC~M2.ENDCLASS. "pqr DEFINITION

    CLASS PQRIMPLEMENTATION.

    METHOD A1.

    WRITE :/ 'inside m1'.

    CALLMETHOD A2.

    ENDMETHOD. "a1

    METHOD A2.WRITE :/ 'inside m2'.

    ENDMETHOD. "a2

    ENDCLASS. "pqr IMPLEMENTATION

    START-OF-SELECTION.DATA KTYPEREFTO PQR.

    CREATE OBJECT K.

    CALLMETHOD : K->A1.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    61/93

    61 | P a g e S a n t o s h P

    Z915AM_INT1:

    Z915AM_IMPL:

    methodZ915AM_INT1~M1.

    write :/ 'inside m1'.

    callmethod a2.

    endmethod.

    method Z915AM_INT1~M2.

    write :/ 'inside m2'.

    endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    62/93

    62 | P a g e S a n t o s h P

    Z915AM_OOPS43:

    REPORT Z915AM_OOPS43.

    data ob typerefto z915am_impl.create object ob.

    callmethod ob->a1.

    OUTPUT:

    Z915AM_OOPS44:

    REPORT Z915AM_OOPS44.

    INTERFACE ABC.METHODS : M1,

    M2,

    M3.

    ENDINTERFACE. "abc

    CLASS PQRDEFINITION ABSTRACT.

    PUBLICSECTION.

    INTERFACES ABC ABSTRACT METHODS M2 M3.

    ENDCLASS. "pqr DEFINITION

  • 7/30/2019 Ooabap Notes With Programs

    63/93

    63 | P a g e S a n t o s h P

    CLASS PQRIMPLEMENTATION.METHOD ABC~M1.

    WRITE :/ 'inside m1'.ENDMETHOD. "abc~m1

    ENDCLASS. "pqr IMPLEMENTATION

    CLASS XYZ DEFINITIONINHERITINGFROM PQR.

    PUBLICSECTION.

    METHODS : ABC~M2 REDEFINITION,

    ABC~M3 REDEFINITION.

    ENDCLASS. "xyz DEFINITION

    CLASS XYZ IMPLEMENTATION.

    METHOD ABC~M2.

    WRITE :/ 'inside m2'.

    ENDMETHOD. "abc~m2

    METHOD ABC~M3.

    WRITE :/ 'inside m3'.ENDMETHOD. "abc~m3

    ENDCLASS. "xyz IMPLEMENTATION

    START-OF-SELECTION.

    DATA KTYPEREFTO XYZ.

    CREATE OBJECT K.

    CALLMETHOD : K->ABC~M1,

    K->ABC~M2,

    K->ABC~M3.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    64/93

    64 | P a g e S a n t o s h P

    Z915AM_OOPS45:

    REPORT Z915AM_OOPS45.

    interface pqr.

    methods : m1,m3.

    endinterface.

    class abc definition abstract.

    publicsection.

    methods : m1 abstract,m2.

    endclass.

    class abc implementation.

    method m2.write :/ 'inside m2'.

    endmethod.

    endclass.

    class xyz definitioninheritingfrom abc.

    publicsection.

    interfaces pqr.

    methods m1 redefinition.

    endclass.

    class xyz implementation.method pqr~m1.

    write :/ 'inside m1 of pqr'.

    endmethod.

    method m1.

    write :/ 'inside m1'.endmethod.

    method pqr~m3.write :/ 'inside m3'.

    endmethod.endclass.

    start-of-selection.

    data ob typerefto xyz.create object ob.

    callmethod : ob->m1,

    ob->pqr~m1,

    ob->pqr~m3,

    ob->m2.

  • 7/30/2019 Ooabap Notes With Programs

    65/93

    65 | P a g e S a n t o s h P

    OUTPUT:

    Persistence service: - It is used for storing the state of an object formality it is similarly to

    serialization java and .net. This service is implemented by using persistence classes. This service isimplemented in two ways.

    1) By using business key identity.2) By using GUID (global unique identifier)

    Storing the state of object permanently in the database is called as persistence. By default the lifetime of the scope of an object is within the program where it is created. Persistence class is always global and the naming standard is ZCL_ (or) YCL. Persistence class is always created as protected class. Whenever a persistence class is created SAP automatically create to class.

    1) Base agent class naming standard is ZCB_ (or) YCB_.2) Agent class or actor class ZCA_ (or) YCA_.

    Base agent class is always created as abstract the class and it is the friend of persistenceclass.

    Actor class is always created as private class and it is a sub class of base agent class. Once the persistence class is created it needs to mapped with the corresponding database

    table.

    Persistence class using business key identity: -

    In this we consider the primary key fields of the database table as business key identitywhich is used for identity the object uniquely.

    In this case when the persistence class is mapped with the database tables SAP adds thefields of the database as the attributes of the persistence class.

    Also it creates the following methods as part of the base agent class.1) Create_persistence.2) Delete_persistence.3) Get_persistence.

    The above three method are public instance methods which gets inherited to actor class.We need to use the above methods to interact with the persistence service.

    A part from this SAP also generates getter and setter methods as part of persistence class. Getter method is generated for all the fields of the database and setter methods aregenerated for non-primary key fields of the table.

  • 7/30/2019 Ooabap Notes With Programs

    66/93

    66 | P a g e S a n t o s h P

    To access the above three methods we require the object of base agent class. But the baseagent class is always created as abstract class and therefore cannot be instated.

    Since the above three method are inherited to actor class we need to instantiate the actorclass and access these methods. But actor class is created as private class and therefore

    cannot be instated.

    We use the following mechanize to access these methods. Actor class is created as singleton class. As part of the actor class SAP as provided a public static attribute agent. We need to access this public static attribute agent using the actor class name. When

    accessed internal it execute the static constructor of actor class it is reasonable for

    creating the object. This object is return back using which we access the above three

    methods.

    Singleton class: -

    Creating a class in such way so that we can create exactly one object is called as

    singleton.

    Z915AM_OOPS47:

    REPORT Z915AM_OOPS47.

    class abc definitioncreateprivate.

    publicsection.

    class-methods class_constructor.

    class-methods m1 returning value(m)

    typerefto abc.

    methods m2.

    protectedsection.class-data ktyperefto abc.

    endclass.

    class abc implementation.method class_constructor.

    write :/ 'inside static constructor,about to create object'.create object k.

    endmethod.

    method m1.

    write :/ 'inside static method m1,about to return object'.m = k.

    endmethod.

    method m2.write :/ 'inside instance method m2'.

    endmethod.

    endclass.

    start-of-selection.

    data rtyperefto abc.

  • 7/30/2019 Ooabap Notes With Programs

    67/93

    67 | P a g e S a n t o s h P

    *r = abc=>m1( ).callmethod abc=>m1

    receivingm = r.

    callmethod r->m2.

    OUTPUT:

    Z915AM_OOPS48:

    REPORT Z915AM_OOPS48.

    PARAMETERS : P_DEPTNO TYPE Z730CDEPT-DEPTNO,

    P_DNAME TYPE Z730CDEPT-DNAME,

    P_LOC TYPE Z730CDEPT-LOC.

    PARAMETERS : R1 RADIOBUTTONGROUP G1,

    R2 RADIOBUTTONGROUP G1,R3 RADIOBUTTONGROUP G1.

    DATA ACTORTYPEREFTO ZCA_915DEPT.

    DATA PERS TYPEREFTO ZCL_915DEPT.

    START-OF-SELECTION.

    ACTOR = ZCA_915DEPT=>AGENT.

    IF R1 = 'X'.

    TRY.

    CALLMETHOD ACTOR->CREATE_PERSISTENT

    EXPORTING

    I_DEPTNO = P_DEPTNO

    I_DNAME = P_DNAME

    I_LOC = P_LOC

    RECEIVING

    RESULT = PERS.

    IF PERS ISNOTINITIAL.

  • 7/30/2019 Ooabap Notes With Programs

    68/93

    68 | P a g e S a n t o s h P

    COMMITWORK.ENDIF.

    CATCH CX_OS_OBJECT_EXISTING .WRITE :/ 'Exception raised while creating'.

    ENDTRY.

    ELSEIF R2 = 'X'.TRY.

    CALLMETHOD ACTOR->DELETE_PERSISTENT

    EXPORTING

    I_DEPTNO = P_DEPTNO.

    COMMITWORK.

    CATCH CX_OS_OBJECT_NOT_EXISTING .

    WRITE :/ 'Object not found'.

    ENDTRY.

    ELSEIF R3 = 'X'.

    TRY.

    CLEARPERS.CALLMETHOD ACTOR->GET_PERSISTENT

    EXPORTINGI_DEPTNO = P_DEPTNO

    RECEIVINGRESULT = PERS.

    IF PERS ISNOTINITIAL.

    CLEAR: P_DNAME,

    P_LOC.

    TRY.

    CALLMETHOD PERS->GET_DNAME

    RECEIVING

    RESULT = P_DNAME.

    CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Exception in getter of dname'.

    ENDTRY.

    TRY.

    CALLMETHOD PERS->GET_LOC

    RECEIVING

    RESULT = P_LOC.

    CATCH CX_OS_OBJECT_NOT_FOUND .WRITE :/ 'Exception in getter of loc'.ENDTRY.

    WRITE :/ 'Department name :',P_DNAME,

    / 'Department location :',P_LOC.

    ENDIF.

    CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Object not found'.

    ENDTRY.

    ENDIF.

  • 7/30/2019 Ooabap Notes With Programs

    69/93

    69 | P a g e S a n t o s h P

    OUTPUT:

    Persistence service using GUID: - In this we need to consider database table which contains

    GUID as the first field. The data element of this field can be GUID/OS-GUID.

    The data type of this field raw data type.

    This field is used for unique identification of the object. The value for this field is generated dynamically by SAP.

    Note: - when a persistence class is mapped with the database table containing GUID also the field

    expects the GUID are added as attribute of class and also getter and setter method are generated for

    the entire field except GUID.

    Z915AM_OOPS49:

    REPORT Z915AM_OOPS49.

    PARAMETERS : P_DEPTNO TYPE Z915DEPT-DEPTNO,P_DNAME TYPE Z915DEPT-DNAME,

    P_LOC TYPE Z915DEPT-LOC,

    P_GUID TYPE Z915DEPT-GUID.

    PARAMETERS : R1 RADIOBUTTONGROUP G1,

    R2 RADIOBUTTONGROUP G1,

    R3 RADIOBUTTONGROUP G1.

    DATA ACTORTYPEREFTO YCA_915DEPT.

    DATA PERS TYPEREFTO YCL_915DEPT.DATA OB TYPEREFTO OBJECT.

    START-OF-SELECTION.ACTOR = YCA_915DEPT=>AGENT.

    IF R1 = 'X'.

    TRY.

    CALLMETHOD ACTOR->CREATE_PERSISTENT

    EXPORTING

    I_DEPTNO = P_DEPTNO

    I_DNAME = P_DNAME

  • 7/30/2019 Ooabap Notes With Programs

    70/93

    70 | P a g e S a n t o s h P

    I_LOC = P_LOCRECEIVING

    RESULT = PERS.IF PERS ISNOTINITIAL.

    COMMITWORK.

    ENDIF.CATCH CX_OS_OBJECT_EXISTING .

    WRITE :/ 'Exception raised'.

    ENDTRY.

    ELSEIF R2 = 'X'.

    TRY.

    CALLMETHOD ACTOR->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID

    EXPORTING

    I_OID = P_GUID

    RECEIVING

    RESULT = OB.

    PERS ?= OB.IF PERS ISNOTINITIAL.

    TRY.CALLMETHOD ACTOR->IF_OS_FACTORY~DELETE_PERSISTENT

    EXPORTING

    I_OBJECT = PERS.

    COMMITWORK.

    CATCH CX_OS_OBJECT_NOT_EXISTING .

    WRITE :/ 'Object not found'.

    ENDTRY.

    ENDIF.

    CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Object not found'.

    CATCH CX_OS_CLASS_NOT_FOUND .

    WRITE :/ 'class not found'.

    ENDTRY.

    ELSEIF R3 = 'X'.

    TRY.

    CALLMETHOD ACTOR->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OIDEXPORTINGI_OID = P_GUID

    RECEIVINGRESULT = OB.

    PERS ?= OB.

    IF PERS ISNOTINITIAL.

    CLEAR: P_DEPTNO,

    P_DNAME,

    P_LOC.

    TRY.

    CALLMETHOD PERS->GET_DEPTNORECEIVING

  • 7/30/2019 Ooabap Notes With Programs

    71/93

    71 | P a g e S a n t o s h P

    RESULT = P_DEPTNO.CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Exception in getdeptno'.ENDTRY.

    TRY.

    CALLMETHOD PERS->GET_DNAMERECEIVING

    RESULT = P_DNAME.

    CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Exception in getdname'.

    ENDTRY.

    TRY.

    CALLMETHOD PERS->GET_LOC

    RECEIVING

    RESULT = P_LOC.

    CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Exception in getloc'.ENDTRY.

    WRITE :/ 'Department no :',P_DEPTNO,/ 'Department name :',P_DNAME,

    / 'Department loc :',P_LOC.ENDIF.

    CATCH CX_OS_OBJECT_NOT_FOUND .

    WRITE :/ 'Object not found'.

    CATCH CX_OS_CLASS_NOT_FOUND .

    WRITE :/ 'class not found'.

    ENDTRY.

    ENDIF.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    72/93

    72 | P a g e S a n t o s h P

    Z915_TRANS:

    methodM1.

    data : trans_mng typereftoif_os_transaction_manager,

    trans typereftoif_os_transaction.

    CALLMETHOD CL_OS_SYSTEM=>GET_TRANSACTION_MANAGER

    RECEIVING

    RESULT = trans_mng.

    TRY.

    CALLMETHOD TRANS_MNG->CREATE_TRANSACTION

    RECEIVING

    RESULT = trans.ENDTRY.

    iftrans isnotinitial.

    TRY.

    CALLMETHOD TRANS->START.

    callmethod m2.

    TRY.

    CALLMETHOD TRANS->END.

    CATCH CX_OS_CHECK_AGENT_FAILED .CATCH CX_OS_TRANSACTION .

    write :/ 'Exception in end'.TRY.

    CALLMETHOD TRANS->UNDO.ENDTRY.

    ENDTRY.

    CATCH CX_OS_TRANSACTION .

    message'Exception in transaction'type'I'.

    ENDTRY.

    endif.

    endmethod.

    method M2.data : actortyperefto zca_915dept,

  • 7/30/2019 Ooabap Notes With Programs

    73/93

    73 | P a g e S a n t o s h P

    pers typerefto zcl_915dept.

    actor = zca_915dept=>agent.ifactorisnotinitial.

    TRY.

    CALLMETHOD ACTOR->CREATE_PERSISTENTEXPORTING

    I_DEPTNO = '77'

    I_DNAME = 'ABAP'

    I_LOC = 'Ameerpet'

    RECEIVING

    RESULT = pers.

    CATCH CX_OS_OBJECT_EXISTING .

    write :/ 'Exception in create persistent'.

    ENDTRY.

    endif.

    endmethod.

    Transaction service: - It is use for managing the object oriented transaction involving database

    operations. As part of this we need to use the following class and interfaces.

    1) CL_OS_SYSTEM CLASS2) IF_OS_TRANSCATION_MANGAER INTERFACE3) IT_OS_TRANSACTION INTERFACEProcedure for interacting with transaction service: -

    1) Start the object oriented transaction by calling the start method of the interface.IT_OS_TRANSACTION

    Start method is an instance method of interface IT_OS_TRANSACTION so we need to instantiate.

    The interface IF_OS_TRANSACTION which cannot be done directly. So we need to access theinstance method Create_Transaction of the interface IF_OS_TRANSACTION_MANAGER to

    access this method we required the object of transaction manger interface. To get these object of

    transaction manger interface. To get this object we need to access the static methodGET_TRANSCATION_MANAGER of the class CL_OS_SYSTEM.

    2) Perform the required operation: - End the transaction by calling the end method of the interface IF_OS_TRASACTION. When the transaction is successfully completed SAP issues commit work statement

    internally for saving the transaction permanently.

    If the transaction fails SAP raise the exception as part of this exception handling we needto cancel the transaction by calling the undo method of the interface

    IF_OS_TRANSCATION.

  • 7/30/2019 Ooabap Notes With Programs

    74/93

    74 | P a g e S a n t o s h P

    Z915AM_OOPS50:

    REPORT Z915AM_OOPS50.

    data ktyperefto z915_trans.

    create object k.

    CALLMETHOD K->M1.

    OUTPUT:

    IT IS CONSIDERING IT AS TWO DIFFERENT PROGRAMS THATS WHY IT IS NOTEXECUTING.

    ZTR10:(TRANSACTION CODE)

    Implementing persistence service using transaction service: -

    Note: -

    Transaction service is always implemented globally.

    Since transaction service is implemented in global classes we need to attach a T-code for a

    transaction class method so that everything will be executed as single process. If we access the

    transaction class method form local program it executed the different process which are assuming

    as two different translations.

  • 7/30/2019 Ooabap Notes With Programs

    75/93

    75 | P a g e S a n t o s h P

    Casting: - It is the process of converting a variable from one data type to another data types theyare two types.

    1) Wide casting2) Narrow castingWide casting: - It is the process of converting an object from a less detailed view to more detailed

    view.

    Narrow casting: - It is a process of converting an object from a more details view to be less

    detailed view.

    Procedure for deleting the persistent object using GUID: -

    Check the existent persistent object using the method gets persistent OID. If thepersistent object is

    available it returns the object of object class which needs to be type casted to the corresponding

    persistent class object. Pass the persistent class object as an input to the method.DELETE_PERSISTENT.

    Event handling in object oriented: - As part of ABAP objects SAP as provided many events as

    part of standard classes. These events are used in ALV reporting work flow customization CRMtechnical DSP and webdynpro programming.

    As part of custom classes we can declare user defined events. These event are declared are raised

    and handle by the developer itself.

    Procedure for interactive with user defined events in local class: -

    Declare the event in the definition of the class. Declare the event handler method in the definition of the class. Implemented the event handler method in the class implementation. Raise the event in one of method implementation. Register the handlers.

    Step1 Syntax: -

    Events/class-events [exporting parameters list].

    Step2 Syntax: -

    Methods/class-methods for event of [importing

    parameter list].

    Step3 Syntax: -

    Raise event [exporting parameter list].

  • 7/30/2019 Ooabap Notes With Programs

    76/93

    76 | P a g e S a n t o s h P

    Step4 Syntax: -

    Set handler [for ].They are two types of events

    1) Instant2) Static Instance events are declared by using keyword events. Static events are declared by using keyword class-events. Instance event is specific to an object. They are static event is not specific to an object. For every event there can be one are more event handler methods within the class are

    across the classes.

    These event handler methods are executed automatically whenever the event is raised aretriggered.

    For executing the event handler methods we need to register the handlers. By using thisregister handlers SAP will execute all the event handler methods. One after the other.

    Accordingly to sequence of register.

    Events can contains only exporting parameters which are imported by event handlermethod these parameters are always passed by values. The parameter name in the eventas well as in event handler method must be same.

    Note: - if the handler is not register events can be triggered but no actions can be performed

    because the event handler methods will not be executed.

    Z915AM_OOPS52:

    REPORT Z915AM_OOPS52.

    CLASS ABC DEFINITION.

    PUBLICSECTION.EVENTS E1. "instance event

    METHODS : M1 FOREVENT E1 OF ABC, "instance event handler methodM2. "instance normal method

    ENDCLASS. "abc DEFINITION

    CLASS ABC IMPLEMENTATION.

    METHOD M1.WRITE :/ 'inside event handler method m1'.

    ENDMETHOD. "m1METHOD M2.

    WRITE :/ 'Inside m2,About to raise event'.RAISEEVENT E1.

    ENDMETHOD. "m2

    ENDCLASS. "abc IMPLEMENTATION

    START-OF-SELECTION.DATA OB TYPEREFTO ABC.

  • 7/30/2019 Ooabap Notes With Programs

    77/93

    77 | P a g e S a n t o s h P

    CREATE OBJECT OB.

    CALLMETHOD OB->M2.

    OUTPUT:

    Z915AM_OOPS53:

    REPORT Z915AM_OOPS53.

    class abc definition.

    publicsection.

    events e1. "instance event

    methods : m1 forevent e1 ofabc, "instance event handler method

    m2. "instance normal methodendclass.

    class abc implementation.method m1.

    write :/ 'inside event handler method m1'.

    endmethod.

    method m2.

    write :/ 'Inside m2,About to raise event'.

    raiseevent e1.

    endmethod.

    endclass.

    start-of-selection.data ob typerefto abc.

    create object ob.

    callmethod ob->m2.

    sethandlerob->m1 forob.

  • 7/30/2019 Ooabap Notes With Programs

    78/93

    78 | P a g e S a n t o s h P

    OUTPUT:

    Z915AM_OOPS54:

    REPORT Z915AM_OOPS54.

    class abc definition.

    publicsection.

    events e1. "instance eventmethods : m1 forevent e1 ofabc, "instance event handler method

    m2. "instance normal method

    endclass.

    class abc implementation.

    method m1.

    write :/ 'inside event handler method m1'.

    endmethod.

    method m2.write :/ 'Inside m2,About to raise event'.

    raiseevent e1.endmethod.

    endclass.start-of-selection.

    data ob typerefto abc.create object ob.

    sethandlerob->m1 forob.

    callmethod ob->m2.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    79/93

    79 | P a g e S a n t o s h P

    Z915AM_OOPS55:

    REPORT Z915AM_OOPS55.

    class abc definition.

    publicsection.events e1.

    methods : m1 forevent e1 ofabc,

    m2 forevent e1 ofabc,

    m3.

    endclass.

    class abc implementation.

    method m2.write :/ 'inside second event handler method m2'.

    endmethod.

    method m1.

    write :/ 'inside first event handler method m1'.

    endmethod.

    method m3.

    write :/ 'inside m3,about to raise event'.

    raiseevent e1.

    endmethod.

    endclass.

    start-of-selection.data ob typerefto abc.

    create object ob.

    sethandlerob->m2 forob.sethandlerob->m1 forob.

    callmethod ob->m3.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    80/93

    80 | P a g e S a n t o s h P

    Z915AM_OOPS56:

    REPORT Z915AM_OOPS56.

    class abc definition.

    publicsection.

    events e1.

    methods : m1 forevent e1 ofabc,

    m2 forevent e1 ofabc,

    m3.endclass.

    class abc implementation.

    method m1.write :/ 'inside first event handler method m1'.

    endmethod.

    method m2.

    write :/ 'inside second event handler method m2'.

    endmethod.

    method m3.

    write :/ 'inside m3,about to raise event'.

    raiseevent e1.

    endmethod.

    endclass.

    start-of-selection.

    data ob typerefto abc.

    create object ob.

    data ob1 typerefto abc.create object ob1.

    sethandlerob->m2 forob.

    sethandlerob->m1 forob.

    callmethod ob1->m3.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    81/93

    81 | P a g e S a n t o s h P

    Z915AM_OOPS57:

    REPORT Z915AM_OOPS57.

    class abc definition.

    publicsection.events e1.

    methods : m1 forevent e1 ofabc,

    m2 forevent e1 ofabc,

    m3.

    endclass.

    class abc implementation.

    method m1.write :/ 'inside first event handler method m1'.

    endmethod.

    method m2.

    write :/ 'inside second event handler method m2'.

    endmethod.

    method m3.

    write :/ 'inside m3,about to raise event'.

    raiseevent e1.

    endmethod.

    endclass.

    start-of-selection.data ob typerefto abc.

    create object ob.

    data ob1 typerefto abc.create object ob1.

    sethandlerob->m2 forob1.

    sethandlerob->m1 forob1.

    callmethod ob1->m3.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    82/93

    82 | P a g e S a n t o s h P

    Z915AM_OOPS58:

    REPORT Z915AM_OOPS58.

    class abc definition.

    publicsection.events e1.

    methods : m1 forevent e1 ofabc,

    m2.

    endclass.

    class abc implementation.method m1.

    write :/ 'inside m1, first event handler'.endmethod.

    method m2.write :/ 'inside m2, about to raise event'.

    raiseevent e1.

    endmethod.

    endclass.

    class pqrdefinition.

    publicsection.

    methods m3 forevent e1 ofabc.

    endclass.

    class pqrimplementation.

    method m3.write :/ 'inside m3, second event handler'.

    endmethod.

    endclass.

    start-of-selection.

    data ob1 typerefto abc.create object ob1.

    data ob2 typerefto pqr.create object ob2.

    sethandlerob1->m1 forob1.

    sethandlerob2->m3 forob1.

    callmethod ob1->m2.

  • 7/30/2019 Ooabap Notes With Programs

    83/93

    83 | P a g e S a n t o s h P

    OUTPUT:

    For all instance: - while register the handler for register events as part of set handler statement we

    need to specify the object name after for keyword. This is reasonable for raising the event. This as

    to be done for every object separately which is raising the event instead of this we can use FORALL INSTANCEkeyword.

    As part of handler such that the event handler method will executed irrespective of objectused for raising the event.

    Z915AM_OOPS59:

    REPORT Z915AM_OOPS59.

    class abc definition.

    publicsection.

    events e1.

    methods : m1 forevent e1 ofabc,

    m2.

    endclass.

    class abc implementation.

    method m1.

    write :/ 'inside event handler m1'.

    endmethod.

    method m2.

    write :/ 'inside m2, about to raise event'.raiseevent e1.

    endmethod.endclass.

    start-of-selection.

    data ob1 typerefto abc.

    create object ob1.

    data ob2 typerefto abc.

    create object ob2.*set handler ob1->m1 for ob1.

    *set handler ob1->m1 for ob2.

  • 7/30/2019 Ooabap Notes With Programs

    84/93

    84 | P a g e S a n t o s h P

    sethandlerob1->m1 forall instances.

    callmethod ob1->m2.callmethod ob2->m2.

    OUTPUT:

    Static event: - While registering the handlers for static even we should not specify the object which

    is reasonable raising the static event. Because static event is not specific to on object i.e. for

    keyword is not allowed for the static event part of set handler this similarly for all instances in case

    of instance event.

    Z915AM_OOPS60:

    REPORT Z915AM_OOPS60.

    class abc definition.publicsection.

    class-events e1.methods : m1 forevent e1 ofabc,

    m2.

    endclass.

    class abc implementation.

    method m1.

    write :/ 'inside m1 event handler'.

    endmethod.

    method m2.

    write :/ 'inside m2 about to raise static event'.

    raiseevent e1.

    endmethod.

    endclass.

    start-of-selection.data ob1 typerefto abc.

    create object ob1.

    data ob2 typerefto abc.

  • 7/30/2019 Ooabap Notes With Programs

    85/93

    85 | P a g e S a n t o s h P

    create object ob2.

    sethandlerob1->m1.

    callmethod ob1->m2.

    callmethod ob2->m2.OUTPUT:

    Static event handler method: -

    Instance event can be raised only in instance methods. Static event can be raised either in instance are static method.

    Z915AM_OOPS61:

    REPORT Z915AM_OOPS61.

    class abc definition.publicsection.

    class-events e1.

    class-methods m1 forevent e1 ofabc.

    methods m2.

    endclass.

    class abc implementation.

    method m1.write :/ 'inside m1, static event handler'.

    endmethod.

    method m2.write :/ 'inside m2'.

    raiseevent e1.

    endmethod.endclass.

    start-of-selection.

    data ob typerefto abc.create object ob.

    sethandlerABC=>m1.

    CALLMETHOD OB->m2.

  • 7/30/2019 Ooabap Notes With Programs

    86/93

    86 | P a g e S a n t o s h P

    OUTPUT:

    Z915AM_OOPS62:

    REPORT Z915AM_OOPS62.

    class abc definition.publicsection.

    events e1.

    class-methods m1 forevent e1 ofabc.

    methods m2.

    endclass.

    class abc implementation.

    method m1.write :/ 'inside m1, static event handler'.

    endmethod.

    method m2.write :/ 'inside m2'.

    raiseevent e1.

    endmethod.

    endclass.

    start-of-selection.

    data ob typerefto abc.

    create object ob.

    sethandlerabc=>m1 forob.callmethod ob->m2.

  • 7/30/2019 Ooabap Notes With Programs

    87/93

    87 | P a g e S a n t o s h P

    OUTPUT:

    Z915AM_OOPS63:

    REPORT Z915AM_OOPS63.

    CLASS ABC DEFINITION.

    PUBLICSECTION.

    EVENTS E1 EXPORTINGVALUE(X) TYPEIOPTIONAL.

    METHODS : M1 FOREVENT E1 OF ABC

    IMPORTINGX,

    M2 IMPORTING Y TYPEI.

    ENDCLASS. "abc DEFINITION

    CLASS ABC IMPLEMENTATION.METHOD M1.

    WRITE :/ 'inside event handler m1'.WRITE :/ 'parameter received is ',X.

    ENDMETHOD. "m1

    METHOD M2.WRITE :/ 'inside m2,about to raise event'.

    RAISEEVENT E1

    EXPORTING

    X = Y.

    ENDMETHOD. "m2ENDCLASS. "abc IMPLEMENTATION

    START-OF-SELECTION.

    DATA OB TYPEREFTO ABC.

    CREATE OBJECT OB.

    PARAMETERS RTYPEI.

    SETHANDLEROB->M1 FOROB.CALLMETHOD OB->M2

    EXPORTINGY = R.

  • 7/30/2019 Ooabap Notes With Programs

    88/93

    88 | P a g e S a n t o s h P

    OUTPUT:

    Z915AM_CLASS11:

    methodM1.

    WRITE :/ 'INSIDE EVENT HANDLER'.

    WRITE :/ 'PARAMETER RECEIVED IS ',P1.

    endmethod.

    method M2.

    WRITE :/ 'INSIDE M2'.

    RAISEEVENT E1

    EXPORTINGP1 = 10.

    endmethod.

  • 7/30/2019 Ooabap Notes With Programs

    89/93

    89 | P a g e S a n t o s h P

    Z915AM_OOPS64:

    REPORT Z915AM_OOPS64.

    data ob typerefto z915am_class11.

    create object ob.

    sethandlerob->m1 forob.

    callmethod ob->m2.

    OUTPUT:

  • 7/30/2019 Ooabap Notes With Programs

    90/93

    90 | P a g e S a n t o s h P

    Z915AM_RESTAURANT:

    methodSET.

    TABLENO = 10.

    STEWARD = 'ABC'.endmethod.

    method DISPLAY.WRITE :/ TABLENO,STEWARD.

    endmethod.

    Z915AMCYCLE:

  • 7/30/2019 Ooabap Notes With Programs

    91/93

    91 | P a g e S a n t o s h P

    method SETCYCLE.

    wheels = 2.

    brakes = 2.

    colour = 'blue'.

    endmethod.

    method DISPLAY.

    write :/ wheels,brakes,colour.endmethod.

    Z915AMSCOOTER:

  • 7/30/2019 Ooabap Notes With Programs

    92/93

    92 | P a g e S a n t o s h P

    method SETSCOOTER.wheels = 2.

    brakes = 4.colour = 'red'.

    endmethod.

    Z915AMCAR:

  • 7/30/2019 Ooabap Notes With Programs

    93/93

    method SETCAR.wheels = 4.

    brakes = 5.colour = 'cyan'.

    endmethod.