Application Development 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: 

what is constructor

Former Member
0 Kudos

can u tell me the description of construction

and send me simple code

could u plz send it clearly

5 REPLIES 5

Former Member
0 Kudos

Constructors

Constructors are special methods that produce a defined initial state of objects and classes. The state of an object is determined by its instance attributes and static attributes. You can assign contents to attributes using the VALUE addition in the DATA statement. Constructors are necessary when you want to set the initial state of an object dynamically.

Like normal methods, there are two types of constructor - instance constructors and static constructors.

For inheritance some special rules apply to constructors that are not described here but in the inheritance context.

Instance Constructors

Each class has one instance constructor. It is a predefined instance method of the CONSTRUCTOR class. If you want to use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section. Unless it is explicitly declared, the instance constructor is an empty method.

The declaration in the public area is necessary for technical reasons. The actual visibility of the instance constructor is controlled by the CREATEPUBLIC|PROTECTED|PRIVATE additions to the CLASS statement. For more information refer to Visibility of Instance Constructors.

Instance constructors are executed once for each instance. They are called automatically, immediatly after you have created an instance using the CREATE OBJECT statement. It is not possible to call an instance constructor directly using the CREATE OBJECT statement.

An instance constructor can contain an interface with IMPORTING parameters and EXCEPTIONS. You define the interface using the same syntax as for normal methods in the METHODS statement. The fact that there are no exporting parameters shows that constructors only exist to define the state of an object and have no other function. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement (see the example in the documentation for CREATE OBJECT).

Static Constructors

Each class has a single static constructor. This is a predefined, public, static method of the CLASS_CONSTRUCTOR class. If you want to use the static constructor, you must declare the static method CLASS_CONSTRUCTOR in the public section of the declaration part of the class using the CLASS-METHODS statement, and implement it in the implementation part. The static constructor has no interface parameters and cannot trigger exceptions. Unless you implement it explicitly it is merely an empty method.

The static constructor is executed once in each program. It is called automatically for the class class before the class is accessed for the first time - that is, before one of the following actions:

Generating an instance of a class using CREATE OBJECT obj, where obj has the data type REF TO class.

Calling a static method using [CALL METHOD] class=>meth.

Registering a static event handler method using SET HANDLER class=>meth for obj.

Registering an event handler method for a static event of the class class.

Addressing a static attribute with class=>a.

The static constructor is always called immediately before the action is executed, with one exception: If your first access to the class is to address a static attribute, the static constructor is executed at the beginning of the processing block (dialog module, event block, procedure) in which the access occurs.

Caution: The static constructor must not access its own class. Otherwise a runtime error will occur.

Caution: The point at which the static constructor is executed has not yet been finalized, and it may yet be changed. SAP only guarantees that it will be executed before the class is accessed for the first time. You are recommended not to write programs that require the static constructor to be executed at the beginning of a processing block.

former_member181962
Active Contributor
0 Kudos

Constructor is the method that gets called automtically when an object is created for a class.

See the SAP provided help for more info:

"METHODS - constructor

Syntax

METHODS constructor [FINAL]

[IMPORTING parameters]

[{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. It is only possible in the public visibility section of the declaration section of a class.

Each class has a predefined method called constructor in its public visibility section. 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 instance constructor of its direct superclass, and calls this 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, and its interface must be populated. 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 Widening 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.

"

Regards,

Ravi

Former Member
0 Kudos

Hello ,

When you create a local class or global class you will method for that class which will be called through the object of the class .

when you do a create object objectname .that calls the constructor of the class .

Refer to the code.

REPORT ZCLASS

data : itab type standard table of spfli.

INTERFACE COST.

METHODS M1.

DATA : X TYPE I.

ENDINTERFACE.

DATA: V_DISTANCE TYPE I.

CLASS PLANE DEFINITION.

PUBLIC SECTION.

METHODS : CONSTRUCTOR IMPORTING

IM_CARRID TYPE I

IM_CONNID TYPE I

IM_DISTANCE TYPE I,

GET_DISPLAY,FUEL_EST.

PRIVATE SECTION.

DATA : V_CARRID TYPE I,

V_CONNID TYPE I,

V_DISTANCE TYPE I,

FUELCAL TYPE I.

ENDCLASS.

CLASS PLANE IMPLEMENTATION.

METHOD CONSTRUCTOR.

V_CARRID = IM_CARRID.

V_CONNID = IM_CONNID.

V_DISTANCE = IM_DISTANCE.

ENDMETHOD.

METHOD FUEL_EST.

FUELCAL = V_DISTANCE * 10.

WRITE:/ FUELCAL.

ENDMETHOD.

METHOD GET_DISPLAY.

WRITE:/ V_CARRID,

V_CONNID,

V_DISTANCE,

FUELCAL.

ENDMETHOD.

ENDCLASS.

CLASS PASS_PASSENGER DEFINITION INHERITING FROM PLANE.

PUBLIC SECTION.

METHODS: CONSTRUCTOR IMPORTING

IM_CARRID TYPE I

IM_CONNID TYPE I

IM_DISTANCE TYPE I

IM_PASSENGER TYPE I.

METHODS: FUEL_EST REDEFINITION.

METHODS: GET_DISPLAY REDEFINITION.

INTERFACES COST.

PRIVATE SECTION.

DATA : V_CARRID TYPE I,

V_CONNID TYPE I,

V_DISTANCE TYPE I,

IM_PASS TYPE I,

X TYPE I,

FUELCAL.

ENDCLASS.

CLASS PASS_PASSENGER IMPLEMENTATION.

METHOD CONSTRUCTOR.

CALL METHOD SUPER->CONSTRUCTOR( IM_CARRID = IM_CARRID

IM_CONNID = IM_CONNID IM_DISTANCE = IM_DISTANCE ) .

IM_PASS = IM_PASSENGER.

ENDMETHOD.

METHOD COST~M1.

X = IM_PASS * V_DISTANCE.

ENDMETHOD.

METHOD FUEL_EST.

FUELCAL = V_DISTANCE * 10.

ENDMETHOD.

METHOD GET_DISPLAY.

WRITE:/ V_CARRID,

V_CONNID,

V_DISTANCE,

IM_PASS.

ENDMETHOD.

ENDCLASS.

DATA REF2 TYPE REF TO PASS_PASSENGER.

START-OF-SELECTION.

CREATE OBJECT REF2 EXPORTING

IM_CARRID = 11

IM_CONNID = 88

IM_DISTANCE = 678

IM_PASSENGER = 10.

REF2->COST~M1( ).

REF2->GET_DISPLAY( ).

CLASS CARGO_PLANE DEFINITION INHERITING FROM PLANE.

PUBLIC SECTION.

METHODS: CONSTRUCTOR IMPORTING

IM_CARRID TYPE I

IM_CONNID TYPE I

IM_DISTANCE TYPE I

IM_WEIGHT TYPE I.

METHODS: FUEL_EST REDEFINITION.

INTERFACES COST.

PRIVATE SECTION.

DATA : IM_WT TYPE I,

FUELCAL TYPE I,

X TYPE I.

ENDCLASS.

CLASS CARGO_PLANE IMPLEMENTATION.

METHOD CONSTRUCTOR.

CALL METHOD SUPER->CONSTRUCTOR( IM_CARRID = IM_CARRID

IM_CONNID = IM_CONNID IM_DISTANCE = IM_DISTANCE ).

IM_WT = IM_WEIGHT.

ENDMETHOD.

METHOD FUEL_EST.

FUELCAL = V_DISTANCE * 200.

ENDMETHOD.

METHOD COST~M1.

X = IM_WT * V_DISTANCE.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA REF3 TYPE REF TO CARGO_PLANE.

CREATE OBJECT REF3 EXPORTING IM_CARRID = 112 IM_CONNID = 34 IM_DISTANCE

= 49 IM_WEIGHT = 56.

REF3->COST~M1( ).

REF3->GET_DISPLAY( ).

DATA: WA_ITAB LIKE LINE OF ITAB.

CLASS LCL_VEH DEFINITION.

PUBLIC SECTION.

METHODS : GET_DATA IMPORTING

IM_CARRID TYPE SPFLI-CARRID

IM_CONNID TYPE SPFLI-CONNID

IM_DISTANCE TYPE SPFLI-DISTANCE,

GET_DISPLAY,GET_FUEL.

PRIVATE SECTION.

DATA : V_CARRID TYPE SPFLI-CARRID,

V_CONNID TYPE SPFLI-CONNID,

V_DISTANCE TYPE SPFLI-DISTANCE.

.

ENDCLASS.

START-OF-SELECTION.

DATA : R_OBJ TYPE REF TO LCL_VEH.

CLASS LCL_VEH IMPLEMENTATION.

METHOD GET_DATA.

V_CARRID = IM_CARRID.

V_CONNID = IM_CONNID.

V_DISTANCE = IM_DISTANCE.

R_OBJ->GET_DISPLAY( ).

ENDMETHOD.

METHOD GET_DISPLAY.

WRITE 😕 V_CARRID,

V_CONNID,

V_DISTANCE.

ENDMETHOD.

METHOD GET_FUEL.

WRITE : ' NO FUEL'.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

CREATE OBJECT R_OBJ.

SELECT * FROM SPFLI INTO TABLE ITAB.

LOOP AT ITAB INTO WA_ITAB.

R_OBJ->GET_DATA( IM_CARRID = WA_ITAB-CARRID

IM_CONNID = WA_ITAB-CONNID

IM_DISTANCE = WA_ITAB-DISTANCE ).

ENDLOOP.

Please reward if useful.

Former Member
0 Kudos

hi,

Executed once for each instance.

Called automatically, immediately after the CREATE OBJECT statement.

Can contain an interface with IMPORTING parameters and EXCEPTIONS , but cannot have any EXPORTING/CHANGING/RETURNING parameters .

The interfaces are defined using the same syntax as for normal methods in the METHODS statement. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement .

Static Constructor

Static methods, declared as CLASS-METHODS : CLASS_CONSTRUCTOR in the public section of the class definition and are also implemented in the implementation part.

Has no interface parameters and cannot trigger exceptions.

Executed once in each program. It is called automatically for the class before it is accessed for the first time - that is, before one of the following actions:

CREATE OBJECT obj from the class.

Call a static method : [CALL METHOD] class=>meth.

Registering a static event handler method using SET HANDLER class=>meth for obj.

Registering an event handler method for a static event of the class class.

Addressing a static attribute with class=>a.

example.

REPORT YSUBOOPS2.

CLASS c1 DEFINITION .

PUBLIC SECTION.

CLASS-DATA : NUM TYPE I VALUE 5.

CLASS-METHODS:CLASS_CONSTRUCTOR.

ENDCLASS.

CLASS c1 IMPLEMENTATION.

METHOD CLASS_CONSTRUCTOR.

WRITE:/5 'I am class constructor'.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

WRITE:/5 C1=>NUM.

Regards,

Sruthi

Former Member
0 Kudos

hi

good

go through this link for the example of constructor and details about the constructure

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

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

thanks

mrutyun^