joshbuhler.com

Pop Quiz on Static in AS3

by Josh on Jan.28, 2009, under ActionScript, Development, Flash, Flex

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.

  1. package example
  2. {
  3.         public class ParentClass
  4.         {
  5.                 public static const CLASS_NAME:String = "Parent Class";
  6.                
  7.                 public static function staticFunction ():void
  8.                 {
  9.                         trace ("ParentClass.staticFunction()");
  10.                 }
  11.                
  12.                 public function inheritedFunction ():void
  13.                 {
  14.                         trace ("ParentClass.inheritedFunction()");
  15.                 }
  16.         }
  17. }

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

  1. package example
  2. {
  3.         public class SubClass extends ParentClass
  4.         {
  5.                 public function SubClass()
  6.                 {
  7.                         super();
  8.                        
  9.                         trace ("CLASS_NAME:", CLASS_NAME);
  10.                 }
  11.         }
  12. }
  13.  

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.

  1. package {
  2.         import example.ParentClass;
  3.         import example.SubClass;
  4.        
  5.         import flash.display.Sprite;
  6.  
  7.         public class StaticInheritance extends Sprite
  8.         {
  9.                 public function StaticInheritance()
  10.                 {
  11.                         // These work because they are defined in ParentClass
  12.                         ParentClass.CLASS_NAME;
  13.                         ParentClass.staticFunction();
  14.                        
  15.                         // These will fail compiling because static variables & methods are not inherited by subclasses.
  16.                         SubClass.CLASS_NAME;
  17.                         SubClass.staticFunction();
  18.                        
  19.                         // However, non-static elements are inherited.
  20.                         var subClass:SubClass = new SubClass ();
  21.                         subClass.inheritedFunction();
  22.                 }
  23.         }
  24. }

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.



:, , , ,
No comments for this entry yet...

Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...