Categories
ActionScript OOP

private, protected & public

In ActionScript 2.0, there are “private” and “public” modifiers, but there is no “protected”. However, “private” behaves like “protected” – i.e. subclasses and instances can access private members; so there is no true private scope.

To make sure the compiler catches access to private properties/functions, strict-typing has to be used:

class SomeClass {
     private var:somePrivateProperty:String;
 }

// without specifying the type of the instance, the compiler will not
// prevent statements from accessing private properties; therefore,
// o.somePrivateProperty is accessible

var o = new SomeClass();
trace(o.somePrivateProperty);

// by specifying the type, the compiler will catch acess to
// private members:
var o:SomeClass = new SomeClass();

Because type-checking is only performed at compile time, there is no guarantee that private members cannot be accessed at runtime.

By default, if the “public” modifier is left out, the member is assumed to be public.

2 replies on “private, protected & public”

Not a bug – it’s a feature! 😎

Seriously, I’d have liked to see this implemented differently too, but because ActionScript 2.0 code is actually compiled to AS1, and there is no “private” in AS1, this is a Fact of Life until runtime type-checking is implemented.

Leave a Reply to Carlos Cancel reply

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