ActionScript: Extending Custom Classes

Extending a custom class with ActionScript is fairly straightforward. As part of my gpAS3Library, I have a button class (gpButton) that extends a shape class (gpShapes). Originally (in my AS2 version), I instantiated the shape class from within the button class and the shape class was always a separate entity. Now that I have created the button class by extending my shape class, interacting with and manipulating the button is much more tidy and easier to maintain.

First I created the main class.

?View Code ACTIONSCRIPT
package com.grasshopper.utils {
 
	public class gpShapes extends Sprite {
 
		public function gpShapes() {	
 
		}
 
		public function createShape(shape:XMLList):void {
 
                } 
        }
}

Then, to create my button class, I just extend the shape class:

?View Code ACTIONSCRIPT
package com.grasshopper.ui {
 
        import com.grasshopper.utils.gpShapes;
 
	public class gpButton extends gpShapes {
 
		public function gpButton() {	
 
		}
 
		public function createButton(xBtn:XMLList, sBtnText:String):void {
			...
			createShape(xBtn);
                        ...
		}
        }
}

So I call createShape (a gpShape method) from within the gpButton class. Because I created gpButton by extending gpShapes, Actionscript knows to look in the gpShapes class for the method (it first looks for the method within the gpButton class).

Using Class Variables

Now let’s say that I want to use a parent’s class variable from the subclass. First we define our variable in the base class (the superclass).

?View Code ACTIONSCRIPT
package com.grasshopper.utils {
 
	public class gpShapes extends Sprite {
 
                public var myShape:String;
 
		public function gpShapes() {	
 
		}
 
		public function createShape(shape:XMLList):void {
                        myShape = 'circle';
                } 
        }
}

I defined a variable, myShape in the superclass. I can access the value (or a method) in the subclass:

?View Code ACTIONSCRIPT
package com.grasshopper.ui {
 
        import com.grasshopper.utils.gpShapes;
 
	public class gpButton extends gpShapes {
 
		public function gpButton() {	
 
		}
 
		public function createButton(xBtn:XMLList, sBtnText:String):void {
			...
			createShape(xBtn);
                        trace(myShape);
                        // output: circle
                        ...
		}
        }
}

Notice that I created the variable as public. If this were a private variable, then the subclass would not have access to it.

?View Code ACTIONSCRIPT
private var myShape:String;

If you then tried to access it in the subclass, you will receive the error:

?View Code ACTIONSCRIPT
1120: Access of undefined property myShape.

Protected

Setting a variable (or function) to public allows it to be accessed by any ActionScript code where the class has been instantiated. To restrict access to only the class and subclasses, you must use the protected keyword.

?View Code ACTIONSCRIPT
package com.grasshopper.utils {
 
	public class gpShapes extends Sprite {
 
                private var myShape:String;
 
		public function gpShapes() {	
 
		}
 
		public function createShape(shape:XMLList):void {
                        myShape = 'circle';
                } 
 
                protected function getMyShape:String {
                        return myShape;
                }
        }
}

Now only the superclass and subclasses have access to the method getMyShape:

?View Code ACTIONSCRIPT
package com.grasshopper.ui {
 
        import com.grasshopper.utils.gpShapes;
 
	public class gpButton extends gpShapes {
 
		public function gpButton() {	
 
		}
 
		public function createButton(xBtn:XMLList, sBtnText:String):void {
			...
			createShape(xBtn);
                        var shape:String = getMyShape();
                        trace(myShape);
                        // output: circle
                        ...
		}
        }
}

Super

The super keyword can be used to call or add to the existing methods of the superclass. If I use it without a method, it refers to the constructor of the superclass.

The Superclass:

?View Code ACTIONSCRIPT
package com.grasshopper.utils {
 
	public class gpShapes extends Sprite {
 
                private var myString:String;
 
		public function gpShapes() {	
			myString = 'Stargate SG-1';
		}
 
                 public function getMyString():String {
                        return myString;
                }
 
        }
}

The Subclass:

?View Code ACTIONSCRIPT
package com.grasshopper.ui {
 
        import com.grasshopper.utils.gpShapes;
 
	public class gpButton extends gpShapes {
 
		public function gpButton() {	
                        super();
                        init();
		}
 
		private function init():void {
			...
                        var str:String = getMyString();
                        trace(myString);
                        // output: Stargate SG-1
                        ...
		}
        }
}

You can also use the super keyword to override an existing method of the superclass.

Note: When using the super keyword, you must use the exact number of arguments in your subclass as are used in the superclass.

The Superclass:

?View Code ACTIONSCRIPT
package com.grasshopper.utils {
 
	public class gpShapes extends Sprite {
 
                private var myString:String;
 
		public function gpShapes() {	
 
		}
 
                 public function createShape(shape:XMLList):void {
 
                } 
        }
}

Now I will override the createShape method in the subclass:

?View Code ACTIONSCRIPT
package com.grasshopper.ui {
 
        import com.grasshopper.utils.gpShapes;
 
	public class gpButton extends gpShapes {
 
		public function gpButton() {	
 
		}
 
		public override function createShape(shape:XMLList):void {
			deleteShape();
                        super.createShape(shape);
		}
 
                private function deleteShape():void {
 
		}
        }
}

That’s it. As you can see, extending a custom class with ActionScript, as with other Object Oriented languages, is not difficult and can save you a lot of development time.

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks