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).
To 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.
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. Object 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.
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 or DATAstatement, 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 cref allows a user to create an instance (object) of the corresponding class. Using
the user can access all visible components compof the object which are also contained in the class class.
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 cref which 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. After TYPE you can specify the class dynamically as the contents of a field using the standard parenthesis syntax.
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 class=>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 implicitly 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 attr of 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.
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.
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 of cref2. 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 variables can be checked statically.
iref1 = iref2.
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 = cref1.
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 of iref1 as a component.
cref1 = iref1.
In this case, the class of cref1 must be the class OBJECT, that is, the reference variable cref1 must have the type REF TO OBJECT.
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 which the 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..
An object exists for as long as it is being used in the program. An object is in 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.