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

How to Access Protected Method of a Class

Former Member
0 Likes
17,228

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..

6 REPLIES 6
Read only

Former Member
0 Likes
5,882

Hi

My understanding is that you can call it only from another mthod in the class.

Regards

Arun

Read only

Former Member
0 Likes
5,882

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.

Read only

Former Member
0 Likes
5,882

Hi,

You can able to call if your object is in inheritance hierarchy only.

Regards

Bhupal Reddy

Read only

Former Member
0 Likes
5,882

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.

Read only

0 Likes
5,882

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 ...

Read only

0 Likes
5,882

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.