Super() Explained

November 20, 2008 by jared 

The super() class is used to simply call on the particular class from which you are extending from.

In this example we have 3 files:

SuperExample.fla –> Which uses ChildClass.as as the document class.
ChildClass.as –> Our child class which extends from ParentClass.as
ParentClass.as –> Our parent class, which extends from GrandParentClass.as
GrandParentClass.as –> Our grand parent class, which extends from a Sprite class

In ParentClass.as we have a constructor which accepts a String. We’ll use this later on in ChildClass.as . Inside the constructor we have two trace statements, one tracing from the ParentClass itself, and one to accept a string as a parameter.

In ChildClass.as we can see one line in the constructor,

super("Hello from the ChildClass");

Where super is actually calling on the ParentClass constructor, passing in a string “Hello from the ChildClass”, which in turned is “supered” to its extended class GrandParentClass.as

Click here to download the files in this example.

Comments

5 Responses to “Super() Explained”

  1. Walmik on May 29th, 2009 8:23 am

    Hi this is indeed a Simplistik explanation and thank you very much for it. Though if you dont mind i d like to know, what is th reason of calling the parent function if we are already extending it?

    Many thanks in advance

  2. jared on May 29th, 2009 8:32 am

    Well you call your parent class when you want to access a particular method in your parent class. For example if your parent class has a generic function plays an exported sound in the library, you can call the parent with super.playSound(“Sound.mp3″), and so in future projects you simply have to extend to your “generic” parent class so you don’t have to re-code again. Mostly it’s wanting to keep your code clean and organized for reusability; thats pretty much what object oriented is about: reusability. Not sure if what I say makes sense but I do know where you’re coming from and all I can say is keep going and soon you’ll be explaining it to someone else!

  3. Dave on August 5th, 2009 3:55 am

    Nice and clean… thank you very very much…

  4. rbrill on April 27th, 2010 3:34 pm

    A great little tutorial, why can’t they all be like this? Usually they get so complex you end up getting lost altogether. A nice straightforward, simple explanantion of super()

  5. JC on June 24th, 2010 5:31 am

    I think I understand this tutorial. I’m essentially BRAND NEW to actionscript and it indeed was simple. Question though – once you press ctrl+enter is something supposed to happen to let you know that it’s working right? Thanks

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!