Enforcing Abstract Classes in AS3

One of the most common criticisms of ActionScript is that it doesn’t include some of the more advance features of other object-oriented languages. For example, Abstract classes. Abstract classes are similar to Interfaces in that they provide a common API for: implementers of the interface or subclasses of the Abstract class. At the same time, Interfaces and Abstract classes guarantee this common API for classes from other packages to work against. The Abstract class takes it one step beyond the Interface in that it also adds real functionality to the base class which can reused in its subclasses.

So if ActionScript doesn’t enforce the Abstract class, how can you guarantee that someone down the line doesn’t start abusing it? A passable answer to this question is the seldom-used “self” keyword, as seen here:

public function AbstractClass(self:AbstractClass)
{
	if(self != this)
	{
		throw new Error("Whoa!  You can't instantiate AbstractClass directly suckah!");
	}
}

The drawback to this method is that it’s not enforced at compile-time. Instead, the error will be thrown at runtime and alert the developer that they did something wrong. Further, there’s nothing stopping the developer from then catching the error and ignoring it. Ruh roh. So there is no real solution that guarantees the Abstract nature of the class is respected, but at least this little warning will deter all but the most irresponsible of programmers.

Spread the love!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

0 Response to “Enforcing Abstract Classes in AS3”


  • No Comments

Leave a Reply

Powered by WP Hashcash