‘protected’ implementation in Actionscript 3 is BROKEN

Consider following code:

public class BaseClass{
public function modifyProperty(property:String, value:Object):void{
this[property] = value;
}
}

public class SubClass extends SubClass{
protected var count:int;
}

public function test():void{
var testObj:BaseClass = new SubClass();
testObj.modifyProperty('count', 3);
}
view raw gistfile1.as This Gist brought to you by GitHub.

Theoretically, the call at line no. 13 should not cause any error as count is defined for testObj as a field in SubClass, but it gives a Cannot create property count on SubClass. Interestingly if we move modifyProperty method to SubClass or make count public, it works without any error.

So it can be said that we cannot access protected fields dynamically in a subclass.

This entry was posted in Codeprix and tagged . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>