2013 Oct 08 8:29 AM
Hi Team,
Can we declare constructors as private/protected in ABAP objects?
Regards,
Parshuram
2013 Oct 08 8:40 AM
2013 Oct 08 8:54 AM
2013 Oct 08 8:57 AM
Balaji,
please read who have asked the question, please use little bit size font
regards
Fred
2013 Oct 08 9:05 AM
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
2013 Oct 08 10:29 AM
2013 Oct 08 8:42 AM
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.
2013 Oct 08 8:57 AM
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
2013 Oct 08 9:00 AM
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.
2013 Oct 08 9:00 AM
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.
2013 Oct 08 9:03 AM
2013 Oct 08 9:19 AM
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.
2013 Oct 08 9:38 AM
Hi,
I smell a Java programer camuflage as ABAPER.
This a legitimate question in cases where the class contains only static method .
Eitan.
2013 Oct 08 10:48 AM
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 )
2013 Oct 08 12:32 PM
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
2013 Oct 08 1:30 PM
Because not all the people knows and understand perfectly SAP and they only tries to help others people with her knowledge.
no ?
Fred
2013 Oct 08 1:39 PM
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 ...