One of the first questions any beginner in OOP and Flex will ask is: What do those public/protected stuff really mean?
Well here is a short table to describe them.
As you should know by now a flex application can be structured in packages. Imagine an application as a cupboard, and it is composed of shelves (packages). In each shelf you have some boxes (classes). Now inside this boxes (classes) you have some small post-its (variables).
Now if you want your post-it data to be available from the box (class) it is in then you need to declare that post-it (variable) as private.
Imagine now that you have a post-it (variable) that contains something that is required in 2 or more boxes (classes) from the same shelf. In that case you need to declare your post-it (variable) as internal.
Another common case is when you want your post-it to be available to all other boxes no matter where they reside in the cupboard. You need to declare it public.
In your programs you will want to extend the boxes at some point (class inheritance). If you want a post-it (variable) to be available to the box it resides in (declared in) and also all the other boxes that are based on this one (the base box is for example: Documents and it can be extended to Music Documents and Pictures Documents) you need to declare it as internal.
So if we list the variables from their restrictive access level to the most general the list would be:
| Code placement | Private | Internal | Protected | Public |
| Same class | Allow | Allow | Allow | Allow |
| Descendants of class | Deny | Allow | Allow | Allow |
| Classes in same package | Deny | Deny | Allow | Allow |
| Other package | Deny | Deny | Deny | Allow |
A different keyword is const. It can be applied to any post-it (variable). If you apply it that will mean that you cannot change the value that is kept in that post-it (variable). In fact the value there is set in code at the develop time (like the name of the program/cupboard) and cannot be changed later.
Popularity: 42%
Tags: ActionScript, flex, internal, private, protected, public, scope, variables
This post was written by Virgil Cristea
Views: 2392



















[…] in regards to exposing classes and interfaces to the public. For example, you can make classes package protected so that users outside that package cannot use that class. An example is that you do have a public […]
The Science of Packages
Everyone knows about packages, but do you know of the science of packages? There are quite a few principles that deal with maintainability and reusability. Read on to learn about them, as explained by Robert C. Martin.
…
[…] Variables - Public/Protected/Private/Internal/Static - ActionScript, Flex resources… […]