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.

Abstract classes and overriding inheritance<br />

As you be<strong>com</strong>e familiar with design patterns, you’ll see more and more use of interfaces<br />

and its close cousin the abstract class. In <strong>ActionScript</strong> <strong>3.0</strong> the abstract class is a<br />

little problematic because no class can be actually defined as abstract. While you can<br />

use the public statement to make a class public, <strong>ActionScript</strong> <strong>3.0</strong> (and ECMAScript)<br />

chose not to include abstract classes in the language, as does Java.<br />

However, you can create an abstract class in <strong>ActionScript</strong> <strong>3.0</strong>. All you have to do is<br />

create a regular class and treat it as an abstract class. Like interfaces, abstract classes<br />

can have abstract methods that are not directly implemented. Rather, abstract classes<br />

are subclassed and any abstract methods are overridden and implemented very much<br />

like methods are in using interfaces. However, abstract classes can have implemented<br />

methods as well; so when you need both abstract and implemented methods, abstract<br />

classes are key to such design patterns as the Factory Method (Chapter 2), Decorator<br />

(Chapter 4), and the Composite (Chapter 6), as well as others.<br />

You know from inheritance that when one class subclasses another, the subclass<br />

inherits the methods of the superclass. With an abstract class, you do not implement<br />

the class but instead subclass it, and then implement the subclass and its methods.<br />

Because of the abstract nature of at least some of the methods in the abstract class,<br />

you must override them. Using the override statement, an overridden class maintains<br />

its signature but can have its own unique details. You must be sure that the<br />

name, number, type of parameters, and the return type are the same. In other words,<br />

when you override a method from an abstract class, you treat the method exactly the<br />

same as an interface method.<br />

Example 1-21 through Example 1-23 make up an application that illustrates how an<br />

abstract class works. The abstract class has two methods; a concrete one and an<br />

abstract one.<br />

Example 1-21. AbstractClass.as<br />

package<br />

{<br />

//Abstract class<br />

public class AbstractClass<br />

{<br />

function abstractMethod( ):void {}<br />

function concreteMethod( ):void<br />

{<br />

trace("I'm a concrete method from an abstract class")<br />

}<br />

}<br />

}<br />

The subclass inherits the methods and interface from the abstract class, and it provides<br />

details for the abstract method by overriding it. However, the subclass leaves<br />

the concrete class as is and does not attempt to instantiate the superclass.<br />

Inheritance | 31

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

Saved successfully!

Ooh no, something went wrong!