‎2007 Feb 08 11:57 PM
hi,
may i know the following from gurus. thanks in advance.
1) may i know if in the class has no constructor method then it is something like function already? i was told constructor is just declaration and not peculiar to object. but to me, if no constructor, then the private/public/protected variable for instance will only have initial value. if that is so, then to me, its like function and not much difference besides object is multiple instance whereas function only 1 instance. also if all instance have initial value for its private/public/protected variable, then all instances are the same.
for example, if class has constructor and i have 2 instances of the class, my first instance has variable count with value 10 then my second instance has value 30. from here we can see the difference between instance or object.
i thought that is the beauty of abap object as value also wrapped in the instance(object).
2) may i know if constructor important to abap object?
3) somebody told me that the beauty of oo is the "."
but i don't know how abap is using the "." like below example. can have the advice?
e.g.
customer.new()
customer.setNo()
customer.setName()
customer.save()
‎2007 Feb 09 12:28 AM
I think you may be a little confused about constructor. You can think of a constructor, as a initialization method. Meaning, that when you say CREATE OBJECT, the CONSTRUCTOR is the first method which is fired by the CREATE OBJECT statement. You do not need a CONSTRUCTOR in order to create an instance(object) of a class.
For example, this program has no CONSTRUCTOR, there for it only creates a instance of the class, nothing more, nothing less.
REPORT zrich_0001.
*----------------------------------------------------------------------*
* CLASS lcl_app DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
DATA: value TYPE i.
METHODS: set_value IMPORTING im_value TYPE i.
ENDCLASS. "lcl_app DEFINITION
DATA: o_app1 TYPE REF TO lcl_app.
DATA: o_app2 TYPE REF TO lcl_app.
CREATE OBJECT o_app1.
o_app1->set_value( 10 ).
write:/ o_app1->value.
CREATE OBJECT o_app2.
o_app2->set_value( 20 ).
write:/ o_app2->value.
*----------------------------------------------------------------------*
* CLASS lcl_app IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_app IMPLEMENTATION.
METHOD set_value.
value = im_value.
ENDMETHOD. "set_value
ENDCLASS. "lcl_app IMPLEMENTATION
Notice that I have a method called SET_VALUE. This method will set the value of the instance of the object which was created by the CREATE OBJECT statement. You could fill the VALUE directly with the CREAT OBJECT statement, this will fire the CONSTRUCTOR and fill the value. So we have to modify the class a bit.
REPORT zrich_0001.
*----------------------------------------------------------------------*
* CLASS lcl_app DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
DATA: value TYPE i.
METHODS: constructor IMPORTING im_value TYPE i.
ENDCLASS. "lcl_app DEFINITION
DATA: o_app1 TYPE REF TO lcl_app.
DATA: o_app2 TYPE REF TO lcl_app.
CREATE OBJECT o_app1
EXPORTING im_value = 10.
WRITE:/ o_app1->value.
CREATE OBJECT o_app2
EXPORTING im_value = 20.
WRITE:/ o_app2->value.
*----------------------------------------------------------------------*
* CLASS lcl_app IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_app IMPLEMENTATION.
METHOD CONSTRUCTOR.
value = im_value.
ENDMETHOD. "set_value
ENDCLASS. "lcl_app IMPLEMENTATION
Lastly, the "." period is a separator between the class or object and the method name. The period is used in the java language as well as others. In ABAP the separator is -> for instance methods and attributes, and => for static methods and attributes.
Hope this helps.
REgards,
RIch Heilman
‎2007 Feb 09 2:26 AM
hi rich,
i tested and found out
1) if create object with exporting addition, it looks for constructor method. may i know why?
CREATE OBJECT o_app1
EXPORTING im_value = 10.
2) why the sample with constructor shows nothing even has write?
thanks
‎2007 Feb 09 2:34 AM
If a CONSTRUCTOR method exists for the class, it will be fired when CREATE OBJECT is executed regardless if the CONSTRUCTOR has a signature(exporting, importing parameters)
2) Not sure what you mean, as that example works fine in my system, it simply outputs the values 10 and 20.
Regards,
RIch Heilman
‎2007 Feb 09 2:51 AM
hi rich,
1) when i change the method name to other than constructor, i will get the following when compile if have the exporting addition.
For "LCL_APP", no CONSTRUCTOR has been defined.
2) your first example can show the output. just need to add the call method as your sample missing. but second example not showing anything.
thanks
‎2007 Feb 09 3:09 AM
1) That's right, if you have parameters in the CREATE OBJECT statement, then you must have a constructor, but if there are no parameters, and there is a CONTRUCTOR, it will be fired at CREATE OBJECT.
For example....
REPORT zrich_0001.
*----------------------------------------------------------------------*
* CLASS lcl_app DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
DATA: value TYPE i.
METHODS: constructor." IMPORTING im_value TYPE i.
ENDCLASS. "lcl_app DEFINITION
DATA: o_app1 TYPE REF TO lcl_app.
DATA: o_app2 TYPE REF TO lcl_app.
CREATE OBJECT o_app1. " EXPORTING im_value = 10.
WRITE:/ o_app1->value.
CREATE OBJECT o_app2. " EXPORTING im_value = 20.
WRITE:/ o_app2->value.
*----------------------------------------------------------------------*
* CLASS lcl_app IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_app IMPLEMENTATION.
METHOD CONSTRUCTOR.
value = 10. "im_value.
ENDMETHOD. "set_value
ENDCLASS. "lcl_app IMPLEMENTATION
2) Again, my 2nd example is working well for me. If you post the code that you implemented, I can see what may be the problem. The reason why you must add the CALL METHOD, is because you are using an older version. You can omit the CALL METHOD in Netweaver releases.
Regards,
Rich Heilman
‎2007 Feb 09 3:20 AM
hi rich, you have cleared my doubt. thanks
this is the 2nd example that not showing the write statement. it executed but immediately after the execution the screen shows the se80 screen back.
thanks
REPORT zrich_0001.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
DATA: value TYPE i.
METHODS: constructor IMPORTING im_value TYPE i.
ENDCLASS. "lcl_app DEFINITION
CLASS lcl_app IMPLEMENTATION.
METHOD CONStructor.
value = im_value.
ENDMETHOD. "set_value
ENDCLASS. "lcl_app IMPLEMENTATION
DATA: o_app1 TYPE REF TO lcl_app.
DATA: o_app2 TYPE REF TO lcl_app.
CREATE OBJECT o_app1
EXPORTING im_value = 10.
WRITE:/ o_app1->value.
CREATE OBJECT o_app2
EXPORTING im_value = 20.
WRITE:/ o_app2->value.
‎2007 Feb 09 3:36 AM
The reason is that since you do not have a START-OF-SELECTION event, hence the IMPLEMENTATION must be at the end of the program. You can either add this START-OF-SELECTION event. Like so.
REPORT zrich_0001.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
DATA: value TYPE i.
METHODS: constructor IMPORTING im_value TYPE i.
ENDCLASS. "lcl_app DEFINITION
CLASS lcl_app IMPLEMENTATION.
METHOD CONStructor.
value = im_value.
ENDMETHOD. "set_value
ENDCLASS. "lcl_app IMPLEMENTATION
DATA: o_app1 TYPE REF TO lcl_app.
DATA: o_app2 TYPE REF TO lcl_app.
start-of-selection.
CREATE OBJECT o_app1
EXPORTING im_value = 10.
WRITE:/ o_app1->value.
CREATE OBJECT o_app2
EXPORTING im_value = 20.
WRITE:/ o_app2->value.
Or simply move the IMPLEMENTATION to the bottom of the program.
REPORT zrich_0001.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
DATA: value TYPE i.
METHODS: constructor IMPORTING im_value TYPE i.
ENDCLASS. "lcl_app DEFINITION
DATA: o_app1 TYPE REF TO lcl_app.
DATA: o_app2 TYPE REF TO lcl_app.
CREATE OBJECT o_app1
EXPORTING im_value = 10.
WRITE:/ o_app1->value.
CREATE OBJECT o_app2
EXPORTING im_value = 20.
WRITE:/ o_app2->value.
CLASS lcl_app IMPLEMENTATION.
METHOD CONStructor.
value = im_value.
ENDMETHOD. "set_value
ENDCLASS. "lcl_app IMPLEMENTATION
Don't ask why the system is so picky, it just is.
Regards,
RIch Heilman
‎2007 Feb 09 1:00 AM
Hi,
Check this example:
REPORT YSUBDEL LINE-SIZE 120.
CLASS c1 DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible to all'.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
DATA : obj1 type ref to c1.
create object : obj1.
write:/5 obj1->commondata.
In abap,we can use '.' function by ->.
obj1 is the instance of the class and we can access the methods of the class
using obj1->meth1.
Hope this helps u,
keerthi
‎2007 Feb 09 3:48 AM