09-04-2008 6:27 AM
Hi all
i am new to abap objects and i am going thro' the sap materials and i am facing some probelm in understanding the constructor part which is also a type of method..can any one please let me know what is the difference between methods and constructors..
Your explanations are greatly welcome instead of links.
Thanks
09-04-2008 6:43 AM
Hi,
actualy constructor is a special method in OOABAP which is used for initialisation and it is always named as constructor.
Constructors are of 2 type 1. CLASS or Static Constructor
and 2 . Method Constructor.
Check the following code for the use and effect of both types of constructors
REPORT demo_constructor_ANIRBAN.
----
CLASS cl1 DEFINITION
----
*
----
CLASS cl1 DEFINITION.
PUBLIC SECTION.
METHODS:
add,
constructor IMPORTING v1 TYPE i
v2 TYPE i,
display.
CLASS-METHODS:
class_constructor.
PRIVATE SECTION.
DATA:
w_var1 TYPE i,
w_var2 TYPE i,
w_var3 TYPE i,
w_result TYPE i.
ENDCLASS. "cl1 DEFINITION
----
CLASS cl1 IMPLEMENTATION
----
*
----
CLASS cl1 IMPLEMENTATION.
METHOD constructor.
w_var1 = v1.
w_var2 = v2.
ENDMETHOD. "constructor
METHOD class_constructor.
WRITE:
/ 'Tihs is a class or static constructor.'.
ENDMETHOD. "class_constructor
METHOD add.
w_result = w_var1 + w_var2.
ENDMETHOD. "add
METHOD display.
WRITE:
/'The result is = ',w_result.
ENDMETHOD. "display
endclass.
" Main program----
data:
ref1 type ref to cl1.
parameters:
w_var1 type i,
w_var2 type i.
start-of-selection.
create object ref1 exporting v1 = w_var1
v2 = w_var2.
ref1->add( ).
ref1->d isplay( ).
09-04-2008 6:43 AM
Hi,
actualy constructor is a special method in OOABAP which is used for initialisation and it is always named as constructor.
Constructors are of 2 type 1. CLASS or Static Constructor
and 2 . Method Constructor.
Check the following code for the use and effect of both types of constructors
REPORT demo_constructor_ANIRBAN.
----
CLASS cl1 DEFINITION
----
*
----
CLASS cl1 DEFINITION.
PUBLIC SECTION.
METHODS:
add,
constructor IMPORTING v1 TYPE i
v2 TYPE i,
display.
CLASS-METHODS:
class_constructor.
PRIVATE SECTION.
DATA:
w_var1 TYPE i,
w_var2 TYPE i,
w_var3 TYPE i,
w_result TYPE i.
ENDCLASS. "cl1 DEFINITION
----
CLASS cl1 IMPLEMENTATION
----
*
----
CLASS cl1 IMPLEMENTATION.
METHOD constructor.
w_var1 = v1.
w_var2 = v2.
ENDMETHOD. "constructor
METHOD class_constructor.
WRITE:
/ 'Tihs is a class or static constructor.'.
ENDMETHOD. "class_constructor
METHOD add.
w_result = w_var1 + w_var2.
ENDMETHOD. "add
METHOD display.
WRITE:
/'The result is = ',w_result.
ENDMETHOD. "display
endclass.
" Main program----
data:
ref1 type ref to cl1.
parameters:
w_var1 type i,
w_var2 type i.
start-of-selection.
create object ref1 exporting v1 = w_var1
v2 = w_var2.
ref1->add( ).
ref1->d isplay( ).
09-04-2008 1:03 PM
>
....
Anirban - please learn to surround your code with It's much easier to read that way.
matt
09-05-2008 10:04 AM
09-04-2008 6:45 AM
Hi,
Constructor is a special instance method, which is called at runtime when object is created. Each class can have only one instance constructor.
Methods are internal procedures in classes that determine the behavior of the objects. They can access all attributes in their class.
Regards
Abhijeet
09-04-2008 7:16 AM
09-04-2008 7:18 AM
09-04-2008 10:53 AM
Hi,
Constructors:
They are special kind of methods which are mainly used to initialise the values or the data in an object. Constructors are implicitly called as and when an object of the clas is created. We in Most of the cases does not call constructors.
Methods:
They are code block which are explictly called to perform some functionality.
Regards
Sumit Agarwal
09-04-2008 10:58 AM
Hello Arun,
Constructor is a special method, which will be called automatically once when you create a instance of that class.
For example you have your website and you wan to check no. of hits to our site. You will declare one method in that you will update some static variable. This method will automatically called when ever you create an instance in this case your instance is calling that web site.
I hope you understand the concept
Regards,
Naresh.
09-04-2008 10:59 AM
09-04-2008 12:16 PM
09-05-2008 6:50 AM