Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0  
 
by Dave Yang, quantumwave.com - swfoo.com
Download Source Files 
 
ActionScript 2.0
 

01. Introduction
02. A little bit of OOP in ActionScript 1.0 
03. What's new in ActionScript 2.0?

04. New keywords and features for OOP
05. Scope and "this"
06. Private or protected?
07. Dynamic vs Static classes

08 Inheritance.
09. Overriding, Overwriting or Overloading?

10. Importing external class files
11. Interface
12. What is missing in ActionScript 2.0?
13. Conclusion

- discuss this tutorial -

11. Interface

When it comes to OOP, besides inheritance, another useful concept is the interface. Like Java and C#, ActionScript does not support multiple inheritance (i.e. a subclass with more than one superclass). This is because multiple inheritance can be a nightmare to debug if used improperly, getting into circular referencing and other sticky situations. In a carefully designed application, using interfaces can achieve similar results with less headaches.

An interface is a contract that specifies what methods must be implemented by a class implementing it; usually there is no relationship between the interface and the implementing class. In an interface, there is no code or property, only method signatures. Interfaces are written almost like classes, except:

  • They do not have properties
  • They ony include method names, arguments, their types, and return types
  • Method definitions end with a semi-colon, not curly braces

Here is an example of a Draggable class (the name of an interface is usually prefixed with the letter 'I'):

interface IDraggable {
   public function startDrag():Void;
   public function stopDrag():Void;
   public function isDragging():Boolean;
}
Use the implements keyword to use an interface. For example, the Child class inherits from the Parent class, and implements from the IDraggable interface:
class Child extends Parent implements IDraggable {
   public function Child() {
      // add Child constructor code
   }
   public function startDrag():Void {
      // implement the code for startDrag();
   }
   public function stopDrag():Void {
      // implement the code for stopDrag();
   }
   public function isDragging():Boolean {
      // implement the code for isDragging();
   }
}
The only thing to keep in mind with interfaces is that all the declared methods must be implemented with the declared methods signatures (i.e. same arguments and types). In a way, interfaces standardize code between different developers. When a known interface is used by a class, the developer only needs to know the interface in order to implement the same in different classes.

- discuss this tutorial -
 
©2003 Ultrashock.com - All rights reserved