Getter & Setter Inheritance Gotcha

I remember reading something about this a year ago on Flashcoders, but never ran into it until today. Basically, if you extend a class that has a getter and a setter on it, your sub-class can access them just fine. However, if your sub-class defines a getter or setter of the same name, it overwrites the one on the superclass.

So, if make a new setter function that does something slightly different in the sub-class, since the sub-class can no longer call the getter, it fails.

That blows.

4 Replies to “Getter & Setter Inheritance Gotcha”

  1. What you’re saying is right, but I don’t understand when you say it fails. The following case works, but is this an example of what you’re trying to do?

    class test2 {

    private var _x:Number;

    function test2() {
    _x=0;
    };

    function set x(p:Number):Void {
    _x = p;
    trace(‘_x=’+p);
    }

    }

    class test1 extends test2 {

    function test1() {
    trace(‘test1.x=3’);
    x=3; // traces x=3
    trace(‘test1.x=-3’);
    x=-3; // traces x=0
    }

    function set x(p) {
    if (pWhat you’re saying is right, but I don’t understand when you say it fails. The following case works, but is this an example of what you’re trying to do?

    class test2 {

    private var _x:Number;

    function test2() {
    _x=0;
    };

    function set x(p:Number):Void {
    _x = p;
    trace(‘_x=’+p);
    }

    }

    class test1 extends test2 {

    function test1() {
    trace(‘test1.x=3’);
    x=3; // traces x=3
    trace(‘test1.x=-3’);
    x=-3; // traces x=0
    }

    function set x(p) {
    if (p<0)
    super.x = 0;
    else
    super.x = p;
    }
    }

  2. I was going to ask if Flash had a super scope… I know CF does, but I also know that there are similarities and differences between the two platforms… and the two can be pretty stark.

    But if you can use the super scope, you’re not overwriting your parent class’s original method, you simply need to prepend the access to the original implementation with ‘super.’ in order to use it.

    Laterz,
    J

  3. This works:
    class SuperClass
    {
    private var _foo:String = 'bar';

    public function get foo():String
    {
    return _foo;
    }

    public function set foo(val:String):Void
    {
    _foo = val;
    }

    function SuperClass()
    {
    }
    }

    class SubClass extends SuperClass
    {
    function SubClass()
    {
    }
    }

    var o:SubClass = new SubClass();
    trace(o.foo); // bar
    o.foo = 'cow';
    trace(o.foo); // cow

    This, however, does not when you try to set it because it assumes it’s read-only (really, it doesn’t compile because it’s looking for a function value):

    class SubClass extends SuperClass
    {
    public function get foo():String
    {
    return _foo;
    }

    function SubClass()
    {
    }
    }

    We had a situation at work where the getter wasn’t present, but the setter was and you always got undefined. I can’t duplicate it now, so will have to get back to this.

Comments are closed.