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

Calling a Private method

Former Member
25,056

Hi All,

i have a class CL_CRM_DOCUMENTS_PLM and private method GET_DOCUMENT_DATA.

Now how could i call this private method in my ABAP Editor ?

Regards.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
9,791

Hi,

First you need to create a an object of the class like

CREATE OBJECT class_name

Now using that object you can call the method

CALL METHOD class_name->method_name

Regards,

Chandru

16 REPLIES 16
Read only

Former Member
0 Likes
9,792

Hi,

First you need to create a an object of the class like

CREATE OBJECT class_name

Now using that object you can call the method

CALL METHOD class_name->method_name

Regards,

Chandru

Read only

0 Likes
9,791

Hi Chandru,

i get an error that you cannot create the object outside the class .

Since the method GET_DOCUMENT_DATA is private i cannot call it outside the class.

Regards.

Read only

rainer_hbenthal
Active Contributor
0 Likes
9,791

Private methods can not be called from outside the class,thats why they are private.

Read only

0 Likes
9,790

Then how could i use that private method ?

Read only

0 Likes
9,790

Private methods can only be called from another method in the same class. As this class is delivered by SAP you can do nothing with this class.

Read only

Former Member
0 Likes
9,790

Try inheriting the class CL_CRM_DOCUMENTS_PLM.

Read only

0 Likes
9,790

Inherting works only for protected methods,not for private methods. private methods cannot be called in an inherited class.

The only thing is too copy the source code into an own class or do amdification on the SAP class.

Read only

0 Likes
9,790

Check if this private method is being used by some public method. If yes u can call the public method from ur abap code.

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
9,790

Hi Rawoof,

Welcome do SDN Forum.

Please dont't forget to read the Forum Rules Document on [rules of engagement|https://wiki.sdn.sap.com/wiki/x/FgQ] to know the rules we must follow to post here.

Really you can't call any private Component outside its class.

Don't forget to close this thread once your question be answered !

Greetings.

Marcelo Ramos

Edited by: Marcelo Ramos on Jun 27, 2008 5:24 PM

Read only

Former Member
0 Likes
9,790

hi

for Accessing a private Method of a class you have to Call it from a Public Method of the same class.

Private Methods are not accessible outside the class or even in the derived class. If you want to access the Private Method inside Derived Class you have to Change Its ATTRIBUTE to Protected which behaves like Private Outside Class but As Public Inside the derived Class.

Hope this will help.

Reward if useful

Sumit Agarwal

Read only

bpawanchand
Active Contributor
0 Likes
9,790

HI

dear it is not possible to call a private method directly,Instead what you can do is you calla public method of that class inside that public method you can call the private members of the class.

regards

PAVAN

Read only

Former Member
0 Likes
9,790

hi,

plz check the steps,

1. Declare a method in public section of the class

CL_CRM_DOCUMENTS_PLM.

2. In the implementation of the public method , call the private method GET_DOCUMENT_DATA..

3. Call the public method from your report program.

Reward if found helpful.

Anirban Bhattacharjee

Read only

Former Member
0 Likes
9,790

Hii!

You cannot call the private method directly.

To call a private method,

You call a public method first,

and then from that method

call that private method.

Regards

Abhijeet Kulshreshtha

Read only

Former Member
0 Likes
9,790

hi check this example..

 Class C2 is created using CREATE PRIVATE option. That means, only the class itself and its friends can instantiate this class.

 Class C2 has a private method M2 and a private attribute , NUM. This means that these components can be accessed by class C2 itself and its friends.

 Now, C2 has granted friendship to class C1.

 So, methods of class C1 can access private components of C2 as well as can instantiate class C2.

report

REPORT YSUBDEL.

CLASS C1 DEFINITION DEFERRED.

CLASS C2 DEFINITION CREATE PRIVATE FRIENDS C1 .

PROTECTED SECTION.

DATA : NUM TYPE I VALUE 5.

METHODS : M2.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

METHOD M2.

WRITE:/5 'I am method m2 in C2'.

ENDMETHOD.

ENDCLASS .

class c1 definition.

public section .

methods : m1.

endclass.

class c1 implementation.

method m1.

DATA : OREF2 TYPE REF TO C2.

CREATE OBJECT OREF2.

WRITE:/5 OREF2->NUM.

CALL METHOD OREF2->M2.

ENDMETHOD.

endclass.

START-OF-SELECTION.

DATA : OREF1 TYPE REF TO C1.

CREATE OBJECT OREF1.

CALL METHOD OREF1->M1.

Read only

Clemenss
Active Contributor
0 Likes
9,790

Hi rawoof,

private methods can only be called from within the class. If you get an error like "cannot create the object outside the class" then the constructor is private, too. This is a technique frequently used in factory methods, in that case You should have a method like get_instance that will create the object and give you a refernece. If a method is declared private, it will usually not make sense to call it from outside. Check the wehere-used-refernce of the private method to find out how it is used in public method.

Regards,

Clemens

Read only

Former Member
0 Likes
9,790

I thank all for the instant support.

Best regards.