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

OOPS question

0 Likes
1,903

how to call the instance construct that is declared in the private section of the class.

class abc definition.

private section.

methods constructor.

endclass.

how to call the above constructor?

1 ACCEPTED SOLUTION
Read only

keremkoseoglu
Contributor
1,791

It actually makes sense to make the constructor private if you are using a creational design pattern; such as:

  • Builder
  • Factory
  • Singleton
  • Multiton
  • ...

In such cases, the class will typically have a public static method which is responsible of creating an instance. You will call this static public method to create the object instance, instead of calling the constructor.

Check the following example class: https://github.com/keremkoseoglu/addict/blob/main/src/ycl_addict_data_element.clas.abap

This is how you create the object:

DATA(my_obj) = ycl_addict_data_element=>get_instance( 'BUKRS' ).

Instead of this:

DATA(my_obj) = NEW ycl_addict_data_element( 'BUKRS' ). " ERROR!

See?

6 REPLIES 6
Read only

BiberM
Contributor
0 Likes
1,791

Beside the fact that a constructor for technically reasons should never be declared Private, your class probably has another static (class-) Method that handles instance creation.

Read only

matt
Active Contributor
0 Likes
1,791

a constructor for technically reasons should never be declared Private

Really? What would they be? That's not even a philosophy I've heard of.

Read only

0 Likes
1,791

Well there is even an abapOpenCheck for that which cites the language help:

https://docs.abapopenchecks.org/checks/90/

I can't really rememeber where I encountered that problem ~5 years ago. But changing constructor visibility back to default public solved that.

Read only

0 Likes
1,791

i know that construct should never be private. But how to answer the above question. it was faced in an interview

Read only

keremkoseoglu
Contributor
1,792

It actually makes sense to make the constructor private if you are using a creational design pattern; such as:

  • Builder
  • Factory
  • Singleton
  • Multiton
  • ...

In such cases, the class will typically have a public static method which is responsible of creating an instance. You will call this static public method to create the object instance, instead of calling the constructor.

Check the following example class: https://github.com/keremkoseoglu/addict/blob/main/src/ycl_addict_data_element.clas.abap

This is how you create the object:

DATA(my_obj) = ycl_addict_data_element=>get_instance( 'BUKRS' ).

Instead of this:

DATA(my_obj) = NEW ycl_addict_data_element( 'BUKRS' ). " ERROR!

See?

Read only

michael_piesche
Active Contributor
0 Likes
1,791

>> how to call the above constructor?

Since this is an interview question, which is more about object oriented programming in general than specifically about SAP OO, the answer should be, the following:

In order to create an object of a class with a private constructor, the class needs to have, for example, a public static method which is able to create the object.

class abc definition.
  public section.
    methods create_instance. " method implementation will contain private constructor
  private section.
    methods constructor.
endclass.