Public, Private, Protected, Internal: Access Modifiers in AS3 ·
25 March 2008, 01:07

Wow, I really went away for a while. Between working, wedding planning and the holidays I really lost track of posting! Ohhh where to begin again :) . Since I have gotten a number of questions regarding access modifiers in AS3, thats seems a good place to start.

What is an access modifier?

Just as it sounds, an access modifier modifies what Classes can instantiate a particular Class or access one of its properties. Not to use a phrase to define itself, but that’s really all there is to it. For example, if I have a class Foo and it has a method bar ( that returns a Beer of course, what else would a bar method do!?!? ) I have to make a decision regarding what other objects in my application should be able to request a Beer from my Foo.bar method. I have some options: I can allow all my other classes access to it, I can allow only classes in Foo’s package access to it, I can allow only subclasses of Foo access to it, or I can not allow any other classes access to it. It all comes down to what access modifier I give the method. Some examples:

public access modifier allows every Class access

// hey everyone, beer is on me!!!
public function bar() :Beer { ... }

internal access modifier allows Classes sharing this package access

//my good package-mates, have some beer on me!
internal function bar() : Beer { ... }

protected access modifier allows subclasses access

// I will only give beer to my children ( they are 21, i swear! )
protected function bar() : Beer { ... }

private access modifier allows no external access

// My beer is just that, MINE!
private function bar() : Beer { ... }

So there are a few different types of access modifiers in AS3, the real question becomes when to use each type. And for my totally unambiguous answer: it depends.

The default access modifier is internal, meaning that if you don’t define an access modifier like so:

function bar() : Beer{ … }

Classes that share this package will have access to the properties, but no other Classes will. While its perfectly fine to not declare an access modifier if you really want it to be internal its just good form to always declare your modifier. It’ll keep your co-workers from ridiculing you over your lack of tact :).

Note: In AS3, the protected and private access modifiers are only allowed to be used for a Class’ properties and methods, not the Class itself.

My preference is to always declare my properties and methods private unless the need arises to allow other Classes access. The more abstracted you can keep the inner workings of your Classes the better, because they will be more loosely coupled to other Classes and therefore more reusable and extensible. I almost never declare a variable as public, but will use implicit getters and setters to emulate a readable/writable class property.

To recap:


Jonathan Greene

comments:

  1. Thanks for the great explanation of Access modifiers in AS3. Anything related to beer is easy for me to understand! I enjoyed your sense of humor and learned a lot. Keep up the good work.

    Rob · Nov 22, 10:47 AM · #

  2. Thanks for this, I had noticed that ‘private’ didn’t allow access to subclasses and was about to just declare everything ‘public’ when I found your article. Nicely explained :)

    MichaelJWilliams · Dec 4, 07:57 AM · #

  3. can you give me some program example of default access modifier, public access modifier, private access modifier, protected access modifier?
    please….hope you’ll understand..

    kenneth · Jun 29, 01:23 AM · #

  4. Hey Kenneth, I’m not quite sure what you are asking for. Are you asking what would be an appropriate use of each type of modifier?

    Jonathan · Jun 29, 07:09 PM · #

  5. “private access modifier allows subclasses access”

    think this is a typo! Great article though.

    Caz · Sep 30, 04:33 AM · #

  6. yes, yes it is! thanks for catching that, i have updated the article.

    Jonathan · Sep 30, 06:35 AM · #

  7. mmmmmm, all this does is encourage binge drinking!!!

    Mike · Oct 27, 07:40 AM · #

  8. brb ..fridge

    nice article!

    mike · Dec 5, 05:12 PM · #

  9. Perfect! Thanks!

    April · Dec 8, 01:26 PM · #

  10. Beer actually made things easier to understand for once!

    j · Apr 20, 01:35 PM · #

Name
Email
Website
Message
  Textile Help