Application Development 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: 

protected class

Former Member
0 Kudos
121

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

MarcinPciak
Active Contributor
0 Kudos
88

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

MarcinPciak
Active Contributor
0 Kudos
89

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

Former Member
0 Kudos
88

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.

Former Member
0 Kudos
88

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

Former Member
0 Kudos
88

thank u all