Using the super Keyword

When overriding a parent class’s method in a subclass, calls to the method in an instance of the subclass will invoke the implementation defined in the subclass. The super keyword enables you to call the method implementation defined in the superclass from a subclass.

For example, consider the code in Listing 12.4 that shows the thickborderedview class. This class extends the borderedview class given in Listing 12.2.

Listing 12.4. Using the super keyword
<library>
    <class name="thickborderedview" extends="borderedview">
        <method name="init">
            super.init();
            this.lineWidth = 10;
            this.stroke();
        </method>
    </class>
</library>

An instance of thickborderedview draws a thicker border than instances of its parent class. As you can see in Listing 12.4, the thickborderedview class overrides the init method in borderedview. It first calls super.init() to execute the init method in the parent class. It then sets the value of line Widthto 10 and calls the stroke method. By calling super.init, the thickborderedview class does not need to draw a rectangle.

Listing 12.5 shows an example that uses thickborderedview class.

Listing 12.5. Using thickborderedview
<canvas height="200">
    <include href="borderedview.lzx"/>
    <include href="thickborderedview.lzx"/>
    <thickborderedview x="40" y="40" width="50"
            height="50"/>
</canvas>

You can compile the code in Listing 12.5 by using this URL:

http://localhost:8080/lps-4.0.x/app12/thickborderedviewTest1.lzx

Figure 12.4 shows the instance of thickborderedview.

Figure 12.4. An instance of thickborderedview


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset