‎2008 Sep 04 8:38 AM
Hi,
i learn now about constructor in Abap and my question is ,
what is the advantage and disadvantage of using constructor oo,that i can build method that do the same action
e.g.
Regular Method:
METHODS: set_attributes IMPORTING
im_name TYPE string
im_planetype TYPE saplane-planetype,
display_attributes.
Constructor :
METHODS: constructor IMPORTING
im_name TYPE string
im_planetype TYPE saplane-planetype,
display_attributes.
in the constructor i use:
CREATE OBJECT r_plane
EXPORTING
im_name = 'LH_Barlin'
im_planetype = 'a321'.
and in Method :
CREATE OBJECT r_plane.
r_plane->set_attributes( im_name = 'LH_usa' im_planetype = 'bbb' ).and both commend do the same :
Regards
‎2008 Sep 04 8:44 AM
The constructor is nothiing else but a method which is called automatically when the class is instantiated.
‎2008 Sep 04 8:44 AM
The constructor is nothiing else but a method which is called automatically when the class is instantiated.
‎2008 Sep 04 9:02 AM
Hi Rainer Hübenthal ,
Thanks ,
if i can do it in method so way to have it (constructor) ?
Regards
‎2008 Sep 04 9:18 AM
First, you should post these questions in the ABAP Objects forum.
Anyway, you use the constructor to force certain actions when the instance is created. If you as designer of the class require that two attributes be set, then making them obligatory parameters ensures that an object can't be instantiated without those parameters being given some values. You could include some validation in the constructor as well to make sure that the values are valid.
If you don't put them in a constructor, you are relying on future developers - possibly even yourself - remembering to set those parameters after they've created.
One other advantage - it's one line of ABAP instead of three!
As ever though, which is better comes down to what specifically you're trying to do. It is usually better programming practice to leave as little to luck and remembering, as possible.
matt
‎2008 Sep 04 8:47 AM
Hi Cosmo,
refer to the link :
http://help.sap.com/saphelp_erp2004/helpdata/en/0a/b552f7d30911d2b467006094192fe3/content.htm
With luck,
Pritam.
‎2008 Sep 04 9:07 AM
Hi,
In constructor the method will execute automatically when the class has been instanced.
But for the normal method we have to call the method explicitly.
In some cases we need constructor so that some piece of code is executed automatically when the class is initiated.
Regards
Manjari.
‎2008 Sep 04 9:21 AM
Hi Cosmo Kramer,
Please check these threads..
/message/5904557#5904557 [original link is broken]
Please go through the below SAP HELP
[Constructor Vs Method |http://help.sap.com/erp2005_ehp_03/helpdata/EN/08/d27c03b81011d194f60000e8353423/content.htm]
Hope this would help you to understand the difference..
Good luck
Narin