10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 1-22. Subclass.as<br />

package<br />

{<br />

//Subclass of Abstract class<br />

public class Subclass extends AbstractClass<br />

{<br />

override function abstractMethod( ):void<br />

{<br />

trace("This is the overidden abstract method");<br />

}<br />

}<br />

}<br />

When the application finally implements the methods originating in the abstract<br />

class, it does so by programming to the interface (AbstractClass) but instantiates<br />

through the subclass (Subclass). So the instance, doDemo, is typed as AbstractClass<br />

but instantiated as Subclass.<br />

Example 1-23. ImplementSub.as<br />

package<br />

{<br />

//Implement Subclass of Abstract class<br />

import flash.display.Sprite;<br />

}<br />

public class ImplementSub extends Sprite<br />

{<br />

private var doDemo:AbstractClass;<br />

}<br />

public function ImplementSub( )<br />

{<br />

doDemo=new Subclass( );<br />

doDemo.abstractMethod( );<br />

doDemo.concreteMethod( );<br />

}<br />

The following shows what appears in the Output window when you test the program:<br />

This is the overidden abstract method<br />

I'm a concrete method from an abstract class<br />

At this point you may be scratching your head wondering why you should go<br />

through this kind of convolution to do what you could do with a non-abstract class.<br />

Instead of subclassing the abstract class, overriding one of the methods, and then<br />

implementing the subclass, we could have just written both methods the way we<br />

wanted them in the first place and not have to override anything. The next section<br />

attempts to answer that question.<br />

32 | Chapter 1: Object-Oriented Programming, <strong>Design</strong> <strong>Patterns</strong>, and <strong>ActionScript</strong> <strong>3.0</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!