Understanding (AS3) access modifiers (Public, Internal, Protected, Private)

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (15 votes, average: 2.80 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.

9 Responses

  1. The title is wrong!

    :)

  2. 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.

  3. mookstar says:

    I always wondered whether using private instead of public affects performance. Anyone…?

  4. Nathan says:

    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
    }
    }

  5. Nathan says:

    By introduced I mkeant featured in ActionScript.

  6. Porter says:

    Nice clear explanation of the different types, definitely worth showing to anyone confused on the matter.

  7. Wojtek says:

    “in most small projects [...] you can define everything as public and forget about the rest”

    That’s a very bad practice !

  8. 30ml says:

    Nice tutor of modifier access . thanks ..

  9. Quintus says:

    i have a problem, how kan i find/trace a variable from a action script file to an other action sctipy file.

Leave a Reply

flash games company