Tag Archives: classes

Understanding OOP – Introduction

Everything that has an instance name,is an Object. Each object inherits the properties and methods of its class. The Class is basically the “prototype” of an Object,and as such,you can have as many objects as you want from a single class.

In AS3,we’ve two ways of creating objects,one is using the “new” keyword,and the other is to just declare the variable typing its datatype(this method only works for top-level classes like Strings,Arrays,ints,Numbers and so on).

var numberObject:Number = 5; //This is a Number object.
var myOtherNumber:Number = new Number(5); // this is also a Number object.

Lets take a look at how a custom Class is structured:

package myPackage
class MyClass


First we’ve the Package,which can be used to group classes with similar functionality or that are meant to work with each other.Using the above example,we could import that class to use like this,based on its package:

//This imports only "MyClass"
import myPackage.MyClass;

//This imports all the classes inside the package "myPackage"
import myPackage.*;

Next,we’ve the class itself,and inside it is where we put our variables,properties and function.Its important to note that the class name should be the same as the file name.In our example,we should name that file “MyClass.as”.

The most important function a class has,is the constructor.That is what let us create an object based on that class.Constructor functions are always public(in AS3).You won’t need a constructor for static classes(more on another tutorial).

public class MyClass
//this is the constructor function
public function MyClass() {


Objects can define how their methods and properties can be accessed by other objects.Those are called “access modifiers“.We’ve 4 types of access modifiers in AS3:

public: Classes,methods or properties declared public can be accessed by any other object.
protected: Methods or properties declared protected can be accessed by any subclass object(more on another tutorial).
private: Methods or properties declared private can be accessed by the object itself only.
internal: The class,property or method can only be used by classes inside this package.

Access modifiers are another step in providing abstraction to programming.It lets you provide an object with methods that are public(can be accessed anywhere),but the complex functionality is hidden through the protected,private and internal modifiers.

To create a custom function or variable for your object,is simple:

public var myVar:String = "This is myVar";

public function myFunction():void {
trace("This is myFunction");

To use our created class,we instantiate it using the “new” method

var myObject:MyClass = new MyClass();

To access its method or variables:


Classes also have properties(which differ abit from variables).They normally are used to set internal variables and possibly run another internal function. To create a property,you need to use the special “get” and “set” method.
get” is used to retrieve the variable,but not allowing you to change its value(it simulates a read-only enviroment).
set” is used to write the variable,where you can run custom functions after changing its value:

private var _myProperty:String;

public function get myProperty():String {
return _myProperty;

public function set myProperty(value:String):void
_myProperty = value;
trace("myProperty value was changed! Value = " + value);

The property is then handled just like a variable:

myObject.myProperty = "myValue";

Output :

myProperty value was changed! Value = myValue