Posted by: Yogesh Puri on: April 30, 2008
I have been thinking to write on Access Modifiers in AS3. An access modifier is a modifier that changes the visibility of a class or its members. The actual use of access modifier depends upon the requirements as sometime these are used to encapsulate or hide information where as sometime you provide it for making the information open and accessible for outside objects or classes.
There have been a lot of changes between AS2 and AS3 access modifiers and it’s behaviors.
in AS2 the access modifiers were something like this
Public: The public modifier provides access to external members i.e. those are not a part of same package or same class to modify / invoke public members.
Private: The private modifier in AS2 was more like protected where it provides the private member’s access to it’s subclass not the external classes.
Now the changes have been made by Adobe in the new version of Actionscript. Following is the behavior new access modifiers are having
Public: Same as Actionscript 2
Private: The private modifier is very much private for the owner class where the private members can only be called/modified by the owner class itself. The private
members are not exposed to external, instances or extended classes.
Protected: You can call the Protected method/members in a subclass i.e. the class which is inheriting from the class having protected members. Now with the protected modifier gives access to it’s members to only it’s sub classes you can invoke or modify the protected members from sub-class.
Internal: The internal methods or properties are build to be shared across a common package. You need not to extend or inherit to the classes having Internal members(Where as we do inherit in case of protected) for accessing them. The internal members/methods are like public to the classes those are in same package.
Following is an Example i have build to make it a little bit clear. You can download the source files from this Link
Following is a Base Class having all type of members
Base.as
package com.tis.demoAccessModifier
{
public class Base
{
/**
* Added by Yogesh
* Date : 29th APR’ 2008
* Modified :
* Function: Base
* Description: Constructor for the class
* Parameters: @
* Returns:
*/
public function Base():void
{
//– Uncomment following to Call the private member…
//iAmPrivate();
//—– The private members are very strictly bounded to it’s defination class
//—- where the private members can only be called by the class itself. The private
//—- members are not exposed to external, instances or extended classes
}
/**
Public method in Base Class
*/
public function iAmPublic():void
{
trace(”I am public method in Base Class “);
}
//———- Private Method in Base class ———–
private function iAmPrivate():void
{
trace(”I am private method in Base Class “);
}
//——– Protected Method in Base class ———–
protected function iAmProtected():void
{
trace(”I am protected method in Base Class “);
}
//——– Internal Method in Base Class ————-
internal function iAmInternal():void
{
trace(”I am Internal Method in Base Class “);
}
}
}
//————- InternalModifierDemo.as ——–
package com.tis.demoAccessModifier
{
public class InternalModifierDemo
{
//————— Import Base class into this class to use Internal —————
// The internal methods or properties are build to be shared across a common package
// so you need not to extend or inherit to the classes having Internal members for
// accessing them
//——————————————–
import com.tis.demoAccessModifier.Base;
//———- Constructor ————-
public function InternalModifierDemo()
{
var baseInstance:Base = new Base();
baseInstance.iAmInternal();
//————- You can call Public function as well ——-
baseInstance.iAmPublic();
//———– Following will throw the Error ——
//baseInstance.iAmPrivate();
//——— Following will throw error because of Protected type ——-
//baseInstance.iAmProtected();
}
}
}
//————- ProtectedModifierDemo.as ———
package com.tis.demoAccessModifier
{
import com.tis.demoAccessModifier.Base;
public class ProtectedModifierDemo extends Base
{
public function ProtectedModifierDemo():void{
//—- You can call the Protected Method in this class because the class is
//—- an extended class of the Base class which gives access to it’s protected members
//—- to only it’s extended members
super.iAmProtected();
//———— The private function will not work here
//super.iAmPrivate();
//———– The Internal function works here because the classes are of the same package
super.iAmInternal();
//——– The public will also work in this class as the public members are exposed to both
//——– external, Instances and the extended classes
super.iAmPublic();
//———— Call overridden Protected Method ——-
iAmProtected();
}
override protected function iAmProtected():void
{
trace(” Now i am an overridden method from child”);
}
}
}
Put following Code in your FLA on First Frame
import com.tis.demoAccessModifier.InternalModifierDemo;
import com.tis.demoAccessModifier.ProtectedModifierDemo;
var iModifier:InternalModifierDemo = new InternalModifierDemo();
var proModifier:ProtectedModifierDemo = new ProtectedModifierDemo();
[...] O mejor piénsatelo antes de declarar en AS3 un método privado. Cuando lo haces nada ni nadie puede sobreescribirlo, ni siquiera las clases que extiendan la tuya. Si crees (aunque sea remotamente) que vas a necesitar sobreescribir esos métodos o propiedades, mejor cúbrete las espaldas declarándolos “protected”. Un rápido resumen de cómo se comportan los modificadores de acceso en AS3 se puede ver aquí. [...]
[...] we have our Phone objects instantiated. We are now able to access its properties (assuming their access modifier is set to [...]
[...] we have our Phone objects instantiated. We are now able to access its properties (assuming their access modifier is set to [...]
[...] Flexcoms Weblog – Access Modifiers [...]
May 30, 2009 at 5:34 pm
Thanks for putting this post together.
[f]