Private vs. protected
Since I have gotten some comments on my public vs. private post, I thought I'd investigate a little further.
Simon Wacker mentioned that a private method is no longer private when accessed dynamically. Say that we have a class instance called myClass with a private method called makeMe. Calling the private method directly would result in an error message.
myClass.makeMe(); // results in an error message because the method is private
But calling it dynamically does not result in an error message.
myClass["makeMe"](); // does not result in an error message
This is because the private keyword is a compile time feature only. At compile time, myClass["makeMe"]() is not yet resolved to mean myClass.makeMe(), so the calling of a private method is never caught.
Darron Schall pointed out that the private keyword in ActionScript 2.0 works the way the protected keyword works in other languages. I did some research to find out what the difference is between private and protected in other languages. Here is the answer I found:
Private members are accessible by member functions.
Protected members are accessible by member functions and derived classes.
What does this mean? It means that private methods can only be used by the class that owns the methods. Protected methods, on the other hand, can be used by the class that owns the methods and by any of its subclasses.
It is true that in AS 2.0 a subclass can access a private method of its parent class.
// baseClass.as
class baseClass {
private function privateMethod() {
trace("you have called a private method");
}
}
// derivedClass.as
class derivedClass extends baseClass {
function tryPrivateMethodFromBaseClass () {
privateMethod();
}
}
// .fla that implements these classes
var myClass:derivedClass = new derivedClass();
myClass.tryPrivateMethodFromBaseClass(); // does not result in an error message
In the above code, I am creating a new class called derivedClass which is a subclass of baseClass. In one of the subclass's methods I then access the parent class's private method. It lets me. This is proof that the private keyword really does act like the protected keyword. If the private method was truly private, it would not be available to the subclass.
Lastly, Peter Hall mentioned that private methods are accessible by other instances of the same class. I tried to see what that's all about.
// someClass.as
class someClass {
// this variable will reference the other instance of the same class
var sisterInstance:Object;
private function privateMethod() {
trace("you have called a private method");
}
}
// .fla
// create two instances of the same class
var myClass1:someClass = new someClass();
var myClass2:someClass = new someClass();
// assign a reference to one instance to the other instance
myClass2.sisterInstance = myClass1;
// the following results in an error message
myClass1.privateMethod();
// this, however, does not
myClass2.sisterInstance.privateMethod();
That's fascinating. myClass1.privateMethod() throws a compile time error while myClass2.sisterInstance.privateMethod() does not, even though myClass2.sisterInstance resolves to myClass1, and thus the statements say the same thing. So this is yet another abnormality of the AS 2.0 private keyword. Don't you love how unique ActionScript is as compared to other languages? I do!





posted
by Vera (

