Access Control Modifiers in ActionScript

What the hell are access control modifiers!?

That’s an entirely valid question. Access control modifiers modify well, what has access to your method/variable/constant/class. To explain that a little bit, access control modifiers control which classes can use a method or access a variable or create an object of a certain class. Confused? Me, too.

Okay, but why do I even need to control the access to stuff?

Again, a good question. Let’s say that you have a class called Person and you create an object of the Person class and call it bob. All instances of the Person class have an instance variable called age which is of the type Number because… that’s what an age is. [Note: If you’re a bit lost, you might want to go review your OOP basics :) ] Anyway, we want to set bob’s age to -5. Why? Just because we do, alright? So, anyway, we’re going to do the natural thing and write

bob.age = -5;

Which in theory is fine, except that you now have a person with a negative age. And then dogs start sleeping with cats and everything just sort of goes downhill. So, the point is that we don’t want just anyone doing just anything to our object’s age. Therefore, we want to control the access to the variable so that no one can screw around and set the age to ‘q’. Instead, we might create a method called setAge that outputs some kind of an error if you pass it an argument that is not a number greater than zero. [or equal to zero, I guess. That’s a separate issue]

Now, you’re probably thinking, “But I’m not a fool. I’m not going to set the age equal to a letter! That’s stupid!” Yeah, but if you’re working on a project with some other programmers and one of them is stupid and wants to set poor bob’s age equal to ‘q’ , then you’re in trouble.

Enough theory, let’s actually talk about the four different modifiers.

Public vs. Internal

Public is well… public. Anything can access it. Internal means that it can only be accessed within the same package. It’s important to note that internal is the default setting which means that

internal var age:Number;

is the same thing as

var age:Number;

However, it’s good to explicitly write internal when you want something to be internal. That way, when you’re looking back at your code, you know that you meant for something to be internal and didn’t just leave it blank by mistake.

Note: Classes can only be declared as public or internal. This will make more sense in a second

Protected vs. Private

Let’s start with private. A private method or variable/constant cannot be accessed by anything. And I mean anything. Why then even have a private method? Picture a car. A car has several public methods that you can access. You can turn the wheel, you can turn the key. The private methods that a car has are the things that happen when you interact with the public methods. For example, when you turn the key, a number of things happen to start the car that you really don’t care about. However, all of these things could be divided into little private methods. In fact, the car does not want you to be able to control some of its internal workings, which is why it provides public methods for you to use to interact with it.

Hopefully, that made sense. Protected is just like private except that subclasses have access. It’s fairly self-explanatory.

So now do you see why classes must be declared as public or internal? A class really doesn’t do you much good if another class can’t even interact with objects of that class. [You’re thinking “But what about the class that is compiled when I run the program? I never even instantiate that!” well, yeah. But the compiler needs to be able to access it. If you really don’t believe me, try declaring a private or a protected class. It won’t work.]

Actual syntax

The order: modifier comes before what the heck the thing is… examples:

public class Square

private var age:uint;

protected function area(side:uint) :uint

Not that difficult.

How do I choose?

This is tough. If you’re really not sure, play it safe and make it less accessible. It’s easy to make something more open than restrict access later in terms of modifying other code. If you really have no idea, just make methods or variables private unless you have a specific reason to believe that it needs a different access (Like, for example, you have a class called Shape and you want to make subclasses. Private methods won’t do you much good because they won’t be accessible to the subclasses.) In general, it’s fine to make classes public unless you have a really good reason to make it internal.

Ultimately, it doesn’t even matter that much. In most of the applications you make, it honestly won’t matter if a variable is internal or protected. However, it’s important to recognize situations where this is extremely important and think carefully about access control in those cases. But don’t sit there for an hour wondering if your method should be public or private. Just write some code.

Note: Although this is specific to AS3, many of the principles apply to other languages. However, the internal modifier is not found in most languages and, therefore, some things are a little bit different. In other words, if you’re actually programming in Java, go read a Java tutorial.


People also view

Leave a Reply

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