PDA

View Full Version : Adding functionality to a superclass function.



andriusain
February 26th, 2009, 03:17 PM
Hello World!

I just made my jump into OOP and I am quite excited to find out such marvelous things that I did not know.

If someone has a quick answer or can point me in some direction please reply to this question:

How can I, if at all possible, modify a function from a superclass but preserving all of it's functionality? I know about the "override public function" but that is not precisely what I want.

Thank you!

ofeet
February 26th, 2009, 03:43 PM
override is actually what you want to do, but in the override you can call the original function then and your own twist:


override public function ILikesMeSomeSummerTime():void {
super.ILikesMeSomeSummerTime();
trace("but only on the weekends");
}

bLasTamos
February 26th, 2009, 05:49 PM
Maybe what you are really looking for is the Template Method design pattern.
For example, you have a super-class with function procedure(), which calls action1() and action2().
Your sub-class can override just one of the actions, keeping the functionality of procedure() intact (it would still call action1 and action2). To make sure procedure() isn't violated, you can declare it as final.

andriusain
February 26th, 2009, 05:52 PM
how do I declare procedure as final in as3 language?

cheers

bLasTamos
February 26th, 2009, 06:21 PM
final public function doNotOverrideMe() { }

andriusain
February 26th, 2009, 06:29 PM
oh thanks, I am reading now a good overview about the template method design pattern. It looks interesting. Thanks for the direction! Very much appreciated.