Object Handling

download Object Handling

of 4

Transcript of Object Handling

  • 7/27/2019 Object Handling

    1/4

    PRINT FROM SAP HELP PORTAL

    Document:Object Handling

    URL:http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htm

    Date created:August 07, 2013

    2013 SAP AG or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the expresspermission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and i ts distributors contain proprietary

    software components of other software vendors. National product specifications m ay vary. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for

    informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only

    warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein

    should be construed as constituting an additional warranty. SAP and other SAP products and services mentioned herein as well as thei r respective logos are trademarks or

    registered trademarks of SAP AG in G ermany and other countries. Please see www.sap.com/corporate-en/legal/copyright/index.epx#trademark for additional trademark information

    and notices.

    Note

    This PDF document contains the selected topic and its subtopics (max. 150) in the selected structure.Subtopics from other structures are not included.

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 1 of 4

    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htmhttp://help.sap.com/
  • 7/27/2019 Object Handling

    2/4

    Object Handling

    Objects

    Objects are instances of classes. Each object has a unique identity and its own attributes. All transient objects reside in the context of an internal session

    (memory area of an ABAP program). Persistent objects in the database are not yet available. A class can have any number of objects (instances).

    Object ReferencesTo access an object from an ABAP program, you use only object references. Object references are pointers to objects. In ABAP, they are always contained in

    object reference variables .

    Object Reference Variables

    There are only two types of variable that can contain references in ABAP: object reference variables and data reference variables for data references. Object

    reference variables contain references to objects. An object reference variable is either initial or contains a reference to an existing object. The identity of an object

    depends on its object reference. A reference variable that points to an object knows the identity of that object. The instance-dependent components of an object

    can only be addressed using reference variables that point to the object.

    Users cannot access the identity of the object directly. Reference variables are based on reference semantics. When reference semantics is used and a

    reference variable is assigned to another one, then it is only the address of the object the object reference which is passed and not the attributes of the object.

    Ob ject attributes can themselves also be reference variables .

    Like all variables, you initialize object reference variables using the CLEAR statement. The initial value of a reference variable is a reference that does not point to

    an object.

    Data Types for References

    ABAP contains a predefined data type for references, comparable to the data types for structures or internal tables. The full data type is not defined until the

    declaration in the ABAP program. The data type of an object reference variable determines how the program handles its value (that is, the object reference). There

    are two general types of object reference: class references and interface references (see Interfaces).

    You define class references using the

    ... TYPE REF TO class

    addition in the TYPES orDATAstatement, where class refers to a class. A reference variable with the type class reference is called a class reference variable,

    or class reference for short.

    A class reference crefallows a user to create an instance (object) of the corresponding class. Using

    cref->comp

    the user can access all visible components compof the object which are also contained in the class class.

    Creating Objects

    Now that you defined a class reference variable crefwith reference to a class class, you can now create an instance (object) of the class.

    CREATE OBJECT cref [TYPE class] .

    This statement creates an instance of the class class, and the reference variable crefcontains a reference to the object. You do not need the TYPE class

    addition in this case. This addition is only important in the following two situations: 1. if you use inheritance and want to create an instance of a class class with a

    class reference variable crefwhich does not have the type of the class class, or 2. if you use interfaces and want to create an instance of a class classwith an

    interface reference variable iref. AfterTYPE you can specify the class dynamically as the contents of a field using the standard parenthesis syntax.

    Addressing the Components of Objects

    Programs can only access the instance components of an object using references in object reference variables. This is done using the object component selector

    -> (ref is a reference variable):

    To access an attribute attr: ref->attr

    To call a method meth: CALL METHOD ref->meth

    You can access static components using the class name or the class component selector => as well as the reference variable. It is also possible to address the

    static components of a class before an object has been created.

    To access a static attribute attr: class=>attr

    To call a static method meth: CALL METHOD c lass=>meth

    Note that the properties of instance attributes behave like static components. It is therefore possible to refer in a LIKE addition to the visible attributes of a class

    through the class component selector or through reference variables, without prior creation of an object.

    Each class implic itly contains the reference variable me. In objects, the reference variable mealways contains a reference to the respective object itself and is

    therefore also referred to as the self-reference. Within a class, you can use the self-reference me to access the individual class components:

    To access an attribute attrof your class: me->attr

    To call a method meth of your class: CALL METHOD me->meth

    When you work with attributes of your own class in methods, you do not need to specify a reference variable. The self-reference me is implicitly set by the

    system. Self-references allow an object to give other objects a reference to it. You can also access attributes in methods from within an object even if they are

    obscured by local attributes of the method.

    Creating More Than One Instance of a Class

    In a program, you can create any number of objects from the same class. The objects are fully independent of each other. Each one has its own identity within the

    program and its own attributes. Each CREATE OBJECT statement generates a new object, whose identity is defined by its unique object reference.

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 2 of 4

    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/14/11e70b0c5c11d3b9350000e8353423/content.htm
  • 7/27/2019 Object Handling

    3/4

    Assigning Object References

    You can assign references to different reference variables using the MOVE statement. In this way, you can make the references in several object reference

    variables point to the same object. When you assign a reference to a different reference variable, their types must be either compatible or convertible.

    When you use the MOVE statement or the assignment operator (=) to assign reference variables, the system must be able to recognize in the syntax check

    whether an assignment is possible. The same applies when you pass object reference variables to procedures as parameters. If you write the statement

    cref1 = cref2

    the two class references cref1 and cref2 must have the same type, that is, they must either refer to the same class, or the class of cref1 must be a superclass

    of the class ofcref2. In particular, cref1 can be the predefined empty class OBJECT. The class OBJECT is the superclass of all classes in ABAP Objects

    and does not have any components. It has the same function for reference variables as the data type ANY has for normal variables. Reference variables with the

    type OBJECT can function as containers for passing references. However, you cannot use them to address objects.

    In addition, the following relations with interface reference v ariables can be checked statically.

    iref1 = ir ef2.

    Both interface references must refer to the same interface, or the interface of iref2 must be nested, containing the interface of iref1 as a component.

    iref1 = cr ef1.

    The class of the class reference cref1 or one of its superclasses must implement the interface of the interface reference iref1. The interface can be implemented

    either directly or by using a nested interface containing the interface ofiref1 as a component.

    cref1 = ir ef1.

    In this case, the class ofcref1 must be the class OBJECT, that is, the reference variable cref1 must have the type REF TO OBJECT.

    Casting

    Whenever a static type check is not possible or when the type checks are to be performed at program runtime, you must use the statement

    MOVE ... ?TO ...

    or the casting operator (?= ). The casting assignment replaces the assignment operator (= ). In the MOVE... ? TO statement, or when you use the casting

    assignment, there is no static type check. Instead, the system checks at runtime whether the object reference in the source variable points to an object to whichthe object reference in the target variable can also point. If the assignment is possible, the system makes it, otherwise, the catchable runtime error

    MOVE_CAST_ERROR occurs.

    Syntax rules force you to use casting whenever a static type check is not possible, for example:

    cref1 ?= iref1.

    Here, an interface reference is assigned to a class reference. For the casting to be successful, the object to which irefrpoints must be an object of the same

    class as the class of the class variable cref1 or one of its subclasses..

    Object Lifetime

    An object exists for as long as it is being used in the program. An object is i n use by a program for as long as at least one reference points to it, or at least one

    method of the object is registered as an event handler.

    As soon as there are no more references to an object, and so long as none of its methods are registered as event handlers, it is deleted by the automatic memory

    management (garbage collection). The ID of the object then becomes free, and can be used by a new object.

    See also:Overview Graphic

    Example

    Objects: Overview Graphic

    Objects as Instances of a Class

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 3 of 4

    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/48/ad3779b33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/48/ad3776b33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm
  • 7/27/2019 Object Handling

    4/4

    The above illustration shows a class C1 on the left, with its instances represented in the internal session of an ABAP program on the right. To distinguish them

    from classes, instances are drawn with rounded corners. The instance names above use the same notation as is used for reference variables in the Debugger.

    Objects - Introductory Example

    The following example shows how to create and handle objects of the class counter from the example in the section Classes.

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 4 of 4

    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/48/ad377cb33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw73ehp1/helpdata/en/73/d18759b27011d194ef0000e8353423/content.htm