What are Access Methods?
Back to Listing

If you have programmed in object-oriented languages like AS3 or Java before, then you might have caught wind of things called "access methods". These special functions play an important role in maintaining the integrity of structures in programs. For those who are already experts, this post will not be discussing anything new, but I provide it as a stepping stone into more interesting topics regarding access methods and encapsulation. For those that seek to increment their programming vocabulary, read on!

First, let me define what an "access method" is. Access methods are functions in a structure or class that allow other structures to have read and/or write permission to internal properties.

In other words, these special methods allow limited access to what would be private variables in the class. Under normal circumstances, private variables (or internal properties) cannot be accessed by anything except members of the class itself. Sometimes, that level of restriction might be undesired.

Consider the case of a Circle class defined in the following way:

public class Circle extends Object{
    private var _radius:Number;
    public function Circle(__radius:Number){
        _radius = __radius;
    }
}

Here, we see the _radius property defined as a private variable. We need it private so that other objects cannot make it negative by accident. Yet, that means that if some other object needed to know the radius to perform a function (which seems like a common case), it couldn't.

However, if we provide an access method, the other object could. All we really need to do is create a public function in the Circle class that returns the value of the radius:

public function getRadius():Number{
    return _radius;
}

Now, other objects can call circle.getRadius() in order to read the value of a circle object's radius property. This kind of a method is often known as a getter since it gets the value of a private property.

While this looks pretty good, AS3 actually offers specific support for such an access method. In AS3, the word get is actually a keyword. Using this keyword in a function declaration allows you to declare the function specifically as a getter function. Therefore, instead of the above function, you can use the following:

public function get radius():Number{
    return _radius;
}

This may look effectively identical to the first function definition, and that's because it almost is. However, using the get keyword tells the compiler to treat "radius" like a property instead of a method. Because of this, instead of needing to call circle.getRadius(), you can use circle.radius. Using AS3's inbuilt support for access functions, you can use radius like a property of a Circle object.

However, with only a get method defined, you cannot set the value of the variable. If you were to try and do circle.radius = 5, the program would throw an error. In other words, the radius property is read-only. Other objects and find out its value, but they cannot change it. Only the Circle class can make changes.

So, what if you do want other objects to make changes yet preserve the rule that the radius can never be less than 0? You can use another kind of access method, called a setter, which will allow other objects to edit the property in question. In AS3, the set keyword is used to create these.

Consider the example below for radius:

public function set radius(value:Number):void{
    if(value <= 0)
        throw new Error("Radius cannot be less than 0!");
    _radius = value;
}

The above function will actually change the internal _radius variable of the circle object. However, because of the conditional, there is a restriction imposed on the possible values. Anytime something makes an attempt to change the radius to a negative number, an error will be thrown by the program. Therefore, we preserve the non-negative rule while also allowing other objects to modify the radius.

With the setter method above, it now becomes possible to do circle.radius = 5, but trying to do circle.radius = -1 will fail.


That basically sums up how to use access methods. In summary, getter and setter methods allow restricted access to private variables in object-oriented languages. In AS3, the get and set keywords can be used to declare access methods specifically, but to do this properly, it might be worth noting a couple of conventions:

  • The name of the private variable itself should begin with an underscore (_) as you see in my above circle class. This prevents naming conflicts.
  • The names of the getter and setter functions should be the same.
  • It is common practice that the names of the access methods be the name of the private variable without the leading underscore.
  • The setter method takes exactly one argument which is by convention called value.
  • A property that only has a getter method is called a read-only property.
  • A property that has both a getter and setter method is said to be read-write.

While this sums up how to use access methods, it by no means concludes discussions about them. In a future post, I will discuss some of the nuances and caveats that come with using access methods in AS3 (and in general). After that, I will also attempt to investigate whether or not access methods should be used at all, and if so when and why to use them.

Related Posts

Leave a Comment

Please refer to the Legal Guidelines for license information.