Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CONSTRUCTORS

Former Member
0 Likes
2,587

hello sap gurus,

can any one tell me why we use the constructors in oops concept ?

what is the full form of oops ?

Thank you very much in advance.

5 REPLIES 5
Read only

Former Member
0 Likes
1,508

hi

refer the below mentioned link

Read only

Former Member
0 Likes
1,508

Hi,

Constructor method

Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor method with the reserved name class_constructor.

The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class constructor is executed exactly once before you first access a class.

The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

Static constructor

The static constructor is always called CLASS_CONSTRUCTER, and is called autmatically before the clas is first accessed, that is before any of the following actions are executed:

1) Creating an instance using CREATE_OBJECT

2) Adressing a static attribute using <classname>-><attrbute>

3) Calling a ststic attribute using CALL METHOD

4) Registering a static event handler

5) Registering an evetm handler method for a static event

The static constructor cannot be called explicitly.

check out this link for example program using constructor...

http://www.erpgenie.com/abap/OO/eg.htm#Use constructor to create an object with parameters

Reward points if useful

Read only

Former Member
0 Likes
1,508

The full form of OOPS is object oriented programming.

Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor method with the reserved name class_constructor.

The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class constructor is executed exactly once before you first access a class.

The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

If you want to start from scratch go to transaction abapdocu which will help u in learning on ur own.

Also refer to this link.

http://www.erpgenie.com/abap/OO/defn.htm

Reward if helpful.

Thanks.

imran.

Read only

Former Member
0 Likes
1,508

Hi vijaya lakshmi,

OOP is the common abbreviation for Object-Oriented Programming. eg C++ is Object oriented programming language...

like that in SAP we are having OO ABAP which stands for Object Oriented ABAP.

METHODS - constructor


Syntax 
METHODS constructor [FINAL] 
  [IMPORTING parameters [PREFERRED PARAMETER p]] 
  [{RAISING|EXCEPTIONS} exc1 exc2 ...]. 



Extras: 
1. ... IMPORTING parameters 

2. ... RAISING exc1 exc2 ... 

3. ... EXCEPTIONS exc1 exc2 ... 

4. ... FINAL 

Effect

: This statement declares the instance constructor constructor of the class. For public and protected instantiatable classes, this is only possible in the public visibility section of the declaration section of a class. If you use the addition CREATE PRIVATE in the CLASS DEFINITION statement, this is possible in all visibility sections.

Each class has a predefined method called constructor. By declaring this explicitly, the interface of the method constructor can be defined specifically for a class, and its functions can be implemented. Without explicit declaration, the instance constructor assumes the parameter interface of the direct superclass, and calls it implicitly.

If the instance constructor is implemented in a subclass, the instance constructor of the superclass must be called explicitly using the pseudo reference super->constructor, even if the latter is not explicitly declared. Exceptions to this are direct subclasses of the root node object. Before the superclass contructor is called, an instance constructor only has access to the static components of its class. After the superclass constructor is called, it can also access instance components.

For each instance of a class, the instance constructor is called only once using the statement CREATE OBJECT immediately after it has been generated. For the call, appropriate actual parameters must be assigned to all non-optional input parameters, return values can be assigned to non-class-based exceptions, and class-based exceptions can be declared. It is not possible to call the instance constructor using CALL METHOD, except when calling the superclass constructors using super->constructor in the redefined constructor of a subclass.

During execution of an instance constructor, the current instance temporarily assumes the type of the class in which the constructor is defined. This has the following consequences:

If methods are called during the execution of a superclass constructor, the implementations of the superclass are executed and not the redefinitions of subclasses. The specification of me->, for addressing a redefined method in a subclass that has just been generated, has no effect.

During execution of a superclass constructor, attempts to access components of the subclass using a Down Cast lead to a runtime error.

Notes

Instance constructors are an exception to the rule that all public components on one path in the inheritance hierarchy are in the same namespace. The instance constructor of each class has its own interface and its own implementation. An instance constructor cannot be redefined.

Instance constructors are declared in the public visibility section of a class purely for technical reasons. The actual visibility is controlled by the addition CREATE {PUBLIC|PROTECTED|PRIVATE} to the statement CLASS DEFINITION .


Addition 1 
... IMPORTING parameters 


Addition 2 
... RAISING exc1 exc2 ... 


Addition 3 
... EXCEPTIONS exc1 exc2 ... 

Effect

: Using the IMPORTING addition, input parameters can be defined according to the same rules as for general methods. The additions RAISING and EXCEPTIONS for the declaration of class-based exceptions or the definition of non-class-based exceptions also have the same meaning as for general methods.

Addition 4

... FINAL

Effect

: Instance constructors are implicitly final. The addition FINAL can be specified, but it is not necessary.

Example:

In this example, the class c2 inherits from the class c1. In both classes, the instance constructor constructor is declared explicitly. It must therefore be implemented in both classes, whereby the implementation in c2 must include the call of the superclass constructor.


CLASS c1 DEFINITION. 
  PUBLIC SECTION. 
    METHODS constructor IMPORTING p1 TYPE any. 
    ... 
ENDCLASS. 

CLASS c2 DEFINITION INHERITING FROM c1. 
  PUBLIC SECTION. 
    METHODS constructor IMPORTING p2 TYPE any. 
    ... 
ENDCLASS. 

CLASS c1 IMPLEMENTATION. 
  METHOD constructor. 
    ... 
  ENDMETHOD. 
ENDCLASS. 

CLASS c2 IMPLEMENTATION. 
  METHOD constructor. 
    ... 
    super->constructor( p2 ). 
    ... 
  ENDMETHOD. 
ENDCLASS. 

Hope it will solve your problem..

Reward points if useful...

Thanks & Regards

ilesh 24x7

Read only

Former Member
0 Likes
1,508

hi lakshmi,

oops is nothing but object oriented programming.

Here in abap, uses the concept of OOPS.

we call them as ABAP Objects or OOABAP.

Constructors are special methods for a class.

Instance constructor,

static constructor.

Constructors are the special methods that cannot be called using CALL METHOD.

instead, they are called automatically by the system to set the starting state of a new object or class.

Instance constructor : instance constructor of a class gets triggered when an object is created from the class.

these are Instance specific.( Object specific..if n objects are created , n times the constructor is called ..with each of has its own memory allocated for it..)

instance constructor have import parameters and Exceptions.

Static Constructor : Static constructor (class Constructor) is triggered before any of the following events:

when Creating an Object of a class using

CREATE OBJECT obj_name,

whre obj_name has the Data type REF TO class.

or

calling a static method using Class => method.

we can directly call static method with its class name on left side,

and method name on right side.

<Class_name> => <Method_name>.

Static constructor Cannot have any parameters.

if useful reward me.

if any doubts regarding this let me know...!

thanks & Regards,

Rajesh.