Application Development 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: 

How to use a private class

Former Member
0 Kudos

Hi ,

I want to use a private class cl_http_client which is a private class.How can i use it?

Thanks and Regards

Sohit Gulati

6 REPLIES 6

Former Member
0 Kudos

U can use a class by its public methods only

or

using its FRIEND class

Reward if useful

Former Member
0 Kudos

hi,

what do you mean by private class...If u mean if the instantiation is private then their will be a method something like get_instance which will return the instance of the method..

Use this returned reference to call the other methods of the class.

Regards,

Abhi

Former Member
0 Kudos

If you know about design patterns, you would be aware of something called as a "singleton".

A singleton class essentially means that there can be only one instantiation object of the class during runtime. The class through its implementation does not allow creation of multiple instances.

In ABAP Objects, a private class (rather a class which has the syntax CREATE PRIVATE appended to the definition), does exactly that, implements a singleton.

An example can be shown below.

CLASS a DEFINITION CREATE PRIVATE.

PUBLIC SECTION.

CLASS-METHODS:

get_instance RETURNING VALUE(re_ref) TYPE REF TO a.

PRIVATE SECTION.

CLASS-DATA:

a_ref TYPE REF TO a.

ENDCLASS. "a DEFINITION

CLASS a IMPLEMENTATION.

METHOD get_instance.

IF a_ref IS NOT BOUND.

CREATE OBJECT a_ref.

ENDIF.

re_ref = a_ref.

ENDMETHOD. "get_instance

ENDCLASS. "a IMPLEMENTATION

START-OF-SELECTION.

DATA: a1 TYPE REF TO a.

DATA: a1 TYPE REF TO a,

a2 TYPE REF TO a.

a1 = a=>get_instance( ).

a2 = a=>get_instance( ).

IF a1 = a2 .

WRITE: / 'Both instances are similar.'.

ENDIF.

-


In the above example, you cannot code

CREATE OBJECT a1.

outside of the class a.

When you try to create 2 instances, a1 and a2 using get_instance (static method), all you end up doing is that creating 2 references which point to the same object.

Thus a singleton is achieved. Class a acts as a singleton here.

matt
Active Contributor
0 Kudos

>

> In ABAP Objects, a private class (rather a class which has the syntax CREATE PRIVATE appended to the definition), does exactly that, implements a singleton.

Not quite correct. While it is true that all singletons will have private instantiation, it is NOT TRUE that all classes that have private instantiation implement singletons. Private instantiation, where the instantation is done by a static method - known as a factory method - is used whenever the class designer wants to strictly control how instances are created. For example, it is frequently at a superclass level to return the desired subclass of that superclass, when the subclass isn't known until runtime.

matt

uwe_schieferstein
Active Contributor
0 Kudos

Hello Sohit

As Matthew already said there must be static methods available to instantiate a private class.

Thus, you have to look for either a factory class (different class than the private class) or factory methods within the private class.

In case of CL_HTTP_CLIENT have a look at the static methods:


CREATE_BY_DESTINATION
CREATE_INTERNAL
CREATE_BY_URL
CREATE

All of them return an instance of classes implementing IF_HTTP_CLIENT.

Regards

Uwe

Former Member
0 Kudos

thanks alot guys.