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 (27 votes, average: 3.56 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 12 comments

  1. GabrielBianconi

    on August 26, 2009 at 5:47 pm

    The title is wrong!

    :)

  2. Andrew Jacobs

    on August 26, 2009 at 7:30 pm

    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

    on August 27, 2009 at 8:47 am

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

  4. Nathan

    on August 28, 2009 at 11:19 am

    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

    on August 28, 2009 at 11:20 am

    By introduced I mkeant featured in ActionScript.

  6. Porter

    on September 9, 2009 at 9:07 pm

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

  7. Wojtek

    on February 10, 2010 at 6:07 pm

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

    That’s a very bad practice !

  8. 30ml

    on February 23, 2010 at 1:15 pm

    Nice tutor of modifier access . thanks ..

  9. Quintus

    on March 10, 2010 at 1:46 pm

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

  10. Kawika

    on September 26, 2010 at 3:22 am

    thanks, I need a refresher on access modifiers in AS3.

  11. Eric

    on February 13, 2011 at 10:58 am

    Like mookstar said: Does anyone know if using access modifiers slows down execution time?
    Because I know that for sure it affects.

    Thanks.

  12. Pepe

    on January 7, 2012 at 8:37 am

    No, it doesn’t affect performance using private vs. public.