2014 Feb 06 6:08 AM
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
2014 Feb 06 6:30 AM
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..
2014 Feb 06 6:16 AM
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
2014 Feb 06 6:17 AM
Hi Anand,
What is lv_sum here? where have you declared it? in public or protected?
BR,
Shahir Mirza
2014 Feb 06 6:29 AM
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
2014 Feb 06 6:30 AM
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..