ActionScript Tutorial – Encapsulation, Get and Set

Ready to take the plunge into the wild world of oo ? Then here’s a quick tutorial that will get you started on one of the most important concepts behind oo -encapsulation. In this post I will explain what the concept of encapsulation is, how to use it to make your code easier to manage, more resistant to errors, and best of all, more reusable.

In oo programming, the concept of encapsulation is just what it sounds like. Within each set of code, or class, we make sure that we control what is available to the outside world. This control is more important than it seems; consider the following example:

public class MyMonster {

private var _myMonsterBirthday:Date;

public function MyMonster():void{

_myMonsterBirthday = new Date()

}

}

To invoke a new instance of MyMonster you could do so by the following:

var goblin:myMonster = new MyMonster();

This would invoke the constructor argument class in the class myMonster (the public function with the same name as the class big FYI: you cant call private constructors in AS3 – so no “private function MyMonster()” we will learn how to get around this with something called a singleton). Calling the constructor sets the variable myMonsterBirthday to the current date. Because the variable _myMonsterBirthday is private, we can’t do this: goblin.myMonsterBirthday .The reasoning behind this resriction is that we need to let the myMonster class manage it’s own internal structure. That way we can not only stop errors from propigating through the code, but we are also creating modular structures that can be swapped in and out for each other. Structured development gurus will tell you to code for the interface, not the implementation. This will become more clear as we move along.

It’s good practice to make all of the variables of a class private, and only expose them through controlled ways. You can do so by creating a public method in the class that manages assignment to the variable. One of the best ways of doing this is with getters and setters. These flags tell allow you to skip the () in a typical method call. In the MyMonster class you could have a set of shortcut functions to set the age of the little beast like so:

public function set myMonsterBirthday(age:int):void{

if(age!=0){

_myMonsterBirthday = age;

}

}

public function get myMonsterBirthday():int{

return _myMonsterBirthday

}

In the above example, not only do the getter and setter methods allow us to control and isolate the internal variables of the MyMonster class, they also allow us to reference the class like this:

goblin.myMonsterBirthday = 1;

instead of

goblin.myMonsterBirthday(1);

Incidentally, this is also why I like to use the _ character when declaring private class variables. It helps to avoid compiler errors when you have a var name and a getter function signature that should logically be the same thing.

Happy Coding!

About Curious Minds
We are a web development firm in New York and Chicago, providing development resources and consulting for websites and mobile apps since 2004.