# Why super() can be used in Constructor only?

**URL:** <https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182>\
**Category:** flash\
**Created:** [July 5, 2008, 1:45pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182 "2008-07-05T13:45:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonnieraymond](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bonnieraymond](https://forum.kirupa.com/u/bonnieraymond)\
**Post date:** [July 5, 2008, 1:45pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/1 "2008-07-05T13:45:29Z")

</div>

I just wondering why super() can be used in Constructor only but not in other function?  
for example:

```auto
package
{[INDENT] public class mySubClass extends myClass
{[INDENT] public function mySubClass()
{[INDENT]super(); // ok, you can use super anywhere in this constructor
[/INDENT]}

override public function myFunction()
{[INDENT]super(); // NO WAY MAN!
// extending code here
[/INDENT]}
[/INDENT][/INDENT][INDENT] }
[/INDENT]}

```

What I want is to extend a function without rewriting the old one. Anybody has an idea?

---

<div class="post-metadata">

**Author:** ![nikefido](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nikefido](https://forum.kirupa.com/u/nikefido)\
**Post date:** [July 5, 2008, 1:51pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/2 "2008-07-05T13:51:55Z")

</div>

mmmm, I think you can still call the function w/ something like “parent::myFunction();” or since you are extending myClass, “myClass::myFunction”

don’t quote me on that, I may have just stolen syntax from PHP 😛

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 5, 2008, 2:01pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/3 "2008-07-05T14:01:01Z")

</div>

A super statement may only be used inside a constructor.

Reason why might be because you are extending something, you need to instantiate (if ever) the extended class when you are instantiating your subclass.

---

<div class="post-metadata">

**Author:** ![nikefido](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nikefido](https://forum.kirupa.com/u/nikefido)\
**Post date:** [July 5, 2008, 2:04pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/4 "2008-07-05T14:04:58Z")

</div>

I found your answer with this adobe example: (little disclaimer - this is in the adobe flex area of livedocs, and I didn’t test before posting 😛 )  
[http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs\_Book\_Parts&file=10\_Lists\_of\_data\_166\_6.html](http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=10_Lists_of_data_166_6.html)

It appears that a super does not have to be only within the constructor (at least, in the form of a method).

Check out where it says “TypedArray overridden methods”

the function overrides the “push” method of an array and still calls “push” via:  
ActionScript Code:  
[LEFT][COLOR=#0000ff]super[/COLOR].[COLOR=#0000ff]push[/COLOR].[COLOR=#0000ff]apply[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]this[/COLOR], args[COLOR=#000000])[/COLOR]  
[/LEFT]

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 5, 2008, 2:07pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/5 "2008-07-05T14:07:24Z")

</div>

AS3 super() statement;

[http://livedocs.adobe.com/specs/actionscript/3/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs\_Parts&file=as3\_specification152.html](http://livedocs.adobe.com/specs/actionscript/3/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=as3_specification152.html)

---

<div class="post-metadata">

**Author:** ![nikefido](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nikefido](https://forum.kirupa.com/u/nikefido)\
**Post date:** [July 5, 2008, 2:21pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/6 "2008-07-05T14:21:40Z")

</div>

this works, just tested it:

```auto

package {
	
	public class myparent {
		
		public function myparent() {
			trace('parent constructor');
		}
		
		public function parentSpeak():void {
			trace('parent speaks');
		}
	}
}

```

```auto

package {
	
	import myparent;
	
	public class mychild extends myparent {
		
		public function mychild() {
			trace('child constructor');
		}
		
		public function childSpeak():void {
			trace('child speaks');
			super.parentSpeak();
		}
	}
}

```

Implementation code:

```auto

import mychild;

var it:mychild = new mychild();

it.childSpeak();

```

output:

```auto

parent constructor
child constructor
child speaks
parent speaks

```

---

<div class="post-metadata">

**Author:** ![nikefido](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nikefido](https://forum.kirupa.com/u/nikefido)\
**Post date:** [July 5, 2008, 2:24pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/7 "2008-07-05T14:24:05Z")

</div>

flex uses as3, so it’s methods / structure is (mostly) the same  
super() != super.method

My suggestion to the OP is to use super.myFunction(); which should go to the overridden function in the parent class and perform that code

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 5, 2008, 2:42pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/8 "2008-07-05T14:42:26Z")

</div>

Sorry fido, didn’t really read your posts properly. Thought you were saying you could use super() outside cnstrctr. Your examples work fine 😉

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [July 5, 2008, 3:34pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/9 "2008-07-05T15:34:37Z")

</div>

```auto
package {
   public class mySubClass extends myClass {
      public function mySubClass() {
         doSomething();
         super();
      }
      override public function set width(value:Number):void {
         super.width=value<100?100:value;
      }
      override public function myFunction(param:*):void {
         doSomething();
         super.myFunction(param);
         doSomethingElse();
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![bonnieraymond](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bonnieraymond](https://forum.kirupa.com/u/bonnieraymond)\
**Post date:** [July 5, 2008, 5:52pm UTC](https://forum.kirupa.com/t/why-super-can-be-used-in-constructor-only/265182/10 "2008-07-05T17:52:59Z")

</div>

aha, so the answer is super.myFunction(); I really stupid, why couldn’t I find this soon. Thank you guys! :party:
