Search code examples
actionscript-3oopsubclassing

When subclassing in AS3, is a new constructor function necessary?


Basic OOP question...

I want to add a couple functions to the Array class so that my program will be amazing and make me rich and famous overnight.

So I create a new subclass NewArray which extends Array. Do I need to write a constructor method for NewArray? If I leave it blank will it just use the parent's (Array's) constructor method?

Thanks


Solution

  • Yes, if you leave it blank it'll use the super-class' default constructor, which in the case of an Array is actually a constructor with a default value set:

    Array(numElements:int = 0)

    So by default you'll be creating an array with zero elements, which I guess is probably what you want anyway.

    And don't forget this note from the docs:

    You can extend the Array class and override or add methods. However, you must specify the subclass as dynamic or you will lose the ability to store data in an array.

    http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html