object oriented abap guide

Upload: navabhatt

Post on 06-Apr-2018

283 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Object Oriented ABAP Guide

    1/40

  • 8/3/2019 Object Oriented ABAP Guide

    2/40

    Page 2 of 40

    CLASSES

    Classes are the central element of object-orientation. A Class describes a

    general element or a general concept. Classes realize an abstract data type. Classes

    contain components like: Attributes, Methods and Events. These components are

    described later in this document. In ABAP Objects classes are made up of a definition

    and an implementation part.CLASS CL_CLASSNAME DEFINITION.

    ENDCLASS.

    CLASS CL_CLASSNAME IMPLEMENTATION.

    ENDCLASS.

    OBJECTS / INSTANCES

    An object is nothing more than an instance of a Class.

    Object - A person, place, thing, concept, or event that is applicable to the

    system at hand. Objects both know things (i.e., they have data) and they do things

    (i.e. they have functionality).

    In ABAP Objects, objects are created with the command CREATE OBJECT. The

    developer receives an object reference as a result of its creation:

    DATA: instance TYPE REF TO CL_CLASSNAME.

    START-OF-SELECTION.

    CREATE OBJECT instance.

    ATTRIBUTES

    Attributes can take on values within an object at runtime. The sum of all

    attributes and their values describes the state of an object.

    Attributes can be defined as instance dependent as well as Class dependent.

    Class attributes (Class attributes are also called static attributes.) are not tied to a

    single instance, rather they "belong" to all instances of the Class. These attributes

    exist only once in main memory. Instance-dependent attributes exist once per

    instance and are tied to a single instance.

    In ABAP Objects you differentiate between instance-dependent and class-

    dependent attributes by means of the ABAP keywords DATA or CLASS-DATA to be

    used in the definition part:

  • 8/3/2019 Object Oriented ABAP Guide

    3/40

    Page 3 of 40

    Report : 001

    Code listing for: Z_001_SIMPLE_CLASS

    Description: SIMPLE CLASS WITH ATTRIBUTES

    REPORT Z_001_SIMPLE_CLASS.

    TYPES: TY_NAME(50) TYPE C.

    *----------------------------------------------------------------

    * CLASS CL_NAME DEFINITION

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION.

    PUBLIC SECTION.

    CLASS-DATA I_COUNT TYPE I. " Class Dependent Attribute

    " Will have same value in all objects

    DATA NAME TYPE TY_NAME. " Instance Dependent Attribute

    " Will have different values in different objects

    ENDCLASS. "CL_NAME DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

    " We will use this portion in next examples.

    ENDCLASS. "CL_NAME IMPLEMENTATION

    " CREATE REFERENCE VARIABLES

    DATA: INSTANCE1 TYPE REF TO CL_NAME,

    INSTANCE2 TYPE REF TO CL_NAME,

    INSTANCE3 TYPE REF TO CL_NAME.

    START-OF-SELECTION.

    " CREATE OBJECTS (INSTANCES)

    CREATE OBJECT: INSTANCE1,

    INSTANCE2,

    INSTANCE3.

  • 8/3/2019 Object Oriented ABAP Guide

    4/40

    Page 4 of 40

    " SET ATTRIBUTES OF INSTANCE1

    INSTANCE1->I_COUNT = INSTANCE1->I_COUNT + 10.

    INSTANCE1->NAME = 'NARESH'.

    " SET ATTRIBUTES OF INSTANCE2

    INSTANCE2->I_COUNT = INSTANCE2->I_COUNT + 10.

    INSTANCE2->NAME = 'SAMEER'.

    " SET ATTRIBUTES OF INSTANCE3

    INSTANCE3->I_COUNT = INSTANCE3->I_COUNT + 10.

    INSTANCE3->NAME = 'RAJESH'.

    " DISPLAY ATTRIBUTES OF INSTANCE1

    WRITE: 'ATTRIBUTES OF INSTANCE 1 :::',

    / 'COUNT : ', INSTANCE1->I_COUNT,

    / 'NAME : ' , INSTANCE1->NAME.

    SKIP.

    SKIP.

    " DISPLAY ATTRIBUTES OF INSTANCE2

    WRITE: 'ATTRIBUTES OF INSTANCE 2 :::',

    / 'COUNT : ', INSTANCE2->I_COUNT,

    / 'NAME : ' , INSTANCE2->NAME.

    SKIP.

    SKIP.

    " DISPLAY ATTRIBUTES OF INSTANCE3

    WRITE: 'ATTRIBUTES OF INSTANCE 3 :::',

    / 'COUNT : ', INSTANCE3->I_COUNT,

    / 'NAME : ' , INSTANCE3->NAME.

    " CLEAR MEMORY OCCUPIED BY OBJECTS

    CLEAR: INSTANCE1,

    INSTANCE2,

  • 8/3/2019 Object Oriented ABAP Guide

    5/40

    Page 5 of 40

    INSTANCE3.

    Report Output : 001

    ATTRIBUTES OF INSTANCE 1 :::

    COUNT : 30

    NAME : NARESH

    ATTRIBUTES OF INSTANCE 2 :::

    COUNT : 30

    NAME : SAMEER

    ATTRIBUTES OF INSTANCE 3 :::

    COUNT : 30

    NAME : RAJESH

    METHODS

    As well as attributes, Classes have so-called Methods. While attributes

    describe the static structure of a class and its objects, Methods describe the behavior

    of objects within a class. With the help of methods, the system provides operations,

    services and functions. Via methods, a user can manipulate the objects in a class or

    also the class itself. As for attributes, there are instance-dependent as well as class-

    dependent (static) methods. ABAP Objects differentiate between instance-dependent

    and class-dependent methods via the ABAP keywords METHODS or CLASS-METHODS

    used in the definition part.

    In order to carry out instance-dependent (or instance-dependent) methods,

    the calling program needs a specific instance of the class. That is, the calling

    program must have a defined reference variable that points to a specific instance.

    Class methods are not instance-dependent. They can be called at any time by a user.

    To see how the syntax calls the various method types, see the following example.

    Report : 002

    Code listing for : Z_002_METHODS

    Description: EXAMPLE SHOWING USE OF METHODS

    REPORT Z_002_METHODS.

    TYPES: TY_NAME(50) TYPE C,

    BOOLEAN(1) TYPE C.

    CONSTANTS: CO_TRUE TYPE C VALUE 'X',

  • 8/3/2019 Object Oriented ABAP Guide

    6/40

    Page 6 of 40

    CO_FALSE TYPE C VALUE ' '.

    *----------------------------------------------------------------

    * CLASS CL_NAME DEFINITION

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION.

    PUBLIC SECTION.

    CLASS-DATA I_COUNT TYPE I. " Class Dependent Attribute

    " Will have same value in all objects

    DATA NAME TYPE TY_NAME. " Instance Dependent Attribute

    " Will have different values in different objects

    CLASS-METHODS IS_CLASS_INITIATED

    EXPORTING VALUE(RE_BOOL) TYPE BOOLEAN.

    METHODS: SET_NAME IMPORTING VALUE(IM_NAME) TYPE TY_NAME,

    GET_NAME EXPORTING VALUE(EX_NAME) TYPE TY_NAME.

    ENDCLASS. "CL_NAME DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

    METHOD IS_CLASS_INITIATED.

    IF I_COUNT > 0.

    RE_BOOL = CO_TRUE.

    ELSE.

    RE_BOOL = CO_FALSE.

    ENDIF.

    ENDMETHOD. "IS_CLASS_INITIATED

    METHOD SET_NAME.NAME = IM_NAME.

    ENDMETHOD. "SET_NAME

    METHOD GET_NAME.

    EX_NAME = NAME.

  • 8/3/2019 Object Oriented ABAP Guide

    7/40

    Page 7 of 40

    ENDMETHOD. "GET_NAME

    ENDCLASS. "CL_NAME IMPLEMENTATION

    START-OF-SELECTION.

    DATA BOOL TYPE BOOLEAN.

    " CHECK IF ANY INSTANCE OF THIS CLASS HAS BEEN CREATED BEFORE

    CALL METHOD CL_NAME=>IS_CLASS_INITIATED

    IMPORTING

    RE_BOOL = BOOL.

    IF BOOL = CO_FALSE.

    " CREATE INSTANCE OF THIS CLASS, IF NO INSTANCE EXIST

    DATA: INSTANCE TYPE REF TO CL_NAME,

    NAME TYPE TY_NAME.

    CREATE OBJECT INSTANCE.

    " SET VALUE FOR NAME ATTRIBUTE

    CALL METHOD INSTANCE->SET_NAME

    EXPORTING

    IM_NAME = 'NARESH PATEL'.

    " GET VALUE OF NAME ATTRIBUTE

    CALL METHOD INSTANCE->GET_NAME

    IMPORTING

    EX_NAME = NAME.

    WRITE: / 'NAME : ' , NAME.

    " CLEAR MEMORY OCCUPIED BY OBJECTS

    CLEAR: INSTANCE.

    ENDIF.

    Report Output : 002

    NAME : NARESH PATEL

    CONSTRUCTOR

  • 8/3/2019 Object Oriented ABAP Guide

    8/40

    Page 8 of 40

    Objects must be created at runtime (using CREATE OBJECT). With their

    creation they also get their own identity. However, there are no fixed attribute

    values linked to the identity. You are probably already wondering how objects get to

    their initial state. How do objects recognize their initial attribute values?

    TheConstructor concept exists specifically to answer this question. The

    constructor is a method which runs automatically during the creation of an object.

    The constructor allows you to define IMPORTING-parameters.

    In ABAP Objects you differentiate between instance-dependent and class-dependent

    constructors via the language elements METHODS{{ }}and CLASS-METHODS to be

    used in the definition part and via their namesconstructor and

    CLASS_CONSTRUCTOR:

    The class constructor is called by the first access to a class element (method,

    attribute, event, and object), the (instance) constructor by the creation of an object

    (CREATE OBJECT).

    Report : 003

    Code listing for: Z_003_CONSTRUCTOR

    Description: EXAMPLE OF USING CONSTRUCTOR

    REPORT Z_003_CONSTRUCTOR.

    TYPES: TY_NAME(50) TYPE C.

    *----------------------------------------------------------------

    * CLASS CL_NAME DEFINITION

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION.

    PUBLIC SECTION.

    CLASS-DATA I_COUNT TYPE I. " Class Dependent Attribute

    " Will have same value in all objects

    DATA NAME TYPE TY_NAME. " Instance Dependent Attribute

    " Will have different values in different objects

    CLASS-METHODS CLASS_CONSTRUCTOR.

    METHODS: CONSTRUCTOR IMPORTING VALUE(IM_NAME) TYPE TY_NAME,

    GET_NAME EXPORTING VALUE(EX_NAME) TYPE TY_NAME.

    ENDCLASS. "CL_NAME DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

  • 8/3/2019 Object Oriented ABAP Guide

    9/40

    Page 9 of 40

    METHOD CLASS_CONSTRUCTOR.

    I_COUNT = I_COUNT + 10.

    ENDMETHOD. "IS_CLASS_INITIATED

    METHOD CONSTRUCTOR.

    NAME = IM_NAME.

    ENDMETHOD. "SET_NAME

    METHOD GET_NAME.

    EX_NAME = NAME.

    ENDMETHOD. "GET_NAME

    ENDCLASS. "CL_NAME IMPLEMENTATION

    START-OF-SELECTION.

    " CREATE INSTANCE OF THIS CLASS, IF NO INSTANCE EXISTS.

    DATA: INSTANCE1 TYPE REF TO CL_NAME,

    INSTANCE2 TYPE REF TO CL_NAME,

    NAME1 TYPE TY_NAME,

    NAME2 TYPE TY_NAME.

    CREATE OBJECT INSTANCE1

    EXPORTING

    IM_NAME = 'NARESH PATEL'.

    CREATE OBJECT INSTANCE2

    EXPORTING

    IM_NAME = 'SAMEER V. PATEL'.

    " GET VALUE OF NAME ATTRIBUTE

    CALL METHOD INSTANCE1->GET_NAME

    IMPORTING

    EX_NAME = NAME1.

    CALL METHOD INSTANCE2->GET_NAME

    IMPORTING

    EX_NAME = NAME2.

  • 8/3/2019 Object Oriented ABAP Guide

    10/40

    Page 10 of 40

    " DISPLAY ATTRIBUTES OF INSTANCE1

    WRITE: 'ATTRIBUTES OF INSTANCE 1 : ',

    / 'COUNT : ', INSTANCE1->I_COUNT,

    / 'NAME : ' , NAME1.

    SKIP.

    SKIP.

    " DISPLAY ATTRIBUTES OF INSTANCE2

    WRITE: 'ATTRIBUTES OF INSTANCE 2 : ',

    / 'COUNT : ', INSTANCE2->I_COUNT,

    / 'NAME : ' , NAME2.

    " CLEAR MEMORY OCCUPIED BY OBJECTS

    CLEAR: INSTANCE1,

    INSTANCE2.

    Report Output : 003

    ATTRIBUTES OF INSTANCE 1 :

    COUNT : 10

    NAME : NARESH PATEL

    ATTRIBUTES OF INSTANCE 2 :

    COUNT : 10

    NAME : SAMEER V. PATEL

    OBJECT IDENTITY AND REFERENCE SEMANTICS

    With the help of the previous examples, you have established that objects

    belonging to a class are not created by the simple definition of the class. Neither

    does the instruction DATA: instance ref to CL_NAME creates an object. This

    instruction only creates a Reference, which in its initial state has the logical value

    INITIAL. Only with the instruction CREATE OBJECTinstance is the memory area for anew object requested from the system. The reference instance then refers to the

    object which has just been created. (The command CLEAR{{ instance. }}at this

    point means that the object, to which the reference variable refers, cannot be

    referenced. Therefore it can no longer be addressed in this program run. A Garbage

    Collector running in the background ensures that the object is removed from

    memory.

  • 8/3/2019 Object Oriented ABAP Guide

    11/40

    Page 11 of 40

    This separates object-oriented implementation from classic implementation.

    With the classic DATA instruction, main memory is reserved (which might never be

    used) and is pre-allocated the initial state of the relevant variable. With the "object-

    oriented" instruction DATA-x-TYPE-REF-TO, only the intention to create an object is

    expressed. The only storage space occupied is for an object reference.

    In addition, every object has its own identity. Two references, which refer to

    objects, are only identical if they refer to the same object. Similarity between the

    attribute values of these objects is not the deciding factor. To get more idea about

    this see the following example.

    Report : 004

    Code listing for: Z_004_OBJECT_IDENTI TY

    Description: OBJECT IDENTITY & REFERENCE SEMANTICS

    REPORT Z_004_OBJECT_IDENTITY.

    TYPES: TY_NAME(50) TYPE C.

    *----------------------------------------------------------------

    * CLASS CL_NAME DEFINITION

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION.

    PUBLIC SECTION.

    METHODS: CONSTRUCTOR IMPORTING VALUE(IM_NAME) TYPE TY_NAME.

    DATA: NAME TYPE TY_NAME.

    ENDCLASS. "CL_NAME DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

    METHOD CONSTRUCTOR.

    NAME = IM_NAME.

    ENDMETHOD. "CONSTRUCTOR

    ENDCLASS. "CL_NAME IMPLEMENTATION

    DATA: INSTANCE1 TYPE REF TO CL_NAME.

    DATA: INSTANCE2 TYPE REF TO CL_NAME.

    DATA: INSTANCE3 TYPE REF TO CL_NAME.

  • 8/3/2019 Object Oriented ABAP Guide

    12/40

    Page 12 of 40

    START-OF-SELECTION.

    CREATE OBJECT INSTANCE1

    EXPORTING

    IM_NAME = 'NARESH'.

    CREATE OBJECT INSTANCE2

    EXPORTING

    IM_NAME = 'NARESH'.

    " Reference semantics !!!

    IF INSTANCE1 = INSTANCE2.

    WRITE 'This will never print, as instance1 refers to a

    different object than instance2.'.

    ENDIF.

    " Attribute values are the same !!!

    IF INSTANCE1->NAME = INSTANCE2->NAME.

    WRITE 'Attribute values of instance1 and instace2 are same.'.

    ENDIF.

    INSTANCE3 = INSTANCE1.

    IF INSTANCE3 = INSTANCE1.

    WRITE 'Both references instance1 and instace3 refer to the

    same object and are therefore identical.'.

    ENDIF.

    Report Output : 004

    Attribute values of instance1 and instace2 are same.

    Both references instance1 and instace3 refer to the same object and are

    therefore identical.

    In the above program, the first IF-Query (IF }}INSTANCE1 = INSTANCE2{{.)

    via the object reference produces the value false, although both objects have the

    same attribute value "NARESH". Note that in object-oriented languages the reference

    semantics apply for classes and their objects. Both objects have the attribute value

    {{NAME = }}NARESH, but they are independent objects with their own identity. The

    references therefore refer to two different objects, although their attribute values are

    completely identical.

  • 8/3/2019 Object Oriented ABAP Guide

    13/40

    Page 13 of 40

    VISIBILITY

    An important feature of object-orientation is the encapsulation of attributes

    and methods - ultimately of functionality - in classes. A class guarantees its user

    specific properties and specific behavior. The sum of these properties is called the

    class interface. The Visibility mechanism defines the class interface which is available

    to the users.

    There are three commonly defined types of visibility in object-oriented technology:

    Publ ic

    The relevant class component (attribute, method, event etc.) is visible to all classes.

    Protec ted

    The relevant class component (attribute, method, event etc.) is visible to the class

    itself and all inheritors. (We will return to the terms Inheritor and Inheritance later in

    this document.)

    Pr iva te

    The relevant class component (attribute, method, event etc.) is only visible to the

    class itself.

    Report : 005

    Code listing for: Z_005_VISIBILITY

    Description: COMPONENT VISIBI LITY

    *---------------------------------------------------------------

    * In this example the PRIVATE attribute NAME is hidden from the

    * users. Access to the information it contains is realized via

    * the PUBLIC method SET_NAME & GET_NAME.

    *---------------------------------------------------------------

    *

    * TYPES OF VISIBILITY :

    * (1) PUBLIC : class component is visible to all classes.

    * (2) PRIVATE : class component is visible to the class itself.

    * (3) PROTECTED : class component is visible to class itself and

    * all inheritors (sub class).

    *

    *---------------------------------------------------------------

    REPORT Z_005_VISIBILITY.

    TYPES: TY_NAME(50) TYPE C.

    *----------------------------------------------------------------

    * CLASS CL_NAME DEFINITION

  • 8/3/2019 Object Oriented ABAP Guide

    14/40

    Page 14 of 40

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION.

    PUBLIC SECTION.

    METHODS: SET_NAME IMPORTING VALUE(IM_NAME) TYPE TY_NAME,

    GET_NAME EXPORTING VALUE(EX_NAME) TYPE TY_NAME.

    PRIVATE SECTION.

    DATA NAME TYPE TY_NAME.

    ENDCLASS. "CL_NAME DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

    METHOD SET_NAME .

    NAME = IM_NAME.

    ENDMETHOD. "SET_NAME

    METHOD GET_NAME.

    EX_NAME = NAME.

    ENDMETHOD. "GET_NAME

    ENDCLASS. "CL_NAME IMPLEMENTATION

    START-OF-SELECTION.

    " CREATE INSTANCE OF THIS CLASS, IF NO INSTANCE EXIST

    DATA: INSTANCE TYPE REF TO CL_NAME,

    NAME TYPE TY_NAME.

    CREATE OBJECT INSTANCE.

    " SET VALUE FOR NAME ATTRIBUTE

    CALL METHOD INSTANCE->SET_NAME

    EXPORTING

    IM_NAME = 'NARESH PATEL'.

    " GET VALUE OF NAME ATTRIBUTE

    CALL METHOD INSTANCE->GET_NAME

    IMPORTING

  • 8/3/2019 Object Oriented ABAP Guide

    15/40

    Page 15 of 40

    EX_NAME = NAME.

    WRITE: / 'NAME : ' , NAME.

    " CLEAR MEMORY OCCUPIED BY OBJECTS

    CLEAR: INSTANCE.

    Report Output : 005

    NAME : NARESH PATEL

    EVENTS

    Events are recognized in particular by programming interfaces of the GUIs

    (Windows, Motif, etc.), for example, you can "ask" the GUI to trigger an event if the

    user moves the mouse over a specific part of the screen. When the event occurs you

    are telling the GUI to change the shape of the mouse pointer.

    Events allow for the loose coupling of components (classes or objects) in a

    system. The event trigger does not normally know at the time of coding who is goingto react to the event. Those components, which want to react to the event, register

    at the event runtime, in that they tell the runtime environment which method is to

    be executed when the event is raised. In this way many components can register for

    an event.

    Event handler methods can proceed synchronously as well as asynchronously.

    At present, ABAP Objects only supports synchronous calling of the event handler

    method.

    Report : 006

    Code listing fo r: Z_006_EVENTDescription: EXAMPLE OF EVENTS

    *---------------------------------------------------------------

    * THIS EXAMPLE SHOWS THE USE OF EVENTS.

    *---------------------------------------------------------------

    *

    * The event trigger does not normally know at the time of

    * coding who is going to react to the event. Those components,

    * which want to react to the event, register at the event

    * runtime, in that they tell the runtime environment which

    * method is to be executed when the event is raised. In this way

    * many components can register for an event.

    *

    *---------------------------------------------------------------

  • 8/3/2019 Object Oriented ABAP Guide

    16/40

    Page 16 of 40

    REPORT Z_006_EVENT.

    *----------------------------------------------------------------

    * CLASS CL_NAME

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION.

    PUBLIC SECTION.

    " DEFINE EVENT

    EVENTS OBJECT_CREATED

    EXPORTING VALUE(EX_OBJ) TYPE REF TO CL_NAME.

    METHODS: CONSTRUCTOR,

    " DEFINE EVENT HANDLER METHOD

    PROCESS_EVENT FOR EVENT OBJECT_CREATED OF CL_NAME.

    PRIVATE SECTION.

    DATA MSG(16) TYPE C.

    " register method with runtime will be executed

    " when event OBJECT_CREATED fires.

    ENDCLASS. "CL_NAME

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

    METHOD CONSTRUCTOR.

    MSG = 'OBJECT CREATED'.

    " Register the event handlers for the corresponding/all

    " instance/s.

    SET HANDLER PROCESS_EVENT FOR ALL INSTANCES.

    " Raise event OBJECT_CREATED.

    RAISE EVENT OBJECT_CREATED EXPORTING EX_OBJ = ME.

    "ME refers to current instance

    ENDMETHOD. "CL_NAME

    " EVENT HANDLER

    METHOD PROCESS_EVENT.

  • 8/3/2019 Object Oriented ABAP Guide

    17/40

    Page 17 of 40

    WRITE: 'EVENT FIRED :', ME->MSG.

    ENDMETHOD. "PROCESS_EVENT

    ENDCLASS. "CL_NAME IMPLEMENTATION

    DATA INSTANCE TYPE REF TO CL_NAME.

    START-OF-SELECTION.

    CREATE OBJECT INSTANCE.

    CLEAR INSTANCE.

    Report Output : 006

    EVENT FIRED : OBJECT CREATED

    In the previous program, instances of the Class CL_NAMEcreate an event

    OBJECT_CREATED. The class method PROCESS_EVENT processes this event. With

    the help of the constructor the class registers the method PROCESS_EVENT at the

    event OBJECT_CREATED for all instances of the class CL_NAME. When an object is

    created for the class CL_NAME (CREATE OBJECT instance.), the event

    OBJECT_CREATED is raised in the constructor. The consequence is that the method

    PROCESS_EVENT{{ }}is executed.

    Report : 007

    Code listing for : Z_007_EVENT2

    Description: EVENT OF ONE CLASS HANDLED BY ANOTHER CLASS

    REPORT Z_007_EVENT2.

    *----------------------------------------------------------------

    * CLASS CL_CLASS1 DEFINITION

    *----------------------------------------------------------------

    CLASS CL_CLASS1 DEFINITION.

    PUBLIC SECTION.

    EVENTS OBJECT_CREATED. " DEFINES EVENT

    METHODS CONSTRUCTOR.

    ENDCLASS. "CL_CLASS1 DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_CLASS1 IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_CLASS1 IMPLEMENTATION.

  • 8/3/2019 Object Oriented ABAP Guide

    18/40

    Page 18 of 40

    METHOD CONSTRUCTOR.

    RAISE EVENT OBJECT_CREATED. " RAISES EVENT

    ENDMETHOD. "CONSTRUCTOR

    ENDCLASS. "CL_CLASS1 IMPLEMENTATION

    *----------------------------------------------------------------

    * CLASS CL_CLASS2 DEFINITION

    *----------------------------------------------------------------

    CLASS CL_CLASS2 DEFINITION.

    PUBLIC SECTION.

    CLASS-METHODS INITIALISE.

    PRIVATE SECTION.

    CLASS-METHODS PROCESS_EVENT FOR EVENT OBJECT_CREATED

    OF CL_CLASS1. " REGISTERS METHOD

    ENDCLASS. "CL_CLASS2 DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_CLASS2 IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_CLASS2 IMPLEMENTATION.

    METHOD INITIALISE.

    "SET HANDLER FOR ALL INSTANCES

    SET HANDLER PROCESS_EVENT FOR ALL INSTANCES.

    ENDMETHOD. "INITIALISE

    " EVENT HANDLER

    METHOD PROCESS_EVENT.

    WRITE 'EVENT FIRED.'.

    ENDMETHOD. "PROCESS_EVENT

    ENDCLASS. "CL_CLASS2 IMPLEMENTATION

    DATA INSTANCES TYPE REF TO CL_CLASS1.

    START-OF-SELECTION.

    CALL METHOD CL_CLASS2=>INITIALISE.

    CREATE OBJECT INSTANCES.

  • 8/3/2019 Object Oriented ABAP Guide

    19/40

    Page 19 of 40

    CLEAR INSTANCES.

    Report Output : 007

    EVENT FIRED.

    In the above program, with the help of the class method initialize, the class

    CL_CLASS2 registers the method PROCESS_EVENT at the event OBJECT_CREATED

    for all instances of the class CL_CLASS1. When an object is created for the classCL_CLASS1 (CREATE OBJECT instance.), the event OBJECT_CREATED is raised in the

    constructor. The consequence is that the method PROCESS_EVENT{{ }}belonging to

    the class CL_CLASS2 is executed.

    INHERITANCE

    Inheritance defines the relationship between classes, in which a class

    (subclass) uses the structure and behavior that has already been defined in one or

    more other classes (superclasses). So simply this means "Inheritance is about

    reuse!".

    Allow me to use a concrete example to explain inheritance: Collection.

    A collection is any number of objects (more precisely object references).

    However, there could be many types of collection. Therefore, I will implement each

    type of collection as a class. In principle this approach is correct. However, you will

    soon establish that all collections have several components in common like:

    1. Each class requires a method in order to add objects to a collection.

    2. Each class requires a method in order to delete objects from a

    collection.

    3. Each class has a method which identifies the number of objectreferences in the collection and so on.

    Inheritance is the solution to this situation. You implement all of the

    similarities in the class which is Superclass. You then implement the individual types

    of collection in their own classes which are Subclassesof the Superclass. As a

    subclass, these classes inherit all of the components of the Superclass. Attributes,

    methods and events are inherited. In addition, you can implement additional

    attributes, methods and events in the subclass.

    POLYMORPHISM Polymorphism occurs, where classes implement the same functionality with

    different methods (one functionality, several methods but the same name). This can

    occur via an inheritance relationship, in that the methods belonging to the superclass

    are redefined in the subclasses and implemented differently. ABAP Objects requires

    the method names to be the same and the signature to be the same (signature =

    method interface).

    Polymorphism can be achieved in 2 ways:

  • 8/3/2019 Object Oriented ABAP Guide

    20/40

    Page 20 of 40

    (1) Two independent classes implement methods with the same names and the same

    signature with the intention, that the methods should be called dynamically from a

    third location.

    (2) A superclass implements a method, and in a subclass you want to re-implement

    the same method, as the superclass implementation is not suitable for the subclass.

    The first scenario will not occur very often in ABAP Objects, as the interface concept

    was created precisely for such cases.

    Report : 008

    Code listing for: Z_008_INHERIT_POLY

    Description: EXAMPLE OF INHERITANCE & POLYMORPISM

    REPORT Z_008_INHERIT_POLY.

    TYPES: BEGIN OF TY_NAME,

    NAME(50) TYPE C,

    END OF TY_NAME.

    TYPES: TY_NAMES TYPE TY_NAME OCCURS 0.

    *----------------------------------------------------------------

    * CLASS CL_COLLECTION DEFINITION

    *----------------------------------------------------------------

    CLASS CL_COLLECTION DEFINITION.

    PUBLIC SECTION.

    METHODS: ADD IMPORTING IM_NAME TYPE TY_NAME,

    DISPLAY.

    PROTECTED SECTION.

    DATA IT_NAMES TYPE TY_NAMES.

    ENDCLASS. "CL_COLLECTION DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_COLLECTION IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_COLLECTION IMPLEMENTATION.

    METHOD ADD.

    APPEND IM_NAME TO IT_NAMES.

    ENDMETHOD. "ADD

    METHOD DISPLAY.

  • 8/3/2019 Object Oriented ABAP Guide

    21/40

    Page 21 of 40

    DATA: CNT TYPE I.

    DESCRIBE TABLE IT_NAMES LINES CNT.

    IF CNT > 0.

    DATA WA_NAME LIKE LINE OF IT_NAMES.

    ULINE.

    WRITE 10 'DISPLAYING DATA'.

    ULINE.

    WRITE 10 'NAMES'.

    ULINE.

    LOOP AT IT_NAMES INTO WA_NAME.

    WRITE /10 WA_NAME-NAME.

    ENDLOOP.

    ENDIF.

    ENDMETHOD. "DISPLAY

    ENDCLASS. "CL_COLLECTION IMPLEMENTATION

    *----------------------------------------------------------------

    * CLASS CL_NAME DEFINITION

    *----------------------------------------------------------------

    CLASS CL_NAME DEFINITION INHERITING FROM CL_COLLECTION.

    PUBLIC SECTION.

    METHODS: ADD REDEFINITION. " THIS IS CALLED POLYMORPISM.

    ENDCLASS. "CL_NAME DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_NAME IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_NAME IMPLEMENTATION.

    METHOD ADD.

    " NO DOUBLE ENTRIES ARE ALLOWED.READ TABLE IT_NAMES WITH KEY NAME = IM_NAME-NAME

    TRANSPORTING NO FIELDS.

    IF SY-SUBRC 0.

    CALL METHOD SUPER->ADD

    EXPORTING

  • 8/3/2019 Object Oriented ABAP Guide

    22/40

    Page 22 of 40

    IM_NAME = IM_NAME.

    ENDIF.

    ENDMETHOD. "ADD

    ENDCLASS. "CL_NAME IMPLEMENTATION

    DATA: INSTANCE TYPE REF TO CL_NAME,

    ST_NAME TYPE TY_NAME.

    START-OF-SELECTION.

    CREATE OBJECT INSTANCE.

    ST_NAME-NAME = 'NARESH PATEL'.

    CALL METHOD INSTANCE->ADD

    EXPORTING

    IM_NAME = ST_NAME.

    WRITE / 'ADDED NARESH PATEL FOR THE 1ST TIME.'.

    ST_NAME-NAME = 'SAMEER PATEL'.

    CALL METHOD INSTANCE->ADD

    EXPORTING

    IM_NAME = ST_NAME.

    WRITE / 'ADDED SAMEER PATEL FOR THE 1ST TIME.'.

    ST_NAME-NAME = 'RAJESH PATEL'.

    CALL METHOD INSTANCE->ADD

    EXPORTING

    IM_NAME = ST_NAME.

    WRITE / 'ADDED RAJESH PATEL FOR THE 1ST TIME.'.

    ST_NAME-NAME = 'NARESH PATEL'.

    CALL METHOD INSTANCE->ADD

    EXPORTING

    IM_NAME = ST_NAME.

    WRITE / 'ADDED NARESH PATEL FOR THE 2ND TIME.'.

    ST_NAME-NAME = 'SAMEER PATEL'.

  • 8/3/2019 Object Oriented ABAP Guide

    23/40

    Page 23 of 40

    CALL METHOD INSTANCE->ADD

    EXPORTING

    IM_NAME = ST_NAME.

    WRITE / 'ADDED SAMEER PATEL FOR THE 2ND TIME.'.

    ST_NAME-NAME = 'RAJESH PATEL'.

    CALL METHOD INSTANCE->ADD

    EXPORTING

    IM_NAME = ST_NAME.

    WRITE / 'ADDED RAJESH PATEL FOR THE 2ND TIME.'.

    SKIP.

    SKIP.

    CALL METHOD INSTANCE->DISPLAY.

    Report Output : 008

    ADDED NARESH PATEL FOR THE 1ST TIME.

    ADDED SAMEER PATEL FOR THE 1ST TIME.

    ADDED RAJESH PATEL FOR THE 1ST TIME.

    ADDED NARESH PATEL FOR THE 2ND TIME.

    ADDED SAMEER PATEL FOR THE 2ND TIME.

    ADDED RAJESH PATEL FOR THE 2ND TIME.

    -----------------------------------------------------------------

    DISPLAYING DATA

    -----------------------------------------------------------------

    NAMES

    -----------------------------------------------------------------

    NARESH PATEL

    SAMEER PATEL

    RAJESH PATEL

    In the above example, I have partially implemented the class

    CL_COLLECTION and its inheritor class CL_NAME. The method ADD{{ }}belonging to

    the class CL_COLLECTION is redefined by the method ADD{{ }}belonging to the

    class CL_NAME. In the method ADDbelonging to the CL_NAME the method ADD

    belonging to the superclass CL_COLLECTION is called via CALL METHOD SUPER-

  • 8/3/2019 Object Oriented ABAP Guide

    24/40

    Page 24 of 40

    >ADD.

    INTERFACES

    TheInterface concept describes a class interface. You can define the same

    components in an interface that you can in classes, however without implementing

    them.

    Classes can implement interfaces, and subsequently be addressed via theseinterfaces. This opens an additional mechanism of polymorphism, however without

    being dependent on inheritance. This combination of classes with simple inheritance

    and interfaces is more highly regarded by many experts than full multiple

    inheritances.

    In addition to object references (DATA: instance TYPE REF TO CL_CLASS)

    there are also Interface References (DATA: reference TYPE REF TO CL_INTERFACE).

    A class which implements a specific interface can be addressed via this interface

    reference. Using such an interface reference, you can access the components defined

    in the interface. In this way a user can view different classes through the 'spectacles'

    of an interface and address them in a uniform manner.

    Interfaces therefore define specific, generic functionality. In contrast toclasses, interfaces generally have not only many users, but also many implementers.

    Report : 009

    Code listing for: Z_009_INTERFACE

    Description: EXAMPLE OF I NTERFACE

    *---------------------------------------------------------------

    * Report Z_009_INTERFACE

    *---------------------------------------------------------------

    *

    * The Interface concept describes a class interface. You can

    * define the same components in an interface that you can in

    * classes, however without implementing them. Classes can

    * Implement interfaces, and subsequently be addressed via these

    * interfaces.

    *

    *---------------------------------------------------------------

    * IN THIS EXAMPLE : The Interface IF_INTERFACE is implemented

    * via the classes CL_CLASS1 And CL_CLASS2.

    *---------------------------------------------------------------

    REPORT Z_009_INTERFACE.

  • 8/3/2019 Object Oriented ABAP Guide

    25/40

    Page 25 of 40

    *----------------------------------------------------------------

    * INTERFACE IF_INTERFACE

    *----------------------------------------------------------------

    INTERFACE IF_INTERFACE.

    METHODS DISPLAY.

    ENDINTERFACE. "IF_INTERFACE

    *----------------------------------------------------------------

    * CLASS CL_CLASS1 DEFINITION

    *----------------------------------------------------------------

    CLASS CL_CLASS1 DEFINITION.

    PUBLIC SECTION.

    INTERFACES IF_INTERFACE.

    ENDCLASS. "CL_CLASS1 DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_CLASS1 IMPLEMENTATION

    *----------------------------------------------------------------

    CLASS CL_CLASS1 IMPLEMENTATION.

    METHOD IF_INTERFACE~DISPLAY.

    WRITE / 'METHOD IMPLEMENTED IN CLASS 1.'.

    ENDMETHOD. "IF_INTERFACE~DISPLAY

    ENDCLASS. "CL_CLASS1 IMPLEMENTATION

    *----------------------------------------------------------------

    * CLASS CL_CLASS2 DEFINITION

    *----------------------------------------------------------------

    CLASS CL_CLASS2 DEFINITION.

    PUBLIC SECTION.

    INTERFACES IF_INTERFACE.ENDCLASS. "CL_CLASS2 DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_CLASS2 IMPLEMENTATION

    *----------------------------------------------------------------

  • 8/3/2019 Object Oriented ABAP Guide

    26/40

    Page 26 of 40

    CLASS CL_CLASS2 IMPLEMENTATION.

    METHOD IF_INTERFACE~DISPLAY.

    WRITE / 'METHOD IMPLEMENTED IN CLASS 2.' .

    ENDMETHOD. "IF_INTERFACE~DISPLAY

    ENDCLASS. "CL_CLASS2 IMPLEMENTATION

    DATA: INSTANCE1 TYPE REF TO CL_CLASS1,

    INSTANCE2 TYPE REF TO CL_CLASS2,

    INTERFACE TYPE REF TO IF_INTERFACE.

    START-OF-SELECTION.

    CREATE OBJECT INSTANCE1.

    INTERFACE = INSTANCE1. " THIS ASSIGNATIN IS CALLED CASTING

    CALL METHOD INTERFACE->DISPLAY.

    CREATE OBJECT INSTANCE2.

    INTERFACE = INSTANCE2.

    CALL METHOD INTERFACE->DISPLAY.

    Report Output : 009

    METHOD IMPLEMENTED IN CLASS 1.

    METHOD IMPLEMENTED IN CLASS 2.

    ALV GRID CONTROL

    The ALV Grid Control is a tool that you can use to display non-hierarchical

    lists in a uniform format. The list data is displayed as a table on the screen. This tool

    is very user-friendly: You only have to carry out a minimum number of programming

    steps.

    The ALV Grid Control has a number of interactive standard functions that list

    users frequently use, for example, printing, and export. Developers can hide these

    standard functions if they wish. However, you can modify the implementations on an

    application-specific basis if required. You can also add your own functions to the

    application toolbar.

    Application controls like ALV Grid Control, Tree Controls, and Picture Controls

    must be embedded in a Container Control, which in turn must be connected with the

    screen. Container Controls form the technical connection between a screen and an

    application control. There are different types of container controls. All of these types

    encapsulate basic control functions (for example, scroll bars).

  • 8/3/2019 Object Oriented ABAP Guide

    27/40

    Page 27 of 40

    In the following sections, we will work trough a standard case as an example:

    Positioning an ALV Grid Control in a screen area with a fixed size. To do this, an

    instance of each of the global classes CL_GUI_CUSTOM_CON-

    TAINER and CL_GUI_ALV_GRID must be generated.

    For a basic standard display, it is enough to program the following steps:

    1. Define a custom control area on your screen using the screen painter's graphicalfull screen editor.

    2.

    Generate an instance of the CL_GUI_CUSTOM_CONTAINER class and give theconstructor the name of the custom control area you have defined.

    3. Generate an instance of the class CL_GUI_ALV_GRID and assign the constructorthe reference to the container control instance you have just generated.

    4. Call the SET_TABLE_FOR_FIRST_DISPLAY method of the Grid Control instanceand assign it the internal standard table and application data.

    If this method has a global line type, you can give it the name of this global

    structure. The field catalog will then be set up by the grid control instance itself.

    If the contents of the internal table change during the rest of the program run, it is

    enough to call the method REFRESH_TABLE_DISPLAY in the corresponding dialog

    step, to update the display accordingly.

    Report : 010

    Code listing for: Z_010_ALV_GRID_CONTROL

    Description: EXAMPLE OF ALV GRID CONTROL

    *----------------------------------------------------------------

    * SCREEN 101 : FLOW LOGIC

    *----------------------------------------------------------------

    PROCESS BEFORE OUTPUT.

    MODULE STATUS_0101.

    MODULE GET_DATA.

    MODULE CREATE_OBJECTS.

    MODULE SHOW_ALV.

    PROCESS AFTER INPUT.

    MODULE USER_COMMAND_0101.

    *----------------------------------------------------------------

    * MODULE POOL Z_010_ALV_GRID_CONTROL

    *----------------------------------------------------------------

    INCLUDE Z_010_ALV_GRID_CONTROL_TOP. " Global Data

    INCLUDE Z_010_ALV_GRID_CONTROL_O01. " PBO-Modules

    INCLUDE Z_010_ALV_GRID_CONTROL_I01. " PAI-Modules

  • 8/3/2019 Object Oriented ABAP Guide

    28/40

    Page 28 of 40

    INCLUDE Z_010_ALV_GRID_CONTROL_F01. " FORM-Routines

    *----------------------------------------------------------------------------------

    * TOP INCLUDE Z_010_ALV_GRID_CONTROL_TOP

    *-----------------------------------------------------------------------------------

    REPORT Z_010_ALV_GRID_CONTROL.

    TABLES: SFLIGHT.

    DATA: OK_CODE TYPE SY-UCOMM,

    IT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT,

    R_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

    R_GRID TYPE REF TO CL_GUI_ALV_GRID.

    *-------------------------------------------------------------------------------

    * PBO INCLUDE Z_010_ALV_GRID_CONTROL_O01

    *--------------------------------------------------------------------------------

    *----------------------------------------------------------------

    * Module SHOW_ALV OUTPUT

    *----------------------------------------------------------------

    MODULE SHOW_ALV OUTPUT.

    CHECK OK_CODE IS INITIAL.

    CALL METHOD R_GRID->SET_TABLE_FOR_FIRST_DISPLAY

    EXPORTING

    I_STRUCTURE_NAME = 'SFLIGHT'

    CHANGING

    IT_OUTTAB = IT_SFLIGHT.

    ENDMODULE. " SHOW_ALV OUTPUT

    *----------------------------------------------------------------

    * Module GET_DATA OUTPUT

    *----------------------------------------------------------------

    MODULE GET_DATA OUTPUT.

    CHECK OK_CODE IS INITIAL.

    PERFORM GET_DATA

    USING

    SFLIGHT-CARRID.

  • 8/3/2019 Object Oriented ABAP Guide

    29/40

    Page 29 of 40

    ENDMODULE. " GET_DATA OUTPUT

    *----------------------------------------------------------------

    * Module CREATE_OBJECTS OUTPUT

    *----------------------------------------------------------------

    MODULE CREATE_OBJECTS OUTPUT.

    CHECK OK_CODE IS INITIAL.

    CREATE OBJECT R_CONTAINER

    EXPORTING

    CONTAINER_NAME = 'CC_ALV'.

    CREATE OBJECT R_GRID

    EXPORTING

    I_PARENT = R_CONTAINER.

    ENDMODULE. " CREATE_OBJECTS OUTPUT

    *----------------------------------------------------------------

    * Module STATUS_0101 OUTPUT

    *----------------------------------------------------------------

    MODULE STATUS_0101 OUTPUT.

    SET PF-STATUS 'Z_010_STATUS'.

    "SET TITLEBAR 'xxx'.

    ENDMODULE. " STATUS_0101 OUTPUT

    *----------------------------------------------------------------------------

    * PAI INCLUDE Z_010_ALV_GRID_CONTROL_I01

    *----------------------------------------------------------------------------

    *----------------------------------------------------------------

    * Module USER_COMMAND_0101 INPUT

    *----------------------------------------------------------------

    MODULE USER_COMMAND_0101 INPUT.CASE OK_CODE .

    WHEN 'SEARCH'.

    PERFORM GET_DATA

    USING

    SFLIGHT-CARRID.

  • 8/3/2019 Object Oriented ABAP Guide

    30/40

    Page 30 of 40

    CALL METHOD R_GRID->REFRESH_TABLE_DISPLAY.

    WHEN 'EXIT'.

    LEAVE PROGRAM.

    ENDCASE.

    ENDMODULE. " USER_COMMAND_0101 INPUT

    *-----------------------------------------------------------------------------------

    * FORM INCLUDE Z_010_ALV_GRID_CONTROL_F01

    *-----------------------------------------------------------------------------------

    *----------------------------------------------------------------

    * FORM : GET_DATA

    * Created : 26.03.2008 12:34:09

    *----------------------------------------------------------------

    FORM GET_DATA USING VALUE(P_CARRID) TYPE SFLIGHT-CARRID.

    IF P_CARRID IS INITIAL.

    SELECT * FROM SFLIGHT INTO CORRESPONDING

    FIELDS OF TABLE T_SFLIGHT.

    ELSE.

    SELECT * FROM SFLIGHT INTO CORRESPONDING

    FIELDS OF TABLE IT_SFLIGHT

    WHERE CARRID = P_CARRID.

    ENDIF.

    ENDFORM. "GET_DATA

    ALV GRID CONTROL USING EVENTS

    An ALV Grid Control can react to a user double-clicking the mouse button.

    One possible reaction is that a subsequent processing is triggered in which additional

    information is displayed. The event DOUBLE_CLICK is caught using a handler

    method. A handler method can be either a class method (static method) or an

    object's instance method. If a class method is defined as a handler method, there isno need to instantiate an object of the handling class, to use the method. To create a

    handler object for an event, you must first of all define a class. This class has a

    public method (in the PUBLIC SECTION) that can react to an event. In the handler

    method's implementation, create the source text that will run when you trigger the

    event. The method receives the information delivered by the event from the position

    of the mouse when the user executes a double-click, and in the control example,

    generates an information message that displays the line and field where the mouse

    is clicked.

  • 8/3/2019 Object Oriented ABAP Guide

    31/40

    Page 31 of 40

    Report : 011

    Code listing for: Z_011_ALV_GRID_EVENT

    Description: EXAMPLE OF ALV GRID CONTROL

    *----------------------------------------------------------------

    * SCREEN 101 : FLOW LOGIC

    *----------------------------------------------------------------

    PROCESS BEFORE OUTPUT.

    MODULE STATUS_0101.

    MODULE GET_DATA.

    MODULE CREATE_OBJECTS.

    MODULE SHOW_ALV.

    PROCESS AFTER INPUT.

    MODULE USER_COMMAND_0101.

    *----------------------------------------------------------------

    * MODULE POOL Z_011_ALV_GRID_EVENT

    *----------------------------------------------------------------

    REPORT Z_011_ALV_GRID_EVENT.

    *----------------------------------------------------------------

    * CLASS CL_EVENT_HANDLER DEFINITION

    *----------------------------------------------------------------

    CLASS CL_EVENT_HANDLER DEFINITION.

    PUBLIC SECTION.

    METHODS: ON_DOUBLE_CLICK FOR EVENT

    DOUBLE_CLICK OF CL_GUI_ALV_GRID

    IMPORTING ES_ROW_NO E_COLUMN,

    ON_RIGHT_CLICK FOR EVENT

    RIGHT_CLICK OF CL_GUI_ALV_GRID.

    ENDCLASS. "CL_EVENT_HANDLER DEFINITION

    *----------------------------------------------------------------

    * CLASS CL_EVENT_HANDLER IMPLEMENTATION

  • 8/3/2019 Object Oriented ABAP Guide

    32/40

    Page 32 of 40

    *----------------------------------------------------------------

    CLASS CL_EVENT_HANDLER IMPLEMENTATION.

    METHOD ON_DOUBLE_CLICK.

    DATA: TEXT TYPE STRING,

    ES_ROW_STRING TYPE STRING.

    ES_ROW_STRING = ES_ROW_NO-ROW_ID.

    CONCATENATE 'ROW : ' ES_ROW_STRING 'COLUMN : '

    E_COLUMN-FIELDNAME INTO TEXT SEPARATED BY SPACE.

    MESSAGE TEXT TYPE 'I'.

    ENDMETHOD. "ON_DOUBLE_CLICK

    METHOD ON_RIGHT_CLICK.

    MESSAGE 'RIGHT MOUSE BUTTON HAS CLICKED !!!' TYPE 'I'.

    ENDMETHOD. "ON_RIGHT_CLICK

    ENDCLASS. "CL_EVENT_HANDLER IMPLEMENTATION

    *----------------------------------------------------------------

    * INCLUDE PROGRAMS

    *----------------------------------------------------------------

    INCLUDE Z_011_ALV_GRID_EVENT_TOP. " Global Data

    INCLUDE Z_011_ALV_GRID_EVENT_O01. " PBO-Modules

    INCLUDE Z_011_ALV_GRID_EVENT_I01. " PAI-Modules

    INCLUDE Z_011_ALV_GRID_EVENT_F01. " FORM-Routines

    *----------------------------------------------------------------

    * TOP INCLUDE Z_011_ALV_GRID_EVENT_TOP

    *----------------------------------------------------------------

    *----------------------------------------------------------------

    * TYPES AND DATA DECLARATION

    *----------------------------------------------------------------TABLES: SFLIGHT.

    DATA: OK_CODE TYPE SY-UCOMM,

    IT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT,

    R_HANDLER TYPE REF TO CL_EVENT_HANDLER,

    R_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

  • 8/3/2019 Object Oriented ABAP Guide

    33/40

    Page 33 of 40

    R_GRID TYPE REF TO CL_GUI_ALV_GRID.

    *-------------------------------------------------------------------------

    * PBO INCLUDE Z_011_ALV_GRID_EVENT_O01

    *------------------------------------------------------------------------

    *---------------------------------------------------------------

    * Module SHOW_ALV OUTPUT

    *---------------------------------------------------------------

    MODULE SHOW_ALV OUTPUT.

    CHECK OK_CODE IS INITIAL.

    CALL METHOD R_GRID->SET_TABLE_FOR_FIRST_DISPLAY

    EXPORTING

    I_STRUCTURE_NAME = 'SFLIGHT'

    CHANGING

    IT_OUTTAB = IT_SFLIGHT.

    ENDMODULE. " SHOW_ALV OUTPUT

    *----------------------------------------------------------------

    * Module GET_DATA OUTPUT

    *----------------------------------------------------------------

    MODULE GET_DATA OUTPUT.

    CHECK OK_CODE IS INITIAL.

    PERFORM GET_DATA

    USING

    SFLIGHT-CARRID.

    ENDMODULE. " GET_DATA OUTPUT

    *----------------------------------------------------------------

    * Module CREATE_OBJECTS OUTPUT

    *----------------------------------------------------------------

    MODULE CREATE_OBJECTS OUTPUT.

    IF R_HANDLER IS NOT BOUND. "CHECKS WHETHER A REFERENCE

    "VARIABLE CONTAINS VALID REFERENCE

    CREATE OBJECT R_HANDLER.

    ENDIF.

  • 8/3/2019 Object Oriented ABAP Guide

    34/40

    Page 34 of 40

    IF R_CONTAINER IS NOT BOUND. "CHECKS WHETHER A REFERENCE

    "VARIABLE CONTAINS VALID REFERENCE

    CREATE OBJECT R_CONTAINER

    EXPORTING

    CONTAINER_NAME = 'CC_ALV'.

    ENDIF.

    IF R_GRID IS NOT BOUND.

    CREATE OBJECT R_GRID

    EXPORTING

    I_PARENT = R_CONTAINER.

    SET HANDLER R_HANDLER->ON_DOUBLE_CLICK

    R_HANDLER->ON_RIGHT_CLICK FOR ALL INSTANCES

    ENDIF.

    ENDMODULE. " CREATE_OBJECTS OUTPUT

    *----------------------------------------------------------------

    * Module STATUS_0101 OUTPUT

    *----------------------------------------------------------------

    MODULE STATUS_0101 OUTPUT.

    SET PF-STATUS 'Z_010_STATUS'.

    "SET TITLEBAR 'xxx'.

    ENDMODULE. " STATUS_0101 OUTPUT

    *------------------------------------------------------------------------

    * PAI INCLUDE Z_011_ALV_GRID_EVENT_I01

    *------------------------------------------------------------------------

    *----------------------------------------------------------------

    * Module USER_COMMAND_0101 INPUT

    *----------------------------------------------------------------MODULE USER_COMMAND_0101 INPUT.

    CASE OK_CODE .

    WHEN 'SEARCH'.

    PERFORM GET_DATA

    USING

  • 8/3/2019 Object Oriented ABAP Guide

    35/40

    Page 35 of 40

    SFLIGHT-CARRID.

    CALL METHOD R_GRID->REFRESH_TABLE_DISPLAY.

    WHEN 'EXIT'.

    LEAVE PROGRAM.

    ENDCASE.

    ENDMODULE. " USER_COMMAND_0101 INPUT

    *-----------------------------------------------------------------------------

    * FORM INCLUDE Z_011_ALV_GRID_EVENT_F01

    *-----------------------------------------------------------------------------

    *----------------------------------------------------------------

    * FORM : GET_DATA

    * Created : 26.03.2008 12:34:09

    *----------------------------------------------------------------

    FORM GET_DATA USING VALUE(P_CARRID) TYPE SFLIGHT-CARRID.

    IF P_CARRID IS INITIAL.

    SELECT * FROM SFLIGHT INTO CORRESPONDING

    FIELDS OF TABLE IT_SFLIGHT.

    ELSE.

    SELECT * FROM SFLIGHT INTO CORRESPONDING

    FIELDS OF TABLE IT_SFLIGHT WHERE CARRID = P_CARRID.

    ENDIF.

    ENDFORM. "GET_DATA

    ALV GRID CONTROL USING FIELD CATALOGS

    The internal table with the data to be displayed can have a user-specific line

    type. In order for the proxy instance to satisfactorily format the data it receives

    during the screen output or when a print list is created, it requires relevant

    information known as the field catalog.

    You can have the proxy instance automatically generate the field catalog. For

    line types that are defined in the ABAP Dictionary, you only need to inform the proxy

    instance of the structure's name in the ABAP Dictionary.

    Alternatively, or in addition to this, you can provide the proxy instance with

    this display information using an additional internal table. For the sake of simplicity,

    we will call this additional table a field catalog. The global data type of this internal

    table is LVC_T_FCAT. Its line type is LVC_S_FCAT.

    Typical examples of where you need to transfer a field catalog include:

  • 8/3/2019 Object Oriented ABAP Guide

    36/40

    Page 36 of 40

    The internal table with data has a line type with a dictionary reference, but thedisplay of the column position or heading is different.

    The internal table has (exclusive or additional) columns that are not contained ina dictionary structure.

    For each column in the data table that deviates from an underlying dictionary

    structure or is not available in a dictionary structure, the field catalog must contain a

    line that determines the technical characteristics and other column format

    information.

    Report : 012

    Code listing for: Z_012_ALV_FIELDCATALOG

    Description: EXAMPLE OF ALV USING FIELDCATALOG

    *----------------------------------------------------------------

    * SCREEN 101 : FLOW LOGIC

    *----------------------------------------------------------------

    PROCESS BEFORE OUTPUT.

    MODULE STATUS_0101.

    MODULE GET_DATA.

    MODULE CREATE_OBJECTS.

    MODULE CREATE_FIELDCAT.

    MODULE DISPALY_ALV.

    *----------------------------------------------------------------------

    * MODULE POOL Z_012_ALV_FIELDCATALOG

    *----------------------------------------------------------------------

    INCLUDE Z_012_ALV_FIELDCATALOG_TOP. " Global Data

    INCLUDE Z_012_ALV_FIELDCATALOG_O01. " PBO-Modules

    INCLUDE Z_012_ALV_FIELDCATALOG_I01. " PAI-Modules

    INCLUDE Z_012_ALV_FIELDCATALOG_F01. " FORM-Routines

    *--------------------------------------------------------------------------

    * TOP INCLUDE Z_012_ALV_FIELDCATALOG_TOP

    *---------------------------------------------------------------------------

    REPORT Z_012_ALV_FIELDCATALOG.

    TABLES SPFLI.

    TYPES: BEGIN OF TY_SPFLI,

    CARRID TYPE SPFLI-CARRID,

  • 8/3/2019 Object Oriented ABAP Guide

    37/40

    Page 37 of 40

    CONNID TYPE SPFLI-CONNID,

    COUNTRYFR TYPE SPFLI-COUNTRYFR,

    COUNTRYTO TYPE SPFLI-COUNTRYTO,

    END OF TY_SPFLI.

    DATA: IT_SPFLI TYPE STANDARD TABLE OF TY_SPFLI,

    IT_FIELDCAT TYPE LVC_T_FCAT,

    WA_FIELDCAT LIKE LINE OF IT_FIELDCAT,

    R_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

    R_ALV TYPE REF TO CL_GUI_ALV_GRID.

    *---------------------------------------------------------------------------

    * PBO INCLUDE Z_012_ALV_FIELDCATALOG_O01

    *---------------------------------------------------------------------------

    *----------------------------------------------------------------

    * Module STATUS_0101 OUTPUT

    *----------------------------------------------------------------

    MODULE STATUS_0101 OUTPUT.

    SET PF-STATUS 'Z_012_STATUS'.

    "SET TITLEBAR 'xxx'.

    ENDMODULE. " STATUS_0101 OUTPUT

    *----------------------------------------------------------------

    * Module GET_DATA OUTPUT

    *----------------------------------------------------------------

    MODULE GET_DATA OUTPUT.

    PERFORM GET_DATA.

    ENDMODULE. " GET_DATA OUTPUT

    *----------------------------------------------------------------

    * Module CREATE_OBJECTS OUTPUT

    *----------------------------------------------------------------

    MODULE CREATE_OBJECTS OUTPUT.

    PERFORM CREATE_OBJECTS.

    ENDMODULE. " CREATE_OBJECTS OUTPUT

  • 8/3/2019 Object Oriented ABAP Guide

    38/40

    Page 38 of 40

    *----------------------------------------------------------------

    * Module CREATE_FIELDCAT OUTPUT

    *----------------------------------------------------------------

    MODULE CREATE_FIELDCAT OUTPUT.

    PERFORM CREATE_FIELDCATALOG.

    ENDMODULE. " CREATE_FIELDCAT OUTPUT

    *----------------------------------------------------------------

    * Module DISPALY_ALV OUTPUT

    *----------------------------------------------------------------

    MODULE DISPALY_ALV OUTPUT.

    PERFORM DISPLAY_ALV.

    ENDMODULE. " DISPALY_ALV OUTPUT

    *-----------------------------------------------------------------------------

    * FORM INCLUDE Z_012_ALV_FIELDCATALOG_F01

    *-----------------------------------------------------------------------------

    *----------------------------------------------------------------

    * FORM : GET_DATA

    * Created : 26.03.2008 17:24:39

    *----------------------------------------------------------------

    FORM GET_DATA.

    SELECT CARRID CONNID COUNTRYFR COUNTRYTO FROM SPFLI

    INTO CORRESPONDING FIELDS OF TABLE IT_SPFLI.

    ENDFORM. "GET_DATA

    *----------------------------------------------------------------

    * FORM : CREATE_FIELDCATALOG

    * Created : 26.03.2008 17:30:03

    *----------------------------------------------------------------FORM CREATE_FIELDCATALOG.

    WA_FIELDCAT-FIELDNAME = 'CARRID'.

    WA_FIELDCAT-REF_FIELD = 'CARRID'.

    WA_FIELDCAT-REF_TABLE = 'SPFLI'.

    WA_FIELDCAT-COL_POS = 0.

  • 8/3/2019 Object Oriented ABAP Guide

    39/40

    Page 39 of 40

    WA_FIELDCAT-FIX_COLUMN = 'X'.

    APPEND WA_FIELDCAT to IT_FIELDCAT.

    CLEAR WA_FIELDCAT.

    WA_FIELDCAT-FIELDNAME = 'CONNID'.

    WA_FIELDCAT-REF_FIELD = 'CONNID'.

    WA_FIELDCAT-REF_TABLE = 'SPFLI'.

    WA_FIELDCAT-COL_POS = 1.

    WA_FIELDCAT-FIX_COLUMN = 'X'.

    APPEND WA_FIELDCAT to IT_FIELDCAT.

    CLEAR WA_FIELDCAT.

    WA_FIELDCAT-FIELDNAME = 'COUNTRY FROM'.

    WA_FIELDCAT-REF_FIELD = 'COUNTRYFR'.

    WA_FIELDCAT-REF_TABLE = 'SPFLI'.

    WA_FIELDCAT-COL_POS = 2.

    WA_FIELDCAT-FIX_COLUMN = 'X'.

    APPEND WA_FIELDCAT to IT_FIELDCAT.

    CLEAR WA_FIELDCAT.

    WA_FIELDCAT-FIELDNAME = 'COUNTRY TO'.

    WA_FIELDCAT-REF_FIELD = 'COUNTRYTO'.

    WA_FIELDCAT-REF_TABLE = 'SPFLI'.

    WA_FIELDCAT-COL_POS = 3.

    WA_FIELDCAT-FIX_COLUMN = 'X'.

    APPEND WA_FIELDCAT to IT_FIELDCAT.

    CLEAR WA_FIELDCAT.

    ENDFORM. "CREATE_FIELDCATALOG

    *----------------------------------------------------------------

    * FORM : DISPLAY_ALV

    * Created : 26.03.2008 17:24:19

    *----------------------------------------------------------------

    FORM DISPLAY_ALV.

    CALL METHOD R_ALV->SET_TABLE_FOR_FIRST_DISPLAY

  • 8/3/2019 Object Oriented ABAP Guide

    40/40

    * EXPORTING

    * I_STRUCTURE_NAME = 'SFLIGHT'

    CHANGING

    IT_OUTTAB = IT_SPFLI

    IT_FIELDCATALOG = IT_FIELDCAT.

    ENDFORM. "DISPLAY_ALV

    *----------------------------------------------------------------

    * Form CREATE_OBJECTS

    *----------------------------------------------------------------

    FORM CREATE_OBJECTS .

    CREATE OBJECT R_CONTAINER

    EXPORTING CONTAINER_NAME = 'CC_ALV'.

    CREATE OBJECT R_ALV

    EXPORTING I_PARENT = R_CONTAINER.

    ENDFORM. " CREATE_OBJECTS