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 Protected and Private method from own instance.

0 Likes
3,919

Hi Experts,

I am facing problem in calling Protected and Private method from own instance. Kindly Give a suggestion that how to call.

Ex:

CLASS cl_super DEFINITION.
PUBLIC SECTION.
DATA: gv_text TYPE char10.
METHODS: percentage IMPORTING sub1 TYPE i
                               sub2 TYPE i
                     EXPORTING sub3 TYPE i.
PROTECTED SECTION.
METHODS: display IMPORTING total TYPE i.
ENDCLASS.
CLASS cl_super IMPLEMENTATION.
METHOD percentage.
sub3 = sub1 + sub2.
ENDMETHOD.
METHOD display.
WRITE: 'This is total', total.
ENDMETHOD.
ENDCLASS.
*& **** Calling


DATA: lr_ref TYPE REF TO cl_super.


lr_ref->display( EXPORTING total = lv_sum ).


Thanks and Regards,

Anand

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,533

Hi Anand,

               as of my knowledge, we cant access private and protected methods directly by using instances. if you want to access private and protected methods outside the class , crate a public method and call private method inside it.

Exmp:

method mt_publicmethod.

call method mt_privatemethod.

endmethod.

-> call this public method by using instance..

4 REPLIES 4
Read only

VijayaKrishnaG
Active Contributor
0 Likes
1,533

Hi Anand,

I think you have not created instance for class.

CREATE OBJECT LR_REF.

Before calling method, the reference variable should have the Instance of the class.

Regards,

Vijay

Read only

Former Member
0 Likes
1,533

Hi Anand,

What is lv_sum here?  where have you declared it? in public or protected?

BR,

Shahir Mirza

Read only

0 Likes
1,533

Hi Experts,

I have created lv_sum in my main program that is outside class and also I have created object for the reference. Kindly advice on calling PROTECTED AND PRIVATE METHOD outside class for the own instance.

Ex:

CLASS cl_super DEFINITION.
PUBLIC SECTION.
DATA: gv_text TYPE char10.
METHODS: percentage IMPORTING sub1 TYPE i
                               sub2 TYPE i
                     EXPORTING sub3 TYPE i.
PROTECTED SECTION.
METHODS: display IMPORTING total TYPE i.
ENDCLASS.
CLASS cl_super IMPLEMENTATION.
METHOD percentage.
sub3 = sub1 + sub2.
ENDMETHOD.
METHOD display.
WRITE: 'This is total', total.
ENDMETHOD.
ENDCLASS.
*& **** Calling


DATA: lr_ref TYPE REF TO cl_super.

data: lv_sum type i.

create object lr_ref.


lr_ref->display( EXPORTING total = lv_sum ).


Thanks and Regards,

Anand

Read only

Former Member
0 Likes
1,534

Hi Anand,

               as of my knowledge, we cant access private and protected methods directly by using instances. if you want to access private and protected methods outside the class , crate a public method and call private method inside it.

Exmp:

method mt_publicmethod.

call method mt_privatemethod.

endmethod.

-> call this public method by using instance..