OOP4AS2#6: Private members

This entry talks about using the private keyword to restrict access to members of a class. It should prove to be a fairly short topic, but there’s more coming in the days ahead.

This post will build on concepts from previous posts, so I would encourage you to have a look at the previous 5 entries before starting on this one.

Read on…

Often in OOP, you will have members in a class that you want to prevent other objects from accessing. For example, it is good form to avoid direct access to a class’s properties by another object. You will typically also have helper methods in a class that carry out tasks for other methods of that class, but that you don’t want other objects to access.

In AS1.0, the only things you could do to prevent access was documenting your class well, and prefix private member names with a “$” or “_” (like $myPrivateVariable or _dontTouchThisMethod()). In ActionScript 2.0, class members are “public” by default (they can be accessed directly by other objects), but we can use the “private” keyword to ensure other objects can’t access that member directl… sort of.

The private keyword is easy to implement, just insert it before a member declaration in your class block, like so:

// in GThing.as
class GThing {
// myName is a private property:
private var myName:String;
function getGreeting(p_salutation: String ): String {
return p_salutation+" "+myName;
}
function GThing(p_myName:String) {
myName = p_myName;
}
}
// in the FLA
var myG:GThing = new GThing("Bob");
myG.getGreeting("Howdy"); // traces "Howdy Bob"
myG.myName = "Frank"; // uh oh! compiler error

The third line of the FLA code above will generate the following compiler error: “The member is private and cannot be accessed.” This is because we are trying to directly access the private myName member from outside of the class. However, you’ll notice that the getGreeting() method and the GThing constructor both access the myName property directly, but they do not generate a compiler error. This is because they belong to the same instance – access to private members is only restricted to other objects (this includes other instances of the same class).

Specifying a method as private is just as easy. Just preface its declaration with private as such:

private function myPrivateMethod() { ... }

You can also preface members with “public”, but because members are public by default, it’s unlikely you’ll use it often.

Unfortunately, as with most ActionScript2.0 features, private member control is limited to compiler time. This means that it’s very easy to bypass, as in the following two examples in the FLA:

// bypasses restriction because the compiler can't match
// the member name when it is accessed dynamically:
var myG1:GThing = new GThing("Bob");
myG1["myName"] = "Frank";
trace(myG1.getGreeting("Bonjour")); // "Bonjour Frank"
// bypasses simply because myG2 isn't strongly typed, so
// the compiler doesn't check it:
myG2 = new GThing("Harold");
myG2.myName = "Tuna Head";
trace(myG2.getGreeting("Ciao")); // "Ciao Tuna Head"

But still, if used properly, it provides a nice reminder that you are doing something you shouldn’t, which comes in particularly handy on projects with multiple developers. It also aids code comprehension and documentation, making it easier to pick out the public members that comprise the interface.

’til next time, happy coding… and remember to take a gander at “Flash MX2004 Demystified” when it hits the shelves for more FMX2004 goodness.

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

2 Comments

  1. Don’t forget that ‘private’ in AS2 is really ‘protected’ in other languages – i.e. subclasses can access private members.

    See these examples:

    http://www.swfoo.com/archives/000009.html

    http://www.swfoo.com/archives/000009.html

Leave a Reply

Your email address will not be published. Required fields are marked *