Call Us: +1-888-227-1645

ActionScript Tutorial – Encapsulation, Get and Set

Eric Meyer

3 min read

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{
   _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


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!

From the blog

Latest Articles

Let's build something amazing together

Give us a ring and let us know how we can help you reach your goals. Or if you'd like, start a chat. We're usually available 9-5 EST. We try to respond to every inquiry within one business day.

Phone number

Technologies and services we work with:

Laravel Laravel
WordPress WordPress
React ReactJS
EmberJS EmberJS
woocommerce WooCommerce
next.js NextJS
gatsby Gatsby
Shopify Shopify
VueJs VueJS
contentful Contentful
next.js JAMStack
gatsby Laravel Jigsaw
WPEngine WP Engine
Laravel Livewire Laravel Livewire
Netlify Netlify