Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3: overriding protected var in subclass
    text
    copied!<p>I'm having a blackout here. I thought I understood these principles, but I can't seem to get it working anymore. I want to let a DeleteButton inherit from a general Button class. This DeleteButton should alter the protected padding values and have a static label. This is what I have:</p> <pre><code>public class Button { private var _labelText:String; protected var _paddingX:Number = 10; protected var _paddingY:Number = 5; public function Button( labelText:String ):void { _labelText = labelText; } } public class DeleteButton extends Button { public function DeleteButton():void { _paddingX = 5; _paddingY = 2; super( 'x' ); } } </code></pre> <p>Now, I thought the altered _paddingX and _paddingY values in the inherited class would bubble up to the super class. But they don't. The DeleteButton is still constructed with the default values of the super class. I can't figure out how to do it anymore. What am I missing here?</p> <p>Thanks in advance.</p> <p><strong>Edit:</strong></p> <p>I guess I had AS3's implementation mixed up with PHP's. This 'equivalent' example in PHP is perfectly legal:</p> <pre><code>class Button { protected $_paddingX = 10; protected $_paddingY = 5; public function __construct( $label = '' ) { var_dump( $label . ':' ); var_dump( $this-&gt;_paddingX ); var_dump( $this-&gt;_paddingY ); echo '&lt;br&gt;'; } } class DeleteButton extends Button { public function __construct() { $this-&gt;_paddingX = 5; $this-&gt;_paddingY = 2; parent::__construct( 'x' ); } } $button = new Button( 'browse' ); $deleteButton = new DeleteButton(); // this outputs: string(7) "browse:" int(10) int(5) string(2) "x:" int(5) int(2) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload