Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't I have to call super() in constructor when class extends Sprite in actionscript3?

I always don't call super() when I extends Sprite.
But doesn't not calling super() cause any problem?

Till now, I don't have any problem and I have never seen code which call super() in constructor which class extends Sprite.

How about TextField?
I don't have any problem about TextField, too.

How to know whether I should call super() or not?

like image 544
js_ Avatar asked Sep 24 '11 11:09

js_


3 Answers

If flash doesn't detect a call to super() in your child constructor then flash will implicitly call super() before your child's constructor. So:

public class Parent {
    public function Parent() {
        trace("Parent");
    }
}

public class Child extends Parent {
    public function Child() {
        trace("Child");
    }
}

new Child();
// Parent
// Child

So your child constructor essentially looks like this

    public function Child() {
        super(); // <-- Added by flash! 
        trace("Child");
    }

So, no, omitting an explicit call to super() will not usually adversely affect your child's class.

So why would you want to explicitly call super()?

The first reason is flash will only ever automatically generate a parameterless call to super, meaning that if your parent classes constructor requires arguments, then you will need to explicitly call it with those arguments. If you omit the super(args...) call in this case, you will get a compiler error.

Second, if even your parent has a parameter-less constructor, you can use super() to control the order that the constructors execute. Flash will always insert the call before the childs constructor. So if you wanted to change that order. Then

public class Child extends Parent {
    public function Child() {
        trace("Child");
        super()
    }
}

would do it in the opposite order. Or you could do:

public class Child extends Parent {
    public function Child() {
        // work before initilizing parent 
        super()
        // work after initilizing parent
    }
}

Lastly, there is a very obscure way to not call your parents constructor by saying:

public class Child extends Parent {
    public function Child() {
        if(false) super()
    }
}

Because flash sees there is a call, it doesn't insert one. However because its behind a if (false) it never gets called, so the parent class never gets initialized.

like image 70
J. Holmes Avatar answered Oct 18 '22 20:10

J. Holmes


If you don't call super() explicitly, Flash will do it automatically before all other code in your constructor.

If you call super() explicitly, it will be called at the line on which you wrote it.

However, note that you can not set or get any this or super properties or call any methods before the super class is instantiated

like image 29
Pranav Hosangadi Avatar answered Oct 18 '22 19:10

Pranav Hosangadi


You can safetly exclude the call to the base constructor. If you do not call super() in the constructor, the compiler will add a call to the base constructor with no arguments.

like image 20
Richard Szalay Avatar answered Oct 18 '22 20:10

Richard Szalay