ActionScript OOP Tips & Tricks

abstract class

Since ActionScript 2.0 does not have the “abstract” modifier, here’s one way to create a class that acts like an abstract class, by making the constructor private:

class PretendToBeAbstractClass {
    // private constructor
    private function PretendToBeAbstractClass() {}

When the following statement is compiled, the compiler will complain that one can’t instantiate from this class because the constructor is private:

var o:PretendToBeAbstractClass = new PretendToBeAbstractClass();

Although the constructor is private, but because it behaves as protected, you can still extend this class:

class MyClass extends PretendToBeAbstractClass {

What is missing is the compiler does not actually know what an abstract class is; therefore there won’t be any warning messages.

As I mentioned in the last post, you can get around all the type-checking at runtime, and even instantiating an “abstract” class at runtime like this:

var o = new _global["PretendToBeAbstractClass"]();

Or even access “private” properties from it:


Obviously these actions defeat the purpose of strict-typing, but it is possible (until runtime type-checking is implemented).

5 replies on “abstract class”

Hi Dave, by privating the class constructor you get the class to be not instanciable, true.

Although that’s a possible result of using Abstract Classes, what really defines a class as an Abstract Class is the use you made of it but not the instanciation-ban itself. An Abstract Class is intended to be a class which contents Abstracts Methods, as an abstraction of it subclasses.

Nevertheless, in spite of Abstract classes are superclasses are *not supposed* to be instanciated, I’ll wait for an abstract modifier instead of privating the class constructor.

Full Featured Abstract Class

This comment explains how to create an as 2.0 class which must be derived and contains some implementation while enforcing specific and complete implementation by all deriving classes.

Remembering that interfaces are types, we may attain the full functionality of an abstract class.

Step One:
Create an interface that contains all the methods that would normally be abstract methods.

interface IAbstract
function myAbstractMethod();

Step Two:
Create the base class that will work as your abstract class. (The base class must cast a reference to itself as the interface type implemented by extending classes. See bellow.)

class Abstract
private var sub:IAbstract;
// Private constructor disallows instantiation
private function Abstract()
// Cast a reference of the extending class instance to the interface type
// which contains the abstract methods.
sub = IAbstract(this);
//Flash Player 7 Only
if(sub == null) // invalid cast will return null in Flash player 7
throw(“Classes extending MenuBase must implement IMenu”);
// end Flash Player 7 Only
/* For Flash Player 6 (or 7) use this runtime type check
if(!(sub instanceof IMenu))
sub = null;
trace(“Classes extending MenuBase must implement IMenu”);
//Because sub is of type IAbstract no compiler error is thrown

Here is the foundation of this example. We store a reference to the current instance of the class extending Abstract in the variable sub.

sub = IAbstract(this);

This allows us to invoke the abstract method myAbstractMethod without a compiler error. If the extending class does not extend the interface, the cast returns null and we throw an exception. This forces the extending class to implement the interface IAbstract. The result is an uninstantiable class which contains some implementation and enforce specific and complete implementation by all deriving classes.

Step Three:
The extending class must simple extend the Abstract and implement IAbstract

class Extending extends Abstract implements IAbstract
// methods and properties here.

Leave a Reply

Your email address will not be published. Required fields are marked *