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

ABAP Objects

Former Member
0 Likes
1,566

Hi Team,

Can we declare constructors as private/protected in ABAP objects?

Regards,

Parshuram

16 REPLIES 16
Read only

FredericGirod
Active Contributor
0 Likes
1,529

Hi,

and .. how will you create your object ??

regards

Fred

Read only

0 Likes
1,529

This message was moderated.

Read only

0 Likes
1,529

Balaji,

please read who have asked the question, please use little bit size font

regards

Fred

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,529

Frédéric Girod wrote:

and .. how will you create your object ??

The same way use create objects for the classes in Factory and/or Singleton patterns

Cheers,

Suhas

Read only

0 Likes
1,529

Sorry Frederic...

Regards,

Balaji

Read only

Former Member
0 Likes
1,529

Hi,

The constructor is a special (instance) method in a class and is always named CONSTRUCTOR.

Each class has exactly one constructor.

The constructor does not need to be
defined if no implementation is defined.

The constructor is automatically called
during runtime within the CREATE OBJECT statement.

If you need to implement the constructor,
then you must define and implement it in the PUBLIC SECTION.

Read only

Former Member
0 Likes
1,529

Hi Parashuram,

You cannot declare a constructor method as protected or private. It should be Public.

Also you cannot call the constructor method explicitly using CALL METHOD statement.

It can only be invoked from the CREATE OBJECT statement used to instantiate the particular class.

Thanks,

Ajay Bose

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,529

You can define the CONSTRUCTOR in all the visibility sections from ABAP Release 702 - http://help.sap.com/abapdocu_740/en/abennews-71-abap-objects.htm#!ABAP_MODIFICATION_1@1@

For prior releases you could define the instantiation level as PUBLIC, PRIVATE ,PROTECTED in the properties section of the class.

BR,

Suhas

PS - I have marked this discussion as not-a-question since this can be easily searched in the ABAP documentation.

Read only

Former Member
0 Likes
1,529

You can not call the constructor directly then there is no need to declare the constructor as private means how you will access them..

for this use the concept of singleton class concept.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,529

You can,  of course , define the constructor as a private method, but then you must provide another method which will create instance and return a reference to the instance.

Regards,

Raymond

Read only

former_member219762
Contributor
0 Likes
1,529

Hi Parshuram,

If you declare class property Instantiation as private then we can declare constructor as private or protected. Then we cannot create object from outside the class. To get the object we have to create object in one of the method of class and set that object as return or export parameter, call that method from out side the class.

Regards,

Sreenivas.

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,529

Hi,

I smell a Java programer camuflage as ABAPER.

This a legitimate question in cases where the class contains only static method .

Eitan.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,529

These are declared only in public section

Well explain this to SAP developpers, there is already some hundreds of class with private constructor in a fresh SAP system (*)

Regards,

Raymond

(*) Execute SE16N on table SEOCOMPODF with method CMPNAME = 'CONSTRUCTOR' and visibility EXPOSURE = 0 private or 1 protected (ok there are more than 20K peblic constructor too )

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,529

Well explain this to SAP developers, there is already some hundreds of class with private constructor in a fresh SAP system

Cliche - "Little knowledge is a dangerous thing"

It still beats me why people respond with half-known facts & add to the confusion of the already confused OP

Cheers,

Suhas

Read only

0 Likes
1,529

Because not all the people knows and understand perfectly SAP and they only tries to help others people with her knowledge.

no ?

Fred

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,529

Because not all the people knows and understand perfectly SAP and they only tries to help others people with her knowledge.

I don't want to start a flamewar here, so no hard feelings

IMHO sharing knowledge is great but then share the correct one ...