Instantiate a ABAP Object Dynamically

I would like to get some guidance on Dynamically instantiating objects within ABAP.
I have worked with Java.  Within Java I am able to instantiate a Java object through the use Class.forName (see example below):
className = "com.vz.it.cleansheet.eFine.db.query.WBRDMTWKCNDATABEAN";
try {
     singleton = (Object) Class.forName(classname).newInstance();
     } catch (ClassNotFoundException cnf) {
               System.out.println("SingletonRegistry - Couldn't find class "}
Is there a similar procedure in ABAP OO to enable dynamically instantiating a ABAP object?
I would appreciate any advice.
Thanks,
Rick

You can instantiate an object dynamically,  here is a sample program.  Notice that we are creating an object passed on what class is named in the selection screen.
report zrich_0001.
*       CLASS lcl_car DEFINITION
class lcl_car definition.
  public section.
    data: car type string.
endclass.
*       CLASS lcl_car IMPLEMENTATION
class lcl_car implementation.
endclass.
*       CLASS lcl_truck DEFINITION
class lcl_truck definition.
  public section.
    data: truck type string.
endclass.
*       CLASS lcl_truck IMPLEMENTATION
class lcl_truck implementation.
endclass.
data: r  type ref to object.
parameters: p_class(20) type c default 'LCL_TRUCK'.
start-of-selection.
  create object r type (p_class).
  check sy-subrc = 0.
Regards,
Rich Heilman

Similar Messages

  • Create ABAP object dynamically

    Is it possible to create objects dynamically?
    so that the name of the class is set via parameters and also the variables and which class is the superclass?
    something like:
    fieldsymbols: <a> type any.
    data: data_object type ref to object.
    create new class type data_object.
    data_object-name = 'dynamic'.
    data_object-subclass = 'superclass'.
    create object <a> like class 'dynamic'
    <a>-(method of superclass)(   ) etc...
    It is not ABAP code, but i hope you know what i mean?
    Kindly hear from you!
    Anton Pierhagen

    You can use dynamic reference to instantiate your objects.
    Like:
    data: lo_ref type ref to object.
    create object lo_Ref type ('ZCLASS').
    call method lo_ref->('METH_1').
    But problem in this design would be that your class & method name is hardcoded and it would be only checked at runtime, not at the design time. To overcome, this you can use the [Factory Method Design pattern|http://help-abap.zevolving.com/2011/10/abap-objects-design-patterns-factory-method/].
    Regards,
    Naimesh Patel

  • ABAP OBJECTS: Dynamic Create object

    Hi folks!
    I have a problem... I need to create a dynamic type object with:
    <b>DATA: my_instance TYPE REF TO class1.
    CREATE OBJECT my_instance TYPE (class2).</b>
    <i>* where class1 is a superclass of class2.</i>
    When I do:
    <b>my_instance ?= m_parent.</b>
    <i>* where m_parent is an instance of class1</i>
    My problem is when I want to access to an attribute of the class2, the compiler says that it cannot find the attribute <i>(this is OK, because the attribute is only in the class2).</i>
    My question:
    Is there anyway to access to an atribute of second class when is not in the first class? (i don't want to create the attribute as an attribute of the first class).
    Thanx!!!!

    Hi David,
    When you do the debugging, you are dealing with run-time - i.e., the program is now running and you are just interrupting it at each statement to examine the program state. You will reach the point where the object is already created. That is why you can see all those attributes. But when you comiple, the program is not yet <i>running</i>, so the attributes will be unknown because of the dynamic type specification.
    I think you will have to redesign the program logic. As i had already said in my earlier post, it is not proper to have the attributes specified statically while the class itself is specified dynamically.
    Your situation is somewhat similar to -
    DATA ITAB TYPE TABLE OF SPFLI.
    PERFORM TEST TABLES ITAB.
    FORM TEST TABLES ITAB.
      LOOP AT ITAB.
        WRITE: / ITAB-CARRID.
      ENDLOOP.
    ENDFORM.
    Hope the point is clear.
    Regards,
    Anand Mandalika.

  • Dynamic documents in ABAP Objects (weblog)

    Hi SDNers,
    Do you want to implement the following features in ABAP Screens?
    1. Large font sizes and more colour options than traditional ABAP/4 (There are some limitations also)
    2. ICONS and pictures in different sizes
    3. Texts
    4. Links
    5. Pushbuttons
    6. Input fields
    7. Dropdown list boxes
    8. Tables with row span and with column span
    9. Tables with frames and without frames
    10. Tables with buttons, icons, pictures, input elements and texts in it.
    Then please read the below weblog to incorporate these features...
    <a href="/people/venkata.ramisetti/blog/2005/12/20/dynamic-documents-in-abap-objects">Dynamic Documents in ABAP Objects</a>
    Thanks,
    Ramakrishna

    one limitation which comes to my mind immediately is that you cannot create spool output of the dynamic document.
    Regards
    Raja

  • Calling a mime object in the WebDynPro ABAP code dynamically

    Hi ,
    Can anyone let me know the API to access the mime objects dynamically in the WD ABAP code. I have a text file in the WD ABAP component as a text file.
    I would like to pick the file with the help of API's and read the content of the file using Mime Object API's .
    Can anyone help me out in this.
    Best Regards
    Sid

    Hi,
    do the methods of CL_WD_WEB_ICON help?
    Regards, Heidi

  • ABAP OO, Creating object dynamically

    Hi everybody,
         I’m currently working on some Abap OO examples. Everything is working ok except for the following: Creating objects dynamically.
    This is what I’d like to do:
    Lets say we obtain a bunch of sales orders using a simple select statement and place them in an internal table. For each entry in our internal table, I’d like to generate a new sales order object. This salesorder object is a simple class with a constructor with an Id and some other attributes.
    When this is done, I’d like to append every object to a new object, say basket, so that this basket contains something like an internal table within each record a reference to the salesorder. Is this easy to do? I’m using the example that is described in the sap guide (create object pointer type (class_name) but it looks to me that you still have to declare the reference variables before you can create the object.
    Hmm, I think Java is better suited for a job like this.
    Hope you can help,
    Cheers and Best wishes
    Laurens Steffers
    The Netherlands.

    Hi,
    Its not all that difficult in ABAP as well. The following code is based on Chapter 5, Listing 5.20 from book ABAP Objects.
    What you need is an internal table of type <your sales object> e.g.
    DATA: BEGIN OF obj_sales_order
            sales_order_no LIKE <sales_order_no_type>,
            <...>,
            objref TYPE REF TO <SALES_ORDER_CLASS>
          END OF obj_sales_order,
          reftab LIKE SORTED TABLES OF obj_sales_order
                      WITH UNIQUE KEY sales_order_no
                                      <other key attributes>.
    Now all you need to do is to do a SELECT...ENDSELECT loop, and:
    SELECT ...
    obj_sales_order-sales_order_no = <selection result>
    obj_sales_order-<other key attr> = <selection result>
    READ TABLE reftab INTO obj_sales_order
                      FROM obj_sales_order.
    IF sy-subrc <> 0.
      CREATE OBJECT obj_sales_order-objref
             EXPORTING ...
             EXCEPTIONS ...
      IF sy-subrc <> 0.
        MESSAGE ...
        CONTINUE.
      ELSE.
        INSERT obj_sales_order INTO TABLE reftab.
      ENDIF.
    ENDIF.
    CALL METHOD obj_sales_order-objref-><method>.
    ENDSELECT.
    Hope this help.
    Regards

  • Hook methods in ABAP Objects

    Hello,
    i discovered that method calling in the class-inheritance of ABAP Objects doesn't work similar to other OO languages.
    Example:
    1 Superclass named "Z_CLASS"
    1 Subclass of "Z_CLASS" named "Z_SUB"
    Z_CLASS defines a method "initialize" and has a constructor which calls "initialize".
    Z_SUB redefines the method "initialize" assigning some values to instance-attributes.
    If i instantiate a new "Z_SUB"-object then the contructor of "Z_CLASS" is called which calls ITS OWN "initialize" not the one of the subclass. For comparison: Java would call the initialize of the subclass.
    I have to implement a constructor in every subclass of "Z_CLASS" which calls the super constructor and the own "initialize" after that to solve this problem. This isn't the proper OO way!
    Did i miss something or am i right?
    Thanks in advance,
    Stefan

    Check out this, it might be helpfull...
          INTERFACE I_COUNTER
    INTERFACE i_counter.
      METHODS: initialize.
    ENDINTERFACE.
          CLASS C_COUNTER1 DEFINITION
    CLASS c_counter1 DEFINITION.
      PUBLIC SECTION.
        INTERFACES i_counter.
        METHODS: constructor.
        DATA count TYPE i.
    ENDCLASS.
          CLASS C_COUNTER1 IMPLEMENTATION
    CLASS c_counter1 IMPLEMENTATION.
      METHOD constructor.
        CALL METHOD me->i_counter~initialize.
      ENDMETHOD.
      METHOD i_counter~initialize.
        count = count + 10.
        write:/ 'count from super', count.
      ENDMETHOD.
    ENDCLASS.
          CLASS C_COUNTER2 DEFINITION
    CLASS c_counter2 DEFINITION INHERITING FROM c_counter1.
      PUBLIC SECTION.
        METHODS: constructor,
                 initialize.
        DATA count1 TYPE i.
    ENDCLASS.
          CLASS C_COUNTER2 IMPLEMENTATION
    CLASS c_counter2 IMPLEMENTATION.
      METHOD constructor.
        CALL METHOD super->constructor.
        CALL METHOD me->initialize.
      ENDMETHOD.
      METHOD initialize.
        count = ( count / 10 ).
        write:/ 'count from sub', count.
      ENDMETHOD.
    ENDCLASS.
    DATA: count TYPE REF TO c_counter2.
    START-OF-SELECTION.
    CREATE OBJECT count TYPE c_counter2.

  • How to implement Strategy pattern in ABAP Objects?

    Hello,
    I have a problem where I need to implement different algorithms, depending on the type of input. Example: I have to calculate a Present Value, sometimes with payments in advance, sometimes payment in arrear.
    From documentation and to enhance my ABAP Objects skills, I would like to implement the strategy pattern. It sounds the right solution for the problem.
    Hence I need some help in implementing this pattern in OO. I have some basic OO skills, but still learning.
    Has somebody already implemented this pattern in ABAP OO and can give me some input. Or is there any documentation how to implement it?
    Thanks and regards,
    Tapio

    Keshav has already outlined required logic, so let me fulfill his answer with a snippet
    An Interface
    INTERFACE lif_payment.
      METHODS pay CHANGING c_val TYPE p.
    ENDINTERFACE.
    Payment implementations
    CLASS lcl_payment_1 DEFINITION.
      PUBLIC SECTION.
      INTERFACES lif_payment.
      ALIASES pay for lif_payment~pay.
    ENDCLASS.                 
    CLASS lcl_payment_2 DEFINITION.
      PUBLIC SECTION.
      INTERFACES lif_payment.
      ALIASES pay for lif_payment~pay.
    ENDCLASS.                   
    CLASS lcl_payment_1 IMPLEMENTATION.
      METHOD pay.
        "do something with c_val i.e.
        c_val = c_val - 10.
      ENDMETHOD.                   
    ENDCLASS.                  
    CLASS lcl_payment_2 IMPLEMENTATION.
      METHOD pay.
        "do something else with c_val i.e.
        c_val = c_val + 10.
      ENDMETHOD.  
    Main class which uses strategy pattern
    CLASS lcl_main DEFINITION.
      PUBLIC SECTION.
        "during main object creation you pass which payment you want to use for this object
        METHODS constructor IMPORTING ir_payment TYPE REF TO lif_payment.
        "later on you can change this dynamicaly
        METHODS set_payment IMPORTING ir_payment TYPE REF TO lif_payment.
        METHODS show_payment_val.
        METHODS pay.
      PRIVATE SECTION.
        DATA payment_value TYPE p.
        "reference to your interface whcih you will be working with
        "polimorphically
        DATA mr_payment TYPE REF TO lif_payment.
    ENDCLASS.                  
    CLASS lcl_main IMPLEMENTATION.
      METHOD constructor.
        IF ir_payment IS BOUND.
          me->mr_payment = ir_payment.
        ENDIF.
      ENDMETHOD.                  
      METHOD set_payment.
        IF ir_payment IS BOUND.
          me->mr_payment = ir_payment.
        ENDIF.
      ENDMETHOD.                  
      METHOD show_payment_val.
        WRITE /: 'Payment value is now ', me->payment_value.
      ENDMETHOD.                  
      "hide fact that you are using composition to access pay method
      METHOD pay.
        mr_payment->pay( CHANGING c_val = payment_value ).
      ENDMETHOD.                   ENDCLASS.                  
    Client application
    PARAMETERS pa_pay TYPE c. "1 - first payment, 2 - second
    DATA gr_main TYPE REF TO lcl_main.
    DATA gr_payment TYPE REF TO lif_payment.
    START-OF-SELECTION.
      "client application (which uses stategy pattern)
      CASE pa_pay.
        WHEN 1.
          "create first type of payment
          CREATE OBJECT gr_payment TYPE lcl_payment_1.
        WHEN 2.
          "create second type of payment
          CREATE OBJECT gr_payment TYPE lcl_payment_2.
      ENDCASE.
      "pass payment type to main object
      CREATE OBJECT gr_main
        EXPORTING
          ir_payment = gr_payment.
      gr_main->show_payment_val( ).
      "now client doesn't know which object it is working with
      gr_main->pay( ).
      gr_main->show_payment_val( ).
      "you can also use set_payment method to set payment type dynamically
      "client would see no change
      if pa_pay = 1.
        "now create different payment to set it dynamically
        CREATE OBJECT gr_payment TYPE lcl_payment_2.
        gr_main->set_payment( gr_payment ).
        gr_main->pay( ).
        gr_main->show_payment_val( ).
      endif.
    Regads
    Marcin

  • Major global classes used in abap objects

    Hi,
    I am new to the abap objects and till now i am to understand that the role of the global classes are important in the abap object program. I had tried to search on the net but i was not able to get the desired result.
    I want to know which are the major global classes which are used in the ABAP objects.
    Plzz provide me guidelines for it.

    Ricx,
       It depends on your requirement.
    For alv's we use these global classes. For Webdynpro related alv we use different global classes.
    CL_GUI_ALV_GRID (List Viewer)
    CL_GUI_ALV_GRID_BASE (Basis Class for ALV grid)
    CL_GUI_ALV_TREE (ALV Tree Control)
    CL_GUI_ALV_TREE_SIMPLE (Simple ALV Tree)
    CL_ALV_TABLE_CREATE (Dynamic Creation of ALV Data Table)
    CL_ALV_TREE_BASE (Basis Class ALV Tree Control)
    CL_CK_ALVTREE_NKEY_2_OBJECT (Converter Node Key Object)
    CL_ALV_EVENT_DATA (Changing Data Container for Events)
    CL_ALV_EVENT_TOOLBAR_SET (ALV Context menu)
    Regards,
    Arun.

  • Object casting: confusion in ABAP Objects: Complete Reference book

    Hi,
    During Object Assignments using casting, is a Type Test carried out during the syntax check or at runtime?
    A.5.2.2 (page 1008) of 'ABAP Objects: The Complete Reference' says about Widening Cast: "...you must check at runtime...". However on the next page under A.5.3.2 it says of Widening Cast in Data References: "The syntax check precludes...".
    A.5.4 (page 1010) concerns Assignments between Object Reference Variables, but makes no mention of whether checks are carried out by a syntax check or at runtime.
    Also nowhere does it mention when Type Tests for Narrow casting takes place. Can anyone clear my confusion please? Unfortunatly I don't know enough about this stuff to test by writing some code.
    Thanks.

    William,
    Your questions can be answered by the following rule for object references, which I found in the book "ABAP Objects" by Horst Keller and Sascha Krüger:
    "... that the static type of the target variable must be equal to or more general than the dynamic type of the source variable."
    Here "static type" means the type with which an object reference variable is declared. "Dynamic type" is the type that the object reference variable has at runtime. The dynamic type of an object reference is always more special than its static type, otherwise a runtime error occurs.
    With this rule all your questions can be answered:
    1. The Narrowing Cast is always checked during the syntax check. Example:
    DATA o_ref1 TYPE REF TO object.
    DATA o_ref2 TYPE REF TO class_1.
    o_ref1 = o_ref2.  
    Here the reference o_ref2 has a dynamic type "class_1" or a subclass of it, which is narrower than its static type "class_1", which is narrower than the static type "object" of the reference o_ref1. Therefore, the syntax check says that the assignment is OK.
    2. The Widening Cast is always checked at runtime and requires an assignment using the operator ?=. If you use the operator = in the assignment, a syntax error occurs. Therefore the following  example produces a syntax error (try it yourself):
    DATA o_ref1 TYPE REF TO object.
    DATA o_ref2 TYPE REF TO class_1.
    o_ref2 = o_ref1.  
    The correction for this syntax error is:
    DATA o_ref1 TYPE REF TO object.
    DATA o_ref2 TYPE REF TO class_1.
    o_ref2 ?= o_ref1.
    Now the syntax check is satified, and the correctness of the widening cast is checked at runtime.
    Kind regards,
    Michael Kraemer
    Message was edited by: Michael Kraemer

  • Help In ABAP Object Certification Question

    Hi ABAP Gurus,
       iam going to write certification Exam on Nov 11. i have got some model question from SAP domain for all the topics Except ABAP Objects which is so important...
    Please anybody send me some Example question you guys got from the Certification Exam
    it will be be grateful to me...
    Thanks a lot in advance.
    Prabhu
    SAP Lead Consultant

    Hi,
    Sending ABAP Objects questions is not an option but normally, your concepts are tested for:
    - Classes and objects
    - Inheritance
    - Casting
    - Interfaces
    - Events
    - Global classes and interfaces
    - Exception handling
    - Dynamic programming
    You can use help.sap.com to take a look at these topics one by one. Hope this helps.
    Good luck.

  • Disable/Grey out a row in ABAP Objects

    HI,
    I have a reqmt where in which in the report -
    when check box display BOM is checked only BOM components needs to be displayed in the output but should not allow user to select the row and make changes by pressing anybutton.
    The display rows should get greyed out. How to achieve this functionality thru ABAP objects.
    Thanks

    Hi Namit,
    Open the particular page , and select dynamic navigation (from the right hand side corner Display drop down menu ) .
    U will get all the links which r there in the detailed navigation.
    and uncheck the visible property .
    Surekha.

  • RE:ABAP objects

    Hi,
    What do u mean by a CONSTRUCTOR concept in OOPS ABAP....
    Thanks in advance,
    Alex.

    Hi,
    Constructors are special PUBLIC methods that are triggered when an object is instantiated from a class. They are necessary when you want to set the initial state of an object dynamically.
    Like normal methods, there are two types of constructor - instance constructors and static constructors.
    To use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section.
    Constructors must always be declared in the PUBLIC section of a class.
    Instance Constructor
    Executed once for each instance.
    Called automatically, immediately after the CREATE OBJECT statement.
    Can contain an interface with IMPORTING parameters and EXCEPTIONS , but cannot have any EXPORTING/CHANGING/RETURNING parameters .
    The interfaces are defined using the same syntax as for normal methods in the METHODS statement. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement .
    Static Constuctor:
    Static methods, declared as  CLASS-METHODS : CLASS_CONSTRUCTOR  in the public section of the class definition and are also implemented in the implementation part.
    Has  no interface parameters and cannot trigger exceptions. 
    Executed once in each program. It is called automatically for the class before it is accessed for the first time - that is, before one of the following actions:
    CREATE OBJECT obj  from the class.
    Call a static method : [CALL METHOD] class=>meth.
    Registering a static event handler method using SET HANDLER class=>meth for obj.
    Registering an event handler method for a static event of the class class.
    Addressing a static attribute with class=>a.
    Exception:-
    The static constructor is always called immediately before the action is executed, with one exception:
    If the first access to the class is to address a static attribute, the static constructor is executed at the beginning of the processing block (dialog module, event block, procedure) in which the access occurs.
    Caution:-
    The static constructor must not access its own class. Otherwise a runtime error will occur.
    The point at which the static constructor is executed has not yet been finalized, and it may yet be changed. SAP only guarantees that it will be executed before the class is accessed for the first time. It is  recommended not to write programs that require the static constructor to be executed at the beginning of a processing block.
    If it is helpful rewards points.
    Regards
    Pratap.M

  • ABAP Objects with Workflows / Classes and Instances

    Hello,
    I am currently designing a workflow using an ABAP-Objects. So far I have been been able to get my Workflow to run with my class, but I have a couple of problems:
    - I am using the Function 'SAP_WAPI_START_WORKFLOW' to start other subflows, which enables me to decide which subflow to start at runtime. All of the subflows have standart importing-parameters in their containers, such as the key of my class. In each workflow I instantiate my class using a self-written method, which checks the table T_INSTANCES in my object, and then either returns the object reference to an existing instance or creates a new one. Obviously all of the subflows that I call from my main workflow should be able to find the instance. As far as I can see in their protocolls, this happens without any problems. The problem starts when I make changes to the instance. For example the changing of attributes (with setter methods) seems not to work. After the subflows are finished, in my main workflow, I do not see (with getter methods) any changes that has been made to the object. Is local persistence really limited to one workflow ?
    - My second problem is basically about the workflow container in workflow protocoll. In the same workflow, I can change the attributes of my object. Nevertheless, the protocoll always show the initial attribute, even though, my task with the getter-method returns the new value of the attribute.
    I appreciate any help and thanks a lot in advance.

    Hello Pauls,
    Thank you for your answer. I think we are misunderstanding each other. The problem occurs (I think) because my class is not a singleton class. Or am I mistaken ?
    When I directly start a subflow from my main workflow, then the instance that I have created in my main workflow is also visible to the subflow. As well as the static table which actually keeps track of the instances. So, in this case the subflows finds the instance and then can use the object as is.
    When I start a subflow from my main workflow using the function I mentioned above, then even though the same object key is used, there is a new instance. And the static table (I assume that you mean a static variable from type table, when you say "class table") is completely empty. In this case, my "new" instance is created which overwrites every attribute that I have set in the main workflow, before I started the subflow. More interestingly, my main workflow instantiates another new object, as soon as the subflow has finished. (I am using an event to wait for the subflow to finish.)
    On the other hand, I am not quite sure that I understood your approach with refresh and how it could help me. This method is not well documented anywhere, and all of the examples that I have found are about "leave it empty"
    As far as I understood, this method is called by the workflow between the steps, when an object is used. I slowly start to think that I need advanced information about Workflows and Memory Management.
    Thanks a lot again. Apparently, I am the only person who came across such a problem
    Greetz
    G.Fendoglu

  • Abap Objects doubts.

    Hi Experts.
    Plz give me some details for the following questions.
    1. what is abstract class.
    2. give me some example program using inheritance.
    3. Some sample programs using events.
    4. Narrow casting & wide casting.. And describe the use of it.
    5. How to declare internal table in ABAP OOPS.
    6. What is the use of table type.?
    7. What is instance.?
    8. Explain friend class.
    Thank in advance.
    Points will be given for the answers.
    regards,
    J.Joe

    HI
    <b>1. what is abstract class.</b>
    Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract) subclasses, for example to define a uniform interface.
    Classes with at least one abstract method are themselves abstract.
    Static methods and constructors cannot be abstract.
    You can specify the class of the instance to be created explicitly: CREATE OBJECT <RefToAbstractClass> TYPE <NonAbstractSubclassName>.
    Abstarct classes themselves can’t be instantiated ( althrough their subclasses can)
    Reference to abstract classes can refer to instance of subclass
    Abstract (instance) methods are difined in the class , but not implemented
    They must be redefined in subclasses
    CLASS LC1 DEFINAITION ABSTARCT
    PUBLIC SECTION
    METHODS ESTIMATE ABSTARCT IMPORTING…
    ENDCLASS.
    <b>2. give me some example program using inheritance.</b>
    The following simple example shows the principle of inheritance within ABAP Objects. It is based on the Simple Introduction to Classes. A new class counter_ten inherits from the existing class counter.
    REPORT demo_inheritance.
    CLASS counter DEFINITION.
      PUBLIC SECTION.
        METHODS: set IMPORTING value(set_value) TYPE i,
                 increment,
                 get EXPORTING value(get_value) TYPE i.
       PROTECTED SECTION .
        DATA count TYPE i.
    ENDCLASS.
    CLASS counter IMPLEMENTATION.
      METHOD set.
        count = set_value.
      ENDMETHOD.
      METHOD increment.
        ADD 1 TO count.
      ENDMETHOD.
      METHOD get.
        get_value = count.
      ENDMETHOD.
    ENDCLASS.
    CLASS counter_ten DEFINITION INHERITING FROM counter.
      PUBLIC SECTION.
        METHODS increment REDEFINITION .
        DATA count_ten.
    ENDCLASS.
    CLASS counter_ten IMPLEMENTATION.
      METHOD increment.
        DATA modulo TYPE I.
         CALL METHOD super->increment .
        write / count.
        modulo = count mod 10.
        IF modulo = 0.
          count_ten = count_ten + 1.
          write count_ten.
        ENDIF.
      ENDMETHOD.
    ENDCLASS.
    DATA: count TYPE REF TO counter,
          number TYPE i VALUE 5.
    START-OF-SELECTION.
       CREATE OBJECT count TYPE counter_ten .
      CALL METHOD count->set EXPORTING set_value = number.
      DO 20 TIMES.
        CALL METHOD count->increment.
      ENDDO.
    The class COUNTER_TEN is derived from COUNTER. It redefines the method INCREMENT. To do this, you must change the visibility of the COUNT attribute from PRIVATE to PROTECTED. The redefined method calls the obscured method of the superclass using the pseudoreference SUPER->. The redefined method is a specialization of the inherited method.
    The example instantiates the subclass. The reference variable pointing to it has the type of the superclass. When the INCREMENT method is called using the superclass reference, the system executes the redefined method from the subclass.
    <b>3. Some sample programs using events.</b>
    Events in ABAP Objects - Example
    The following example shows how to declare, call, and handle events in ABAP Objects.
    Overview
    This object works with the interactive list displayed below. Each user interaction triggers an event in ABAP Objects. The list and its data is created in the class C_LIST. There is a class STATUS for processing user actions. It triggers an event BUTTON_CLICKED in the AT USER-COMMAND event. The event is handled in the class C_LIST. It contains an object of the class C_SHIP or C_TRUCK for each line of the list. Both of these classes implement the interface I_VEHICLE. Whenever the speed of one of these objects changes, the event SPEED_CHANGE is triggered. The class C_LIST reacts to this and updates the list.
    Constraints
    The ABAP statements used for list processing are not yet fully available in ABAP Objects. However, to produce a simple test output, you can use the following statements:
    WRITE [AT] /<offset>(<length>) <f>
    ULINE
    SKIP
    NEW-LINE
    Note: The behavior of formatting and interactive list functions in their current state are not guaranteed. Incompatible changes could occur in a future release.
    Declarations
    This example is implemented using local interfaces and classes. Below are the declarations of the interfaces and classes:
    Interface and Class declarations
    INTERFACE I_VEHICLE.
      DATA     MAX_SPEED TYPE I.
      EVENTS SPEED_CHANGE EXPORTING VALUE(NEW_SPEED) TYPE I.
      METHODS: DRIVE,
               STOP.
    ENDINTERFACE.
    CLASS C_SHIP DEFINITION.
      PUBLIC SECTION.
      METHODS CONSTRUCTOR.
      INTERFACES I_VEHICLE.
      PRIVATE SECTION.
      ALIASES MAX FOR I_VEHICLE~MAX_SPEED.
      DATA SHIP_SPEED TYPE I.
    ENDCLASS.
    CLASS C_TRUCK DEFINITION.
      PUBLIC SECTION.
      METHODS CONSTRUCTOR.
      INTERFACES I_VEHICLE.
      PRIVATE SECTION.
      ALIASES MAX FOR I_VEHICLE~MAX_SPEED.
      DATA TRUCK_SPEED TYPE I.
    ENDCLASS.
    CLASS STATUS DEFINITION.
      PUBLIC SECTION.
       CLASS-EVENTS BUTTON_CLICKED EXPORTING VALUE(FCODE) LIKE SY-UCOMM.
      CLASS-METHODS: CLASS_CONSTRUCTOR,
                    USER_ACTION.
    ENDCLASS.
    CLASS C_LIST DEFINITION.
      PUBLIC SECTION.
      METHODS: FCODE_HANDLER FOR EVENT BUTTON_CLICKED OF STATUS
                                 IMPORTING FCODE,
               LIST_CHANGE   FOR EVENT SPEED_CHANGE OF I_VEHICLE
                                 IMPORTING NEW_SPEED,
               LIST_OUTPUT.
      PRIVATE SECTION.
      DATA: ID TYPE I,
            REF_SHIP  TYPE REF TO C_SHIP,
            REF_TRUCK TYPE REF TO C_TRUCK,
            BEGIN OF LINE,
              ID TYPE I,
              FLAG,
              IREF  TYPE REF TO I_VEHICLE,
              SPEED TYPE I,
            END OF LINE,
            LIST LIKE SORTED TABLE OF LINE WITH UNIQUE KEY ID.
    ENDCLASS.
    The following events are declared in the example:
    The instance event SPEED_CHANGE in the interface I_VEHICLE
    The static event BUTTON_CLICKED in the class STATUS.
    The class C_LIST contains event handler methods for both events.
    Note that the class STATUS does not have any attributes, and therefore only works with static methods and events.
    Implementations
    Below are the implementations of the methods of the above classes:
    Implementations
    CLASS C_SHIP IMPLEMENTATION.
      METHOD CONSTRUCTOR.
        MAX = 30.
      ENDMETHOD.
      METHOD I_VEHICLE~DRIVE.
        CHECK SHIP_SPEED < MAX.
        SHIP_SPEED = SHIP_SPEED + 10.
        RAISE EVENT I_VEHICLE~SPEED_CHANGE
                    EXPORTING NEW_SPEED = SHIP_SPEED.
      ENDMETHOD.
      METHOD I_VEHICLE~STOP.
        CHECK SHIP_SPEED > 0.
        SHIP_SPEED = 0.
        RAISE EVENT I_VEHICLE~SPEED_CHANGE
                    EXPORTING NEW_SPEED = SHIP_SPEED.
      ENDMETHOD.
    ENDCLASS.
    CLASS C_TRUCK IMPLEMENTATION.
      METHOD CONSTRUCTOR.
        MAX = 150.
      ENDMETHOD.
      METHOD I_VEHICLE~DRIVE.
        CHECK TRUCK_SPEED < MAX.
        TRUCK_SPEED = TRUCK_SPEED + 50.
        RAISE EVENT I_VEHICLE~SPEED_CHANGE
                    EXPORTING NEW_SPEED = TRUCK_SPEED.
      ENDMETHOD.
      METHOD I_VEHICLE~STOP.
        CHECK TRUCK_SPEED > 0.
        TRUCK_SPEED = 0.
        RAISE EVENT I_VEHICLE~SPEED_CHANGE
                    EXPORTING NEW_SPEED = TRUCK_SPEED.
      ENDMETHOD.
    ENDCLASS.
    CLASS STATUS IMPLEMENTATION.
      METHOD CLASS_CONSTRUCTOR.
        SET PF-STATUS 'VEHICLE'.
        WRITE 'Click a button!'.
      ENDMETHOD.
      METHOD USER_ACTION.
        RAISE EVENT BUTTON_CLICKED EXPORTING FCODE = SY-UCOMM.
      ENDMETHOD.
    ENDCLASS.
    CLASS C_LIST IMPLEMENTATION.
      METHOD FCODE_HANDLER .
        CLEAR LINE.
        CASE FCODE.
          WHEN 'CREA_SHIP'.
            ID = ID + 1.
            CREATE OBJECT REF_SHIP.
            LINE-ID = ID.
            LINE-FLAG = 'C'.
            LINE-IREF = REF_SHIP.
            APPEND LINE TO LIST.
          WHEN 'CREA_TRUCK'.
            ID = ID + 1.
            CREATE OBJECT REF_TRUCK.
            LINE-ID = ID.
            LINE-FLAG = 'T'.
            LINE-IREF = REF_TRUCK.
            APPEND LINE TO LIST.
          WHEN 'DRIVE'.
            CHECK SY-LILLI > 0.
            READ TABLE LIST INDEX SY-LILLI INTO LINE.
            CALL METHOD LINE-IREF->DRIVE.
          WHEN 'STOP'.
            LOOP AT LIST INTO LINE.
              CALL METHOD LINE-IREF->STOP.
            ENDLOOP.
          WHEN 'CANCEL'.
            LEAVE PROGRAM.
        ENDCASE.
        CALL METHOD LIST_OUTPUT.
      ENDMETHOD.
      METHOD LIST_CHANGE .
        LINE-SPEED = NEW_SPEED.
        MODIFY TABLE LIST FROM LINE.
      ENDMETHOD.
      METHOD LIST_OUTPUT.
        SY-LSIND = 0.
        SET TITLEBAR 'TIT'.
        LOOP AT LIST INTO LINE.
          IF LINE-FLAG = 'C'.
            WRITE / ICON_WS_SHIP AS ICON.
          ELSEIF LINE-FLAG = 'T'.
            WRITE / ICON_WS_TRUCK AS ICON.
          ENDIF.
          WRITE: 'Speed = ', LINE-SPEED.
        ENDLOOP.
      ENDMETHOD.
    ENDCLASS.
    The static method USER_ACTION of the class STATUS triggers the static event BUTTON_CLICKED. The instance methods I_VEHICLEDRIVE and I_VEHICLESTOP trigger the instance event I_VEHICLE~SPEED_CHANGE in the classes C_SHIP and C_TRUCK.
    Using the Classes in a Program
    The following program uses the above classes:
    REPORT OO_EVENTS_DEMO NO STANDARD PAGE HEADING.
    Global data of program
    DATA LIST TYPE REF TO C_LIST.
    Program events
    START-OF-SELECTION.
      CREATE OBJECT LIST.
      SET HANDLER: LIST->FCODE_HANDLER,
                  LIST->LIST_CHANGE FOR ALL INSTANCES.
    AT USER-COMMAND.
      CALL METHOD STATUS=>USER_ACTION.
    The program creates an object of the class C_LIST and registers the event handler method FCODE_HANDLER of the object for the class event BUTTON_CLICKED, and the event handler method LIST_CHANGE for the event SPEED_CHANGE of all instances that implement the interface I_VEHICLE.
    <b>4. Narrow casting & wide casting.. And describe the use of it.</b>
    <b>Narrowing Cast</b>
    The assignment of a subclass instance to a reference variable of the type "reference to superclass" is described as a narrowing cast, because you are switching from a more detailed view to a one with less detail.
    Description up-cast is also used.
    <b>use</b>
    A user who is not interested in the finer points of cars, trucks, and busses (but only, for example, in the fuel consumption and tank gauge) does not need to know about them. This user only wants and needs to work with (references to) the lcl_vehicle class. However, in order to allow the user to work with cars, busses, or trucks, you generally need a narrowing cast.
    <b>Widening cast</b>
    The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be checked statically, only at runtime. The Cast Operator ?= (or the equivalent MOVE ... ?TO … ) must be used to make this visible.
    It changes from a less detailed view to one with more detail.
    <b>use</b>
    The client, the car rental company  wants to execute a function for specific vehicles form the list (vehicle_list).  For example, the client wants to ascertain the truck with the largest cargo capacity.  However, not all vehicles are in the trucks list, it also includes references to cars and busses.
    <b>5. How to declare internal table in ABAP OOPS.</b>
    BEGIN OF LINE,
              ID TYPE I,
              FLAG,
              IREF  TYPE REF TO I_VEHICLE,
              SPEED TYPE I,
            END OF LINE,
    <b>6. What is the use of table type.?</b>
    Please check the SAP help , as all these things are provided in detail in it .
    In case you find it difficult to find it or search for it here is the link
    http://help.sap.com/saphelp_nw04/helpdata/en/90/8d7304b1af11d194f600a0c929b3c3/frameset.htm
    <b>7. What is instance.?</b>
    Instance methods are called using CALL METHOD <reference>-><instance_method>.
    Static methods (also referred to as class methods) are called using CALL METHOD <classname>=><class_method>.
    If you are calling a static method from within the class, you can omit the class name.
    You access instance attributes using <instance>-><instance_attribute>
    <b>8. Explain friend class.</b>
    In rare cases, classes have to work together so closely that they need access to their protected and private components. To avoid making these components available to all users, there is the concept of friendship between classes.
    To do this you use the FRIENDS additions to the CLASS statement, in which all classes and interfaces that are to be provided friendship are listed
    In principle, providing friendship is one-sided: A class providing friendship is not automatically a friend of its friends. If a class providing friendship wants to access the non-public components of a friend, this friend has to explicitly provide friendship to it.
    Classes that inherit from friends and interfaces that contain a friend as a component interface also become friends. However, providing friendship, unlike the attribute of being a friend, is not inherited. A friend of a superclass is therefore not automatically a friend of its subclasses.
    <b>Reward if usefull</b>

Maybe you are looking for

  • GL Account Balance ( BAPI)

    Hi Guri, Do we have any BAPI to get GL account Balances by passing           Company Code,           Fiscal Year           Period So we can get total list of gl with balance for above year and period. Regards, Venkat

  • Mac Pro sudden shutdown

    Hello, anyone with a similar problem? I found related threads but in most reports nothing was solved. My MacPro is completely unusable now. Description: After initial startup the machine runs for 20 minutes (+/-), shuts down without warning then rebo

  • New HDR Image darker than preview

    Today I was using Adobe Bridge to combine sets of 3 photos with different exposures to create HDR images and sending them to Photoshop. So far today I made 20+ such images and successfully saved them into PSDs. I just ran into an odd problem. After c

  • Report Header Monthwise

    hello Experts, I am working on  3.5 BW. we are uploading some data monthwise into BW from application server .the data is qty and value for last 3 months. we have mada a report on that cube  but the user is asking to chane the headers of the columns

  • Installing adobe flash player to ipad2

    I need step by step instructions for downloading adobe flash player to ipad 2