Why super() can be used in Constructor only?

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

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?

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 :stuck_out_tongue:

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.

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 :stuck_out_tongue: )
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]

AS3 super() statement;

http://livedocs.adobe.com/specs/actionscript/3/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=as3_specification152.html

this works, just tested it:


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


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:


import mychild;

var it:mychild = new mychild();

it.childSpeak();

output:


parent constructor
child constructor
child speaks
parent speaks

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

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

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();
      }
   }
}

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