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

access private method in a class

Former Member
0 Likes
3,703

Hi

i have doubts regarding access of class method.

my scenario is

i am having method called M1 which is in class A

now i want 2 access that method M1 in Class B which is different class..

how to do that... how to access????

i want clear explanation with syntax.

points wil be rewarded.

5 REPLIES 5
Read only

Sm1tje
Active Contributor
0 Likes
1,811

If this is a private method, you can't access it from another class. Private methods can only be accessed from within the class itself.

Read only

Former Member
0 Likes
1,811

Hi Micky,

I have a requirement where i need access to the private method of a class.

My class is - cl_wb_mime_repository

Method is - preview_mime

When i try to acess this method in my Z Program, it gives me a compile error which read as below.

"Method "PREVIEW_MIME" is unknown or PROTECTED or PRIVATE."

Can you please let me know how can we get an acess to this method in my Z Program?

Thanks,

Praveen

Read only

Former Member
0 Likes
1,811

hi,

Generally users of a class may only access the public components of that class. Sometimes, however, you might want to create a closer link between classes. The concept of friends could be of use here.

In this case a class can permit explicitly named users (friends) to access its protected and private components.

Prerequisites

All the users declared to be friends (classes and interfaces) were already created.

Procedure

Select the defining class and change to the Class Editor.

Choose Friends.

Go to change mode.

Under Friends, enter the classes or interfaces for which you want to permit access to the protected and private components of the defining class.

If you specify an interface for Friends, this means that all the classes that implement this interface have access to the protected and private components.

If you set the flag to Modeled only, no entry is made in the class pool. The friends relationship is only displayed and managed in the Class Builder. The friends relationship cannot be used at runtime.

Result

All the users specified as friends can also access PROTECTED a nd PRIVATE components of the class in question if the Modeled only flag was not set in step 4. The friends relationship is always one-sided and is not inherited. This means that friends of a superclass are not automatically friends of the subclasses.

Hope this helps, Do reward.

Read only

0 Likes
1,811

the FRIENDS concept is the one to choose here. another way to achive your goal is to implement a kind of proxy methods (public) which just calls the private methods you have to access...

Read only

Former Member
0 Likes
1,811

HI,

Say there are two classes class1 and class2.

and method M1 present in class1 as private method.

Now if you want to access this in your method of second class follow the below method.

in implementation of method M2.

METHOD M2.

DATA: class1 TYPE REF TO zclass1,

CREATE OBJECT class1.

CALL METHOD class1->M1.

ENDMETHOD.

Award points if you find it useful.

Regards,

Prasanna