Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

What is Static refrence and Dynamic reference in Inheritance

Former Member
0 Likes
1,906

Hi all,

Can any one explain what is static and dynamic type reference variable in OOABAP. I didnt uderstand this statement can anyone explain this with an example.

obj1 type ref to class1

obj2 type ref to class2.

create object obj2.

obj1 ?= obj2. ****what does this statement mean.

Thanks,

Sam.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,124

hi,

The static type is the type (class or interface) with which the reference variable has been typed.

DATA : oref type ref to c1. “Static type of oref is pointing to class c1

The dynamic type is the class of the object to which the reference in a reference variable points.

CREATE OBJECT OREF . “Dynamic type also points to c1

CREATE OBJECT OREF TYPE C2. “Dynamic type points to subclass c2 of C1

If the static and dynamic types of a reference variable are different, the static type should always be more general than the dynamic type.

DATA:o_ref1 TYPE REF TO object,
	o_ref2 TYPE REF TO class.
o_ref2 ?= o_ref1.
CATH SYSTEM-EXCEPTIONS move_cast_error = 4.
	o_ref2 ?= o_ref1.
ENDCATCH.

"object(most general class) is the base class to all the classes and all other class(special classes) are subclasses of object.

In some cases, you may wish to make an assignment in which the static type of the target variable is less general than the static type of the source variable. This is known as a widening cast.

The result of the assignment must still adhere to the rule that the static type of the target variable must be the same or more general than the dynamic type of the source variable.

However, this can only be checked during runtime. To avoid the check on static type, use the special casting operator “?=“ and catch the potential runtime error move_cast_error

Hope this explaination helps.

Regards,

Richa.

Hi all,

Can any one explain what is static and dynamic type reference variable in OOABAP. I didnt uderstand this statement can anyone explain this with an example.

obj1 type ref to class1

obj2 type ref to class2.

create object obj2.

obj1 ?= obj2. ****what does this statement mean.

Thanks,

Sam.

3 REPLIES 3
Read only

Former Member
0 Likes
1,125

hi,

The static type is the type (class or interface) with which the reference variable has been typed.

DATA : oref type ref to c1. “Static type of oref is pointing to class c1

The dynamic type is the class of the object to which the reference in a reference variable points.

CREATE OBJECT OREF . “Dynamic type also points to c1

CREATE OBJECT OREF TYPE C2. “Dynamic type points to subclass c2 of C1

If the static and dynamic types of a reference variable are different, the static type should always be more general than the dynamic type.

DATA:o_ref1 TYPE REF TO object,
	o_ref2 TYPE REF TO class.
o_ref2 ?= o_ref1.
CATH SYSTEM-EXCEPTIONS move_cast_error = 4.
	o_ref2 ?= o_ref1.
ENDCATCH.

"object(most general class) is the base class to all the classes and all other class(special classes) are subclasses of object.

In some cases, you may wish to make an assignment in which the static type of the target variable is less general than the static type of the source variable. This is known as a widening cast.

The result of the assignment must still adhere to the rule that the static type of the target variable must be the same or more general than the dynamic type of the source variable.

However, this can only be checked during runtime. To avoid the check on static type, use the special casting operator “?=“ and catch the potential runtime error move_cast_error

Hope this explaination helps.

Regards,

Richa.

Read only

Former Member
0 Likes
1,124

Thanks Richa .Now the concept is clear to me.

Regards,

sam.

Read only

Former Member
0 Likes
1,124

hi

good

go through this link,might help you to solve your problem

http://www.sappressbooks.com/downloads/h958_preview.pdf

thanks

mrutyun^