When you create a new class, you must decide what unique actions you want to include
in its definition. The actions
are defined by their methods. Methods from the parent class are inherited by the subclass
that you create. Although
method inheritance is one of the primary benefits of object-oriented programming techniques, there are
times when you will want to augment a
class's functionality by adding new methods or overriding inherited methods:
-
Create a new method for your subclass if you want to add behavior that is not currently
available in
the parent class.
-
Override an existing method to modify the behavior of the parent class in your subclass.
An overridden method is one that extends a method that has the
same name and
signature on a parent class. In effect, overridden methods add some kind of functionality to
a class, but they
also call the same method on the class's parent to ensure that the method's core functionality
is preserved. When overriding a method, you provide a new method definition, but you
cannot change the method's name, signature, or
scope. You can also
overload methods to complement existing behavior. For more information on overloading, see
Overloading Methods.
To demonstrate how you can add new behavior to a class, consider the Close Button
class that was presented above. Although the Close Button
class is useful, a user of your application might accidentally click a “Close” button.
To help prevent such mistakes, you could add a confirmation dialog box to the Close
Button class. The class could have the following behavior:
-
When the Close button is clicked,
a dialog box is displayed to confirm the action.
-
If the user selects Yes
,
the frame closes. Otherwise, the frame remains open.
To implement this behavior
in a new class, review the existing class:
-
Does the parent class provide a behavior when the button is clicked on the frame?
After deciding to augment the behavior of a class, you must decide whether to override
an existing method or add a new one. In the Close Button class, the _onClick method
that serves as the
event handler is called whenever the button is clicked. You can override this method and have the
new implementation display the dialog box. If you override a method, you should execute
the same method on the parent by calling _super() in your code.
See the
SAS/AF online Help for assistance with specific
tasks.