By js_


2011-09-24 11:27:12 8 Comments

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?

3 comments

@Pranav Hosangadi 2011-09-24 15:17:09

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

@J. Holmes 2011-09-24 12:13:16

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.

@js_ 2011-09-24 13:28:05

Thanks for a great answer! How did you know that secret functionality(Automatic Super Insertion)?

@J. Holmes 2011-09-24 14:01:00

help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/… - Under the section called Constructor Methods

@Kyle Delaney 2017-05-19 19:34:01

Does suppressing the parent constructor mean none of the ancestral constructors get called or just the immediate superclass?

@Richard Szalay 2011-09-24 12:06:12

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.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

15 Answered Questions

3 Answered Questions

[SOLVED] Chain-calling parent initialisers in python

1 Answered Questions

Calling the parent class constructor and super variable in inheritance java

1 Answered Questions

[SOLVED] Java. Super call in class which extends Object

  • 2016-12-11 10:25:36
  • krund
  • 2814 View
  • 2 Score
  • 1 Answer
  • Tags:   java super

2 Answered Questions

[SOLVED] Python calling base class using super vs static

8 Answered Questions

[SOLVED] How to force derived class to call super method? (Like Android does)

5 Answered Questions

3 Answered Questions

[SOLVED] How to avoid calling super in an as3 constructor

5 Answered Questions

[SOLVED] using constructor from the super class

Sponsored Content