2006 Dec 15 11:44 AM
Hi,
I need to access a Protected method (ADOPT_LAYOUT) of class CL_REIS_VIEW_DEFAULT.
If i try to call that method by using class Instance it is giving error as no access to protected class. Is there any other way to access it..
2006 Dec 15 12:02 PM
Hi
My understanding is that you can call it only from another mthod in the class.
Regards
Arun
2006 Dec 15 12:36 PM
Hi,
You can create a sub-class of this class CL_REIS_VIEW_DEFAULT, add a new method which is in public section and call the method ADOPT_LAYOUT of the super-class in the new method. Now you can create an instance of the sub-class and call the new sub-class method.
REPORT ZCD_TEST.
class sub_class definition inheriting from CL_REIS_VIEW_DEFAULT.
public section.
methods: meth1 changing cs_layout type LVC_S_LAYO.
endclass.
class sub_class implementation.
method meth1.
call method ADOPT_LAYOUT changing CS_LAYOUT = cs_layout.
endmethod.
endclass.
data: cref type ref to sub_class.
data: v_layout type LVC_S_LAYO.
start-of-selection.
create object cref.
call method cref->meth1 changing cs_layout = v_layout.
2006 Dec 15 12:58 PM
Hi,
You can able to call if your object is in inheritance hierarchy only.
Regards
Bhupal Reddy
2006 Dec 18 6:46 AM
Protected methods are essentially for inherited calsses only. To access a protected method, inherit a class from this calls and then create and instance of the chold class and then using the instance object, call the protected method. It works when i tried simulating this example.
Award points if you find the answer helpful!
Regards,
Priya.
2006 Dec 18 7:06 AM
Hi Friend,
I tried it. Actually it is already an inherited method from some other class. So if i take inheritance from this class..it is giving error..I tried in Class builder and tried to use in an abap program...But it is giving error as the method is not found or a PRIVATE or PROTECTED . Can u please explain...elobarately why it is ...
2006 Dec 28 7:54 AM
By making the method 'protected', the designer of the class is indicating that it is not appropriate to directly invoke the method's logic from outside of the class or one of it's sub-classes. The designer wants to retain control over how/when/why the protected method is invoked.
In a well-designed class, a protected method will usually be called from within some other method of the class in which the protected method is defined. This is indeed true for ADOPT_LAYOUT.
It turns out that ADOPT_LAYOUT is called implicitly whenever a client calls the <b>public</b> method GET_LAYOUT.
Try replacing your call to ADOPT_LAYOUT with a call to GET_LAYOUT. That should work.