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

protected class

Former Member
0 Likes
721

hi ppl,

Iam new to this oops concept..i learnt that to access the contents of a protected class either we have call by subclass and another is with in itself.so how can we call a protected class with in class itself..pls do alter this code to call within itself

class snap definition.

protected section.

methods: m1.

data: d type char20 value 'PROTECTED'.

endclass.

class snap implementation.

method: m1.

write:/ d.

endmethod.

endclass.

start-of-selection.

data obj type ref to snap.

create object obj.

call method->m1.

end-of-selection.

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
688

As you noticed can only be called either from within the class or its subclass. It can't be called outside as in your example. In this case it had to be public. Call it like this:


class snap definition.
public section.
methods: m2.

protected section.
methods: m1.
data: d type char20 value 'PROTECTED'.
endclass.

class snap implementation.
method: m1.
write:/ d.
endmethod.

method: m2.
  me->me1( ). " call protected method inside the class
endmethod.
endclass.

start-of-selection.
data obj type ref to snap.
create object obj.
call method->m2.  "now you can call the public method which in turn calls the protected one
end-of-selection.

Regards

Marcin

4 REPLIES 4
Read only

MarcinPciak
Active Contributor
0 Likes
689

As you noticed can only be called either from within the class or its subclass. It can't be called outside as in your example. In this case it had to be public. Call it like this:


class snap definition.
public section.
methods: m2.

protected section.
methods: m1.
data: d type char20 value 'PROTECTED'.
endclass.

class snap implementation.
method: m1.
write:/ d.
endmethod.

method: m2.
  me->me1( ). " call protected method inside the class
endmethod.
endclass.

start-of-selection.
data obj type ref to snap.
create object obj.
call method->m2.  "now you can call the public method which in turn calls the protected one
end-of-selection.

Regards

Marcin

Read only

Former Member
0 Likes
688

Declare your class as public

and call protected method by using reference of the same class.

public class snap definition.

protected section.

methods: m1.

data: d type char20 value 'PROTECTED'.

endclass.

public class snap implementation.

method: m1.

write:/ d.

endmethod.

endclass.

start-of-selection.

data obj type ref to snap.

create object obj.

call method obj->m1.

end-of-selection.

Read only

Former Member
0 Likes
688

Hi Priyank,

Marcin's correct but there was a small typo in his post:


...
  me->me1( ).
...

Is supposed to be:


me->m1( ).

as m1 is the name of the protected method. In case you're wondering, "me" is a self reference to the instance. It is not necessary, e.g. you could write just


m1( ).

but including the self reference is considered good practice because it increases the understandability of the code.

Best regards, Erik

Read only

Former Member
0 Likes
688

thank u all