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

ooabap object creation

0 Likes
3,933

Can we create an object for private class

12 REPLIES 12
Read only

Sandra_Rossi
Active Contributor
3,828

I never heard of "private class" concept in ABAP. Can you explain what you mean?

Read only

former_member598787
Participant
0 Likes
3,828

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.

Read only

matt
Active Contributor
3,828

Yes, but only from that class or a friend of that class.

Read only

matt
Active Contributor
0 Likes
3,828

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.

Read only

FredericGirod
Active Contributor
0 Likes
3,828

A private class should (must) avec have a public method to create the instance : Get_Instance( ) or something like that

Read only

FredericGirod
Active Contributor
3,828

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.

Read only

matt
Active Contributor
3,828

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.

Read only

FredericGirod
Active Contributor
0 Likes
3,828

yes ! constant class also use it

Read only

Sandra_Rossi
Active Contributor
3,828

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.

Read only

FredericGirod
Active Contributor
3,828

sandra.rossi I wrote this big example for you, before my coffee, and you already know it ... 🙂

Read only

0 Likes
3,828

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.

Read only

Shruthi2979
Explorer
0 Likes
3,828

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.