Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding inherited getters/setters

I have a class (Wall) that inherits from Sprite.

Sprite already has width and height properties. But for wall, I need to do some other additional calculations when the properties change (f.e. make sure the new size won't cause it to overlap any other walls).

So, how do I set the width property inherited from the Sprite class from within the width setter of the Wall? (or perhaps there is an alternative way to do my bounds checking whenever width is set?)

public override function set width(w:Number):void {
    //make sure it is a valid size
    //if it is, then set the width of the *Sprite* to w. How?
}
like image 980
Ponkadoodle Avatar asked Jul 04 '10 03:07

Ponkadoodle


People also ask

Are getters and setters inherited?

Do the setter/getter methods always affect only values in objects where they are declared, even called from a subclass by inheritance? You cannot inherit the methods but not the variables. You inherit everything from the parent class. Private just means that you cannot directly access it, but it is still there.

Can we override the getter method?

You can always manually disable getter/setter generation for any field by using the special AccessLevel. NONE access level. This lets you override the behaviour of a @Getter , @Setter or @Data annotation on a class.

What can I use instead of getters and setters?

You may use lombok - to manually avoid getter and setter method. But it create by itself. The using of lombok significantly reduces a lot number of code.


1 Answers

super is what you are looking for:

    override public function set width(v:Number):void {
        if(v > 100) {
            super.width = v;
        }
    }
like image 105
Juan Pablo Califano Avatar answered Oct 06 '22 11:10

Juan Pablo Califano