Understanding AS3 super() statement

One interesting statement that’s almost unexplained in the web is super().

Before explaining super()‘s features, let me talk about superclasses.

From Wikipedia: In computer science, a superclass is a class from which other classes are derived. A superclass is also called a parent class’. The classes that are derived from a superclass are known as child classes, derived classes, or subclasses. We can also say that a class A extends class B when A is a subclass of the B superclass.

Ok, now we know a superclass provides data (instance variables) and functions (methdods) to be inherited by the subclass.

So what can you do with super() statement?

With this constructor you can call the superclass or methods of the superclass (superclass’ functions) by just using the super prefix as a reference.

Let’s take this class:

1
2
3
4
5
6
7
8
9
10
11
12
package {
	import flash.display.Sprite;
	public class real_class extends Sprite {
		public function real_class(s:String) {
			trace("I am the text contained in the real class");
			trace(s);
		}
		public function another_function() {
			trace("I am the text passed in another function");
		}
	}
}

This is our superclass, and it’s not the main class inside our Flash project.

The main class is this one:

1
2
3
4
5
6
7
8
9
10
package {
	import flash.display.Sprite;
	public class super_example extends real_class {
		public function super_example() {
			trace("I am the text contained in the main class");
			super("I am the text passed in the real class");
			super.another_function();
		}
	}
}

As you can see, at line 3 I am not extending the Sprite class as usual, but the real_class one.

So in this case I could remove the import flash.display.Sprite; line.

At line 5 I am tracing some text, then at line 6 I am calling the superclass passing another text and at line 7 I am calling a metod, that is a function in the real_class you can find at line 8 that outputs more text.

The result is this one:

I am the text contained in the main class
I am the text contained in the real class
I am the text passed in the real class
I am the text passed in another function

Using superclasses will improve reusability, it means you are writing code that can be used again to add new functionalities with slight or no modification

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (30 votes, average: 4.40 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 8 comments

  1. Dan

    on August 10, 2009 at 10:03 pm

    Hey,

    Actually the textbook opinion is prefer composition over inheritance.
    Well, I never really understood why so I used inheritance frequently. Cause I thought just like you hey nice, I put common stuff in the parent and add custom stuff to the child class.
    After I while though I realized it often costs me time .. jumping back and forth between parent and child classes to make sure things happen as intended.

    I guess it’s because it increases my ‘mental workload’. With composition I only have to worry about a class’ public API -while with composition I have to worry about the whole class!
    So nowadays I use inheritance only when it absolutely makes sense, like when a child class is really a variation of the parent class, not just borrowing some functionality.
    For some reason I feel more relaxed nowadays. ;-)

    Ciao, Dan

  2. Andrew Jacobs

    on August 10, 2009 at 10:06 pm

    Now that you mention it, it would be great to see a superclass that uses constructor parameters which follow the Strategy pattern. That way you could derive subclasses from in-game Sprites and assign them different behaviors by passing in things like a “AIStrategy” or a “PhysicsStrategy”.

    Thanks for the post, Emanuele.

  3. Josh

    on August 11, 2009 at 7:11 am

    I understand the super() command, but I always wondered why I would use it.

  4. ViolentAJ

    on August 11, 2009 at 9:21 pm

    Also, don’t forget that if the super-class’s constructor had arguments, that you can call super(arg0, arg1, … argX), and you would HAVE to if the super’s constructor had arguments.

  5. Xavier

    on February 21, 2010 at 3:04 pm

    “So in this case I could remove the import flash.display.Sprite; line. ”

    you can remove it only if you dont use Sprite class inside.

    adding this var s:Sprite=new Sprite(); somewhere and removing import gives an error. Actually thats what i dont understand.

  6. Fernando Silva

    on June 29, 2011 at 3:52 pm

    Thanks a lot for this knowledgement!

  7. vxcriss

    on August 13, 2011 at 11:39 pm

    Very nice explanation and to the point, thanks. I just spent 15 minutes trying to figure out how this is called cause it wasn’t working with parent as it does in PHP :)

  8. Muhammad bin Yusrat

    on December 6, 2011 at 8:03 pm

    This is without a doubt the most easy to understand explanation of Super() for AS3 on the internet.

    Hats of to you broh!

    Thanks!