Beginning ActionScript 3.0 – super, this, setter/getter

Connect-256x256 In the last part of Constructor(@see Beginning ActionScript 3.0 – Constructor), we do a simple experiment. And the experiment shows that the constructor of the superclass will also be inherited to the subclass. If you don’t call the superclass’s constructor in the subclass’s constructor, then the compile will add some code to the subclass’s constructor automatically.

Aha, what code will be added to the subclass’s constructor? The answer is very simple:


For ActionScript and some other programming languages, if you do not define a constructor method in your class, the compiler will automatically create an empty constructor for you. If your class extends another class, the compiler will include a super() call in the constructor it generates.


super(), is a function call, calls the constructor of the superclass, eh, direct superclass. And, super, is a keyword, which means a reference to the superclass, also the direct superclass.


Suppose, we have a class, CookMaster, inherited from the Cook class, and the Cook class is inherited from Person, they all have the property, salary.

Now, if the CookMaster wants to call the method, haveBreakfast, in Person class, then it can use:


And the statement:


will call the method in Cook class.

So, the super inside the CookMaster refers to Cook, and the super inside the Cook refers to Person.


There is a corresponding keyword to “super”, “this”. “super” refers to the superclass, while “this” refers to the current instance.

The following code shows some simple usage of “this”.

package {

public class Driver{

private var licenseNo:String;
private var carType:String;

function Driver(licenseNo:String){
this.licenseNo = licenseNo;

public function setCarType(carType:String):void{
this.carType = carType;

public function getCarType():String{
return carType;

Look at the constructor of Driver, you can see the constructor needs a parameter, licenseNo, and the Driver itself has a filed named licenseNo. With the help of “this”, the compile can distinct the local variable from the instance variable.

Sometimes, you may need to pass the reference of current instance as a parameter into a function, and then you can use “this”.


In the above example, we write a method to set the value of carType, and a method to get the value of carType. The former one is call setter, the next is getter.

Setter and getter is widely use, especially when you deal with the value object. In OOP domain, how to control the access of data is an important aspect. As you see, we use the private attribute to modify the variables; because we don’t want anyone outside the class can access the variables. But, sometimes, we need to expose some information to the outside world, such as the carType here, so we provide the setter and getter as the interface for the outside.

The above example code is a general code of setter and getter. In ActionScript, it provide a different way to implement the setter and getter, and you can call it set and get accessor methods.

Rewrite the example with the ActionScript style will be as follows:

public function set type(carType:String):void{
this.carType = carType;

public function get type():String{
return carType;

The advantage of this approach is that it allows you to avoid the traditional accessor functions with unwieldy names, such as getCarType() and setCarType(). Another advantage of getters and setters is that you can avoid having two public-facing functions for each property that allows both read and write access.

Of couse, there’re some other advantages of set and get accessor methods, and I left one to the next section, :)

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “Beginning ActionScript 3.0 – super, this, setter/getter”

  1. Andrew says:

    This post was helpful, thanks!

Leave a Reply