‎2021 Aug 17 1:11 PM
‎2021 Aug 17 1:13 PM
I never heard of "private class" concept in ABAP. Can you explain what you mean?
‎2021 Aug 17 3:13 PM
Hello,
Information provided is not sufficient.
But still, by private instantiation of class , This is something you can achieve by using SINGLETON class .(ABAP design patterns) , You can find examples of singleton classes on google.
‎2021 Aug 17 6:52 PM
‎2021 Aug 17 6:54 PM
Other examples are enumeration classes where a public read-only structure holds self-references, and factory classes where the classes they return are all friends of the factory class. In this way the ability to instantiate is entirely controlled.
‎2021 Aug 18 6:42 AM
A private class should (must) avec have a public method to create the instance : Get_Instance( ) or something like that
‎2021 Aug 18 6:47 AM
sandra.rossi
CLASS ZCL_MyBeautifullClass DEFINITION
PUBLIC
FINAL
CREATE PRIVATE. <--
PUBLIC SECTION.
INTERFACES zif_MyBeautifullInterface
METHODS CONSTRUCTOR.
METHODS GET_INSTANCE
RETURNING VALUE(o_instance) TYPE REF TO zif_MyBeautifullInterface.
ENDCLASS.
CLASS zcl_MyBeautifullInterface IMPLEMENTATION.
METHOD constructor.
...
ENDMETHOD.
METHOD get_instance.
o_instance = new zcl_mybeautifullclass( ).
endmethod.
ENDCLASS.You cannot call the constructor from outside. You can only call get_instance, and get_instance will call the constructor.
‎2021 Aug 18 6:47 AM
frdric.girod a public class/static method. But not always.
An example of a private class without a public method, https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-enumeration-classes-to-c...
The second one.
‎2021 Aug 18 6:49 AM
‎2021 Aug 18 7:24 AM
frdric.girod Thanks. Yes I know that 🙂 I just meant that "private class" is not an official term (although I see the term appears once in the official ABAP documentation - in CREATE OBJECT). With a so much general question in 8 words, I'm not sure the person understands the whole topic, so I try to make her speak more about it. The term "class with private instantiation" or "privately instantiated" would be better suited. The term "private class" makes me think that we are talking about package check: a class which cannot be seen/used by objects of other packages.
‎2021 Aug 18 7:46 AM
sandra.rossi I wrote this big example for you, before my coffee, and you already know it ... 🙂
‎2021 Aug 18 1:57 PM
Hi Rajani,
yes we can create object for private class ( within same class)

if we try to create object in sub class error will be thrown.

so if we want to create object of parent class(private class) in sub class then we have to use friend class concept as shown in below image.

and you can also achieve using singleton class.
‎2022 Jan 24 11:33 AM
Hi ,
Yes we can create the object for the private class .
We can also create the object for the friends class which is the friend of the private class.
Thanks.