Operators in OOP
1. ME Operator in Methods
When you declare a variable of any type in public section of a class, you can use it in any other implementation. A variable can be declared with an initial value in public section. We may declare the variable again inside a method with a different value. When we write the variable inside the method, the system will print the changed value. To reflect the previous value of the variable, we have to use ‘ME’ operator.
In this program, we have declared a public variable text1 and initiated with a value. We have declared the same variable again, but instantiated with different value. Inside the method, we are writing that variable with ‘ME’ operator to get the previously initiated value. We get the changed value by declaring directly.
Example
Report ZMEOperator1.
CLASS class1 Definition.
PUBLIC Section.
Data text1 Type char25 Value 'This is CLASS Attribute'.
Methods method1.
ENDCLASS.
CLASS class1 Implementation.
Method method1.
Data text1 Type char25 Value 'This is METHOD Attribute'.
Write: / ME→text1,
/ text1.
ENDMethod.
ENDCLASS.
Start-Of-Selection.
Data objectx Type Ref To class1.
Create Object objectx.
CALL Method objectx→method1.
CLASS class1 Definition.
PUBLIC Section.
Data text1 Type char25 Value 'This is CLASS Attribute'.
Methods method1.
ENDCLASS.
CLASS class1 Implementation.
Method method1.
Data text1 Type char25 Value 'This is METHOD Attribute'.
Write: / ME→text1,
/ text1.
ENDMethod.
ENDCLASS.
Start-Of-Selection.
Data objectx Type Ref To class1.
Create Object objectx.
CALL Method objectx→method1.
The above code produces the following output −
This is CLASS Attribute
This is METHOD Attribute
This is METHOD Attribute
2. Casting operator
?= is used to type cast an object reference of an inherited class to an object of the super class from which it is derived.
<object reference of parent class> ?= <object reference of inherited class>
Type casting helps you to refer several object references of sub classes whose type is resolved only at run time. The parent class object reference can hold the objects and often there would be a method of parent class which can be used to determine what sub class object the type cast reference is holding at run time.
Static Type and Dynamic Type
Each reference variable has a dynamic type and a static type.
- The dynamic type is defined at run-time of the program (the type is defined when program is running), and is the data type of the data object or the class of the object to which the reference variable points. It determines the components contained in the object. The dynamic type of an initial data reference variable is the predefined generic type data. The dynamic type of an initial object reference variable is the predefined generic type object.
- The static type is set with the declaration of the reference variable. In data references, the static type is either a non-generic data type or the predefined generic type data. In object references, the static type is either a class or an interface, so an object reference can also be referred to as a class reference or an interface reference.
The static type of a reference variable is always less specific or the same as the dynamic type.
Up Cast and Down Cast
In an assignment between reference variables, the target variable adopts the dynamic type of the source variable. An assignment is possible if the static type of the target variable is less specific or the same as the dynamic type of the source variable.
Up Cast
If the static type of the target variable is less specific or the same as the static type of the source variable, assignment is always possible. The name up cast arises from the fact that you are moving upwards within the inheritance space. As the target variable can accept more dynamic types in comparison to the source variable, this assignment is also know as a widening cast. An up cast is possible in all ABAP statements in which the content of a data object is assigned to another data object. This includes, for example, assignments with the normal assignment operator (=), the insertion of rows in internal tables, or passes from actual to formal parameters.
Down Cast
If the static type of the target variable is more specific than the static type of the source variable, you must check at runtime before the assignment is executed, whether it is less specific or the same as the dynamic type of the source variable. The name down cast arises from the fact that you are moving downwards in the inheritance space. Because the target variable can accept fewer dynamic types in comparison to the source variable, this assignment is also known as a narrowing cast. A down cast is only possible using the special assignment operator ?= (casting operator) or the statement MOVE ... ?TO .... If this prerequisite is not met, a handleable exception is raised and the reference variable keeps its original value.