Extended Class Private Field

Monkey Forums/Monkey Programming/Extended Class Private Field

aonyn(Posted 2011) [#1]
Hi all,

I encountered something which I am not sure if it is expected behavior, but I want to ask and clarify my understanding.

I created a class which extends another class.
The compiler gave me an error after trying to access a private field from the parent class.
By creating a public get method for the field, I was able to access the field from the child, so it is not really a problem.

It does however leave me with the question, if the child class is an extension of the parent class, should a private field of the parent class be an internally accessible field also of the child or extended class, or is it expected behavior for the child to not have direct access to private members of the parent?

I considered posting this in the bug forum, but decided to post here first, because I am unsure if this is normal behavior in monkey.

Regards,
Dave


Tibit(Posted 2011) [#2]
I thought private was like protected. I thought that's the way I have used it so far. However if testing says otherwise I'd trust the testing.

I'd expect something like this:
private = access from within and from any extended class and anything in the same module
public = no limit


Hima(Posted 2011) [#3]
Hmm, I thought I answered this already but somehow my post is gone.

Anyway, yeah private is different from protected. But I think private in Monkey is also special that if the child class is in the same module ( same file ) as the parent class, then it can access the private attribute/method of the parent class. Here, I found it in the language reference.

"
Note that private class members are not private to the class, but to the entire module. This means that code outside of the class but within the same module can still access class private members.
"


aonyn(Posted 2011) [#4]
Thanks Hima,

That explains my question.

I created the parent class as its own module, and import it into a separate module for my extended class.

So if I put both within the same module, I would be able to directly access private fields of the parent from within the child.

Thank you for the explanation.
My understanding had been a bit different than how monkey was really handling it.

Thanks also Tibit for your reply, and my (I guess incorrect) expectation was more inline with what you describe.

regards,
Dave