Beginning ActionScript 3.0 – Override&Overload

Admistrative-256x256 In the discussion of set and get accessor methods, I said, I left one advantage of using the set and get accessor methods to this post. Of course, I’ll show you. But, I won’t show you the answer directly. You know, show you the answer directly won’t impress you, so, let’s see some code first, :)

Override

Add the following code:

  1. public var classDescription:String = “Person”;

and

  1. public var classDescription:String = “Cook”;

to the Person class and Cook class correspondingly.

Now, you can use the variable classDescription to show you which class you’re using, by using instance.classDescription.

If the instance is a reference of Person class, then this statement equals to “Person”; if the instance is a Cook type reference, then it should be “Cook”. The members in the subclass take the place of the members in the superclass, and we call it override.

Test the movie, and then you’ll get the following result.

clip_image002

This compiler error is all because, in ActionScript, properties that are declared with the var or const keywords are inherited but cannot be overridden. To override a property means to redefine the property in a subclass. The only types of property that can be overridden are methods—that is, properties declared with the function keyword.

So, if we need to override a property, then the set and get accessor methods will be helpful. Remember, the set and get accessor are methods, and you can use them just like accessing the property.

And the solution of this example will be as follows.

At the Person class:

1st, change the variable name, eh, as you like;

2nd, add a new get accessor. So, the code is as below.

  1. private var description:String = “Person”;
  2.  
  3. public function get classDescription():String{
  4. return description;
  5. }

Turn to the Cook class, we also need to change the variable name, remember don’t use the same name with the variable in superclass. Then add a get accessor method, this method should be start with “override”, and the method name should be the same with the get accessor in the superclass. And the code is as below.

  1. private var cookDescription:String = “Cook”;
  2.  
  3. override public function get classDescription():String{
  4. return cookDescription;
  5. }

This solution is call method overriding in object oriented programming, which is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.

A subclass can give its own definition of methods which also happen to have the same signature as the method in its superclass. This means that the subclass’s method has the same name and parameter list as the superclass’s overridden method. Constraints on the similarity of return type vary from language to language, as some languages support covariance on return types.

Method overriding is an important feature that facilitates polymorphism in the design of object-oriented programs.

Overload

After the override, let’s turn to a new concept, overload. For overload, here we’ll just give some introduction, because ActionScript doesn’t support this mechanism. So, the following discussion is just a concept introduction, you can’t put them into practice.

In general, overload means carry too much. In OOP, it means carry too much function.

Look at the following statement:

  1. var a:int = 1+1;
  2. var b:String = “1″+”1″;

As you know, “a” is equals to 2, and “b” is equals to “11”. The plus sign in the first statement means plus in arithmetic, while in the second statement means string concatenation.

Well, you see, the plus sign has two functions here, one is plus, the other is string concatenation. But how can we know which function it will take when we use it? You can notice that, if we put two numbers with the plus sign, then we’ll get the sum of the two digits; if we put two strings, then we’ll get the concatenation of the strings. So, what function it takes depends on what you input.

This kind of overload, we call it operator overload. Remember the overload of plus operator is built-in in the ActionScript, you can use it, but you can’t redefine it or define other operator overload.

Besides operator overload, there’s widely use overload call method overload. Method overload can provide a uniform interface when you deal with different types of data.

For example, if you need to write a function to compare the data, the input can be integers, Strings or some other kinds of data. Then you can write several functions to deal with different types of data, all the functions have the same name but different signatures (you can take signatures as parameters).

  1. function compare(a:int, b:int):Boolean{
  2. // function implemention
  3. }
  4.  
  5. function compare(a:String, b:String):Boolean{
  6. // function implemention
  7. }

Now, if you to compare integers or strings. You just need to write

  1. compare(a,b);

Ignore the data type of “a” and “b”.

For overload and override, there is an explicit difference. Overload happens in one class, while override needs two or more classes, because override needs inheritance.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

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

4 Responses to “Beginning ActionScript 3.0 – Override&Overload”

  1. Emil Tams says:

    As far as I know, to this date, AS3 isn’t capable of doing method overloading…

  2. minidxer says:

    There is no function overloading in AS3. However, we can implement a system that mimics function overloading.

  3. Can you put an example for us how would be the system implementation you said?

    Thanks.

  4. Stojan Ilic says:

    Overloading is not supported by ECMA Script, so Actionscript wouldn’t have overloading. There is some methods to simulate overloading (like make Singleton class when you can’t have private constructor), but it’s too much for “Beginning” actionscript.

Leave a Reply