Understanding (AS3) access modifiers (Public, Internal, Protected, Private)
- August 26, 2009 by Emanuele Feronato
- Filed under Actionscript 3, PROgramming | 9 Comments
I see a lot of confusion around the web about access modifiers, so I am going to give a brief explication about them.
First, if you noticed the post title, you’ll see I wrote AS3 in brackets because access modifiers have not been introduced with AS3, but AS3 finally featured access modifiers.
As far as I can remember, access modifiers came from C, or at least I started studying them when I was programming with C.
Acess modifiers are anguage keywords for specifying the visibility of classes and members, that means they determine which code may access classes and class members.
Just think about a bartender in a exclusive pub that may require membership, and everything will be easier.
public (available to all code): allows access from every class… the bartender will attend everyone, no matter where he’s coming from
internal (available within the same package): allows Classes sharing this package access… the bartender will attend only people somehow related with the pub (workers that built it, people involved in furnishing, and so on)… this definition may lead to misunderstandings, so let’s say access is limited exclusively to classes defined within the current project.
protected (availabile within the same class and subclasses): allows subclasses access… the bartender will only attend customers with membership and their sons
private (available only within the same class): allows access only by the same class… the bartender will only attend customers with membership
Now the big question: is it so important to use the right access modifier?… I have to say in most small projects that you’re sure you’ll be the only one using them, you can define everything as public and forget about the rest, but obviously if you are looking for reusability and portability, you should pay attention to access modifiers.
9 Responses
Leave a Reply
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online
- Giochi casino







The title is wrong!
:)
I would say it’s important to use the right modifier. When your project inevitably grows bigger than “small” it’s helpful to remind you exactly how other classes are supposed to use those members and methods.
Of course access modifiers can only save you from yourself to a point. If you name every method and member poorly you’re likely to make mistakes no matter how tightly you’ve set access to them.
I always wondered whether using private instead of public affects performance. Anyone…?
Actually access modifiers were introduced in AS2. As AS2 was class based and is not what most people think it is. The following is actually AS1:
onClipEvent(enterFrame){
for(i = 0; i<5; i++){
//stuff
}
}
By introduced I mkeant featured in ActionScript.
Nice clear explanation of the different types, definitely worth showing to anyone confused on the matter.
“in most small projects [...] you can define everything as public and forget about the rest”
That’s a very bad practice !
Nice tutor of modifier access . thanks ..
i have a problem, how kan i find/trace a variable from a action script file to an other action sctipy file.