Abstract Classes


Abstract classes play a major role in the Factory and Template Method patterns. Although ActionScript 3.0 doesn't natively support them, we can still use the concept of abstract classes and abstract methods. An abstract class is a class that is always extended and never instantiated directly. Its use is similar to that of an interface, but there is one major difference: An interface defines only the public method signatures, but an abstract class defines both the interface and the implementation.

An abstract class uses something called an abstract method, which has no functionality but serves only as a placeholder. In other languages such as C# and Java, you can define these abstract methods using the abstract keyword that tells subclasses they must override this method. Because ActionScript 3.0 does not have an abstract keyword, you might consider the convention of throwing an exception inside the abstract methods. Such an approach won't throw an error during compilation, but it will during runtime. The bottom line is that there is no sure way to enforce abstract methods in ActionScript 3.0.

You must know about two specific keywords when you're working with abstract classes in ActionScript 3.0. The first is the override keyword. Subclasses must use this keyword to override an abstract method defined in a base class. The method signature must also match exactly.

The other keyword is final. This term can be used by abstract classes that define methods its subclasses cannot override. We'll use the final keyword when we define Template Method patterns.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net