Interfaces as data types

Like a class, an interface defines a new data type. Any class that implements an interface can be considered to be of the type defined by the interface. This is useful for determining if a given object implements a given interface. For example, consider the following interface.

interface Moveable {
	function moveUp();
	function moveDown();
}

Now consider the class Box that implements the Moveable interface.

class Box implements Moveable {
	var x_pos, y_pos;

	function moveUp() {
		// method definition
	}
	function moveDown() {
		// method definition 
	}
}

Then, in another script where you create an instance of the Box class you could assert that the instance is of the Moveable type.

var:Moveable = new Box();

In Flash Player 7, you can cast an expression to the interface type. If the expression is an object that implements the interface, or has a base (super) class that implements the interface, the object is returned. Otherwise, null is returned.