Sunteți pe pagina 1din 15

What is Constructor

Definition:
Constructor is a one special method which is automatically /
Implicitly called when an object is created.

Purpose/Use:
Constructor are used to initialize current class object.
Rules / Characteristic
Constructor name must be CONSTRUCTOR, or
CLASS_CONSTRUCTOR.
Constructor can be inherited into derived class.
Class Constructor can not be inherited into derived class.
Because every class constructor is made for initialize the its
own attribute of the class but not for initialize another class
attribute.
Types of Constructor


Constructor (Instance Constructor)

Class Constructor ( Static Constructor)
Instance Constructor
To create a Instance Constructor use Constructor keyword.
Instance constructor is executed automatically for every object
creation.
Instance constructor is by default is private.
We will not provide Explicit any return type to constructor.
Static Constructor

To design static Constructor use CLASS_CONSTRUCTOR.

Static constructor is executed automatically for either
i. When class name is used. (Only Once)
ii. When first object is created. (Only Once)
Note:
Static Constructor is triggered in the above 2 scenario but which one scenario
comes first.

Class Constructor is by default is public.
We will not provide any parameter to Class-Constructor.
We can not inherit Class Constructor.





Constructor
Method

Methods are executed
explicitly by developer.

Methods can have
importing, exporting,
changing, returning
parameters.


Constructor

Constructors are executed
implicitly.

We can not provide explicitly
returning parameters to
instance constructor but
Static constructor cant have
any parameters.
Advantage
I) Constructor eliminate in a placing value.
0
0
X X X
10
20
X X X
Object Object
Constructor Method
Class Class
II) Constructor eliminate calling of Ordinary Method.
Ordinary Method:
All Method which are not similar to CONSTRUCTOR
name is know as Ordinary Method.
Special Method:
All Method which are similar to CONSTRUCTOR name is
known as Special Method.
Overloaded Constructor
*** ABAP Does not support Overloaded Constructor
Over Load = Class name is Same + Signature is Different

Signature:
No of Parameter
Type of Parameter
Order of Parameter

At Least one will be
Different
Visibility Chart
Private Protected Public
Private Y Y Y
Protected N Y Y
Public N N Y
Method Parameters
Importing
Provide the input value in report program.
We can use more then importing parameters.

Exporting
Provide the output value in report program.
It will return value when program is executed completely.
We can pass table throw exporting parameters.
We can use more then exporting parameters.
Method Parameters
Changing
It treated as a importing as well as exporting parameters.
We can use more then changing parameters.

Returning
It will return the value in the mid of program execution.
We can not pass table throw returning parameters.
If we are using returning parameters we cant use exporting
and changing parameters.
We cant use more then one returning parameters.
Function Module versus Method
Function Module
Returning parameter is not
there.
Method
Returning parameters is
there.

S-ar putea să vă placă și