|
ABAP Tutorials -
Object Oriented ABAP
|
|
Written by mastram
|
|
Monday, 16 June 2008 06:25 |
|
Constructors of a class are special methods that define the initial state of an object of that class. Constructors are called whenever you create a new instance of a class. This method is called only once as soon as the new instance is created. These are called when the CREATE OBJECT statement is executed. Constructors are optional in ABAP Objects. However, constructors are necessary whenever you want to set the initial state of an object of the class. This method will NOT have any exporting parameters as its sole responsibility is to create the object. However, it can have some importing parameters.
The method CONSTRUCTOR is mostly a public method because you would mostly want to create the object of the class outside the class. However, you can make this a private method also. This is done in case you want to restrict the creation of class objects out the class. An example is a Singleton class. (The concept of singleton class will be explained later) Creating an object: To create a object of the class call the statement CREATE OBJECT lref_obj TYPE <class_name>. If the constructor has some importing parameters then pass the parameters also. You can use the 'PATTERN' button on the tool bar to insert the automated code (like you do for the function modules). Select the radio button 'ABAP Object Patters' and then hit enter. On the next screen select the radio button 'CREATE OBJECT' and input the name of the class and variable. Hit enter and you will see the code inserted automatically.
|
|
Last Updated ( Tuesday, 17 June 2008 06:43 )
|