2009 Jan 23 8:09 AM
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.
2009 Jan 23 8:13 AM
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
2009 Jan 23 8:13 AM
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
2009 Jan 23 8:41 AM
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.
2009 Jan 23 8:59 AM
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
2009 Jan 28 12:07 PM