Override function of instance of function defined in mxml component script block

Go To StackoverFlow.com


I have a protected function defined in a script block of a component defined in mxml like so:

<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="onCreationComplete()">
    protected function onCreationComplete():void {


Is it possible to override the function in an instance of the component like so:

<gen:CreateObjectFormSubmit id="formSubmit">
          override protected function onCreationComplete():void {
2012-04-04 03:34
by MetaChrome
your sample should work perfect! what is the problem - Adrian Pirvulescu 2012-04-04 08:42


Just be careful with where and how you are declaring your super class. From the syntax provided above, it appears you might be trying to declare a <component> tag in your mxml class, in which if this is the case - the compiler treats this as a separate child object and not an actual extension.

For one, root level tags cannot declare an id (but components can - and this becomes the Class name), and you would most likely need to explicitly declare all your xml namespaces.

In order for it to be a true extension, your first <VGroup> Class would need to be named CreateObjectFormSubmit, this becomes the root tag of your extending class. There is one gotcha though, if you extend from an mxml class you cannot declare any additional children (in mxml notation because of layout rules).

If as you say an instance of your class, then no you wouldn't be able to override it, since the function scope of the <Script> tag would still reside within the root level. This is also sometimes referred to as the 'outerDocument' when declaring <component> tags.

The above wouldn't be any different doing it in regular AS like the following:

class SomethingCool extends UIComponent {
    public function addButtons():void
         var btn:Button = new Button();
             btn.id = 'formSubmit';
             //can't declare an override of Button here
2012-04-04 18:17
by Mike Petty
This is what I meant by this question, I dunno why I thought there would be anonymous class extension in Flex - MetaChrome 2012-04-04 23:11


Sure, you can. MXML component is a class , so if you create component B based on component A (B inherits from A), then you can override methods of A in B.

2012-04-04 07:20
by Timofei Davydik


to override any function, a function which u want to override should be available in parent class of ur current class.

public class A {
  public function methodtooverride():void{
    trace('in class A');

public class B extends A {
  override public function methodtooverride():void{
    trace('in class B');
2012-04-04 07:45
by ashoo_bob