Skip to Content
avatar image
Former Member

How to use a private class

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • avatar image
    Former Member
    Feb 13, 2008 at 07:20 AM

    U can use a class by its public methods only

    or

    using its FRIEND class

    Reward if useful

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 13, 2008 at 07:36 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 15, 2008 at 06:21 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

    • >

      > 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

  • avatar image
    Former Member
    Feb 16, 2008 at 09:56 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 17, 2008 at 03:58 PM

    thanks alot guys.

    Add comment
    10|10000 characters needed characters exceeded