Public, internal, protected, and private

I was trying to explain the difference between some of the major AS3 attribute keywords this past week (public, internal, protected, private) so came up with some analogies to help with the explanations. I figured I’d share… here is your life as an object class:

Public class members are accessible to all objects, everywhere. Think of this as public health. You can observe a sneezing, coughing, unhealthy looking person walking down the street, and chances are that you’ll keep your distance to avoid having them affect YOUR health.

Internal class members are accessible to all objects within a common class package. This would be like your home alarm code. The alarm code is freely available to all members of the household, but not given to outsiders.

Protected class members would be your genes. These are qualities of you that are hidden within the inner workings of your body and are not outwardly visible; however, they will be passed along to your children.

Private class members are reserved exclusively to a single object. This would be your mail. A letter addressed to you may not even be opened by your spouse or your children; it is private information reserved exclusively for you.


4 comments so far

  1. Porter on

    Nice analogies you have there. Just one thing though, show me one spouse that won’t look at her husbands mail haha, good luck trying to had anything from them :P
    On a serious note, it’s a nice way to help clear up the confusion. I’ll be linking to this in my link dump since I see it as a nice source to clear up something most coders I know aren’t entirely familiar with.

    • bigmac on

      Okay, maybe it’s not a perfect analogy… ;-)

  2. […] reminder of why flash is such a great platform to work with, lots of nice info none the less. 3) Variable scopes in OOP – Some nice analogies to help you remember the variable scopes public, private, internal and […]

  3. Andreas on

    Haha… Nice analogies.

    Not what I would have used, but they work.

    You might also want to include some sort of table (or just text) that explains how this applies to Flash so non-experienced users can understand it. Perhaps some sample code of what would and wouldn’t work.

    Most people who can understand your examples and make the application to code probably don’t need these explanations.

    Still, made me smile. ;)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: