08-31-2007 12:59 PM
Hi,
I am writing below a small code which says Deffered definition of class.
Can anyone tell me what is the main objective of going for deffered defintion .
1.6 Deferred Definition of a Class
Theme This program will demonstrate how one can refer to a class without defining the class before that point. But, the class has to be defined later on.
Program description In this program , class C1 has an interface reference O2 declared with reference to class C2. But, before that, class C2 is not defined. It is defined later with a single public attribute , NUM .
This demonstrates the theme.
In the main section of the program, object : OBJ1 is created from class C1.
Then, an object is created from the reference variable O2 in class C1. Finally, the attribute num of that object is displayed.
Dump report ysubdel1.
<code>
CLASS C2 DEFINITION DEFERRED.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2.
ENDCLASS.
CLASS C2 DEFINITION.
public section.
data : num type i value 5.
ENDCLASS.
start-of-selection.
data : obj1 type ref to C1.
CREATE OBJECT obj1.
create object obj1->o2.
write:/5 obj1->o2->num .
</code>
Thanks,
satya
08-31-2007 1:13 PM
Hi satya,
it's <b>very</b> useful when you have 2 classes, one referring to each other. If there was no deferred definition, you would have a syntax check in the definition of the first class. By deferring, you are telling the compiler: "OK, here is class C1, but I won't declared it completely because it refers to class C2, which is declared afterwards".
For example,
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2. "<-- syntax error!!! C2 is not yet defined
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
DATA O1 TYPE REF TO C1.
ENDCLASS.
But
CLASS C2 DEFINITION DEFERRED.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2. "<-- OK!!! C2 is yet defined (deferred)
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
DATA O1 TYPE REF TO C1.
ENDCLASS.
I hope it helps. Best regards,
Alvaro
08-31-2007 1:40 PM
Hi Alvaro
Thanks for replying i understood ur answer. But anyways we are defining class c2 afterwards right. so wht is the reality of use of this type of application . i mean that will it hold any improtance in real time work.
I have that doubt.
Thanks again.
Satya
08-31-2007 1:44 PM
Hi Satya,
well, if you're talking about performance, or behaviour, it's the same as if it were not deferred. It's just a technique to solve a pitfall when two classes refer one to each other. In any other sense, the functionality is exactly the same as usual.
I hope this explanation is clear enough. If not, please answer back with your doubts. Best regards,
Alvaro