Pop quiz: In AS3, are static methods & variables inherited by subclasses?

Answer: No. Static methods and properties of classes are not inherited by subclasses. You can however access the static members of a parent class from within it’s subclasses.

This question came up today as I was helping a coworker debug something, and Flex Builder was throwing an error saying that it couldn’t find a property on the class we were working in.

We were stumped for a bit, because we were trying to access a static property defined in the parent class, and were trying to access it via the subclass. We were stumped, using the logic that the properties and methods of the parent class, including the static ones are inherited. At least, as long as they’re public or protected, they should be, right?

Here’s a quick example that proved us wrong though. Suppose we have a class ParentClass which defines a few methods and properties, some static, some not.

package example
{
	public class ParentClass
	{
		public static const CLASS_NAME:String = "Parent Class";
 
		public static function staticFunction ():void
		{
			trace ("ParentClass.staticFunction()");
		} 
 
		public function inheritedFunction ():void
		{
			trace ("ParentClass.inheritedFunction()");
		}
	}
}

If we then extend this class, at first glance you might think that static items are inherited:

package example
{
	public class SubClass extends ParentClass
	{
		public function SubClass()
		{
			super();
 
			trace ("CLASS_NAME:", CLASS_NAME);
		}
	}
}

If you create a new instance of SubClass, when the constructor runs, you’ll see CLASS_NAME: Parent Class in the output panel. Inheritance, right? The subclass is using a variable defined by the parent. But that’s where it ends. This works, because subclasses can access static members of their parent classes, from within the class.

If we now try to use the subclass in our application, you’ll see where the inheritance of static items ends.

package {
	import example.ParentClass;
	import example.SubClass;
 
	import flash.display.Sprite;
 
	public class StaticInheritance extends Sprite
	{
		public function StaticInheritance()
		{
			// These work because they are defined in ParentClass
			ParentClass.CLASS_NAME; 
			ParentClass.staticFunction();
 
			// These will fail compiling because static variables & methods are not inherited by subclasses.
			SubClass.CLASS_NAME;
			SubClass.staticFunction();
 
			// However, non-static elements are inherited.
			var subClass:SubClass = new SubClass ();
			subClass.inheritedFunction();
		}
	}
}

You’re free to access the static items in ParentClass, as long as you access them via ParentClass. As they aren’t inherited by SubClass, they cannot be accessed through SubClass. You could however, write your own static methods on SubClass, which then access ParentClass for the values, and returns them, if you really needed to get the static items via SubClass.

For more info, check out the entry on static variables in the ActionScript 3.0 Language Specification.

After a quick survey both in the office and via Twitter, I’ve found that the answers received were a bit 50/50 both ways, so if you didn’t know the answer, you’re not alone.

Originally posted on January 28, 2009