Overriding and Calling Parent Class Code
When you create a class, the class inherits all the properties, methods, and events of the parent class automatically. For example, if code exists for an event in the parent class, that code executes when the event occurs for an object based on a subclass created from the parent class.
The following sections describe how you can override or execute method or event code from the parent class:
Overriding Parent Class Code
Calling Parent Class Code
Overriding Parent Class Code
Usually, you want to add functionality to a new class or the object created from that class in addition to the original functionality. However, you can override the method or event code inherited from the parent. For example, suppose you create a subclass or add an object based on the subclass to a container, such as a form. You can write new code for the Click event of the class to override the event code in the parent class. In both cases, the new code, not the original code, is executed at run time.
You can also prevent the default behavior of the base class from occurring in a subclass's method or event. To prevent base class behavior, include the NODEFAULT keyword in the method or event code that you add.
For example, the following program uses the NODEFAULT keyword in a text box's KeyPress event to prevent the typed characters from displaying in the text box:
frmKeyExample = CREATEOBJECT("test")
frmKeyExample.Show
READ EVENTS
DEFINE CLASS myForm AS FORM
ADD OBJECT text1 AS TEXTBOX
PROCEDURE text1.KeyPress
PARAMETERS nKeyCode, nShiftAltCtrl
NODEFAULT
IF BETWEEN(nKeyCode, 65, 122) && Between 'A' and 'z'
This.Value = ALLTRIM(This.Value) + "*"
ACTIVATE SCREEN && Send output to main Visual FoxPro window.
?? CHR(nKeyCode)
ENDIF
ENDPROC
PROCEDURE Destroy
CLEAR EVENTS
ENDPROC
ENDDEFINE
Calling Parent Class Code
You can optimize class design by adding and executing code at different levels in the class or container hierarchy.
To execute method or event code in the parent class in addition to code for the same method or event in the subclass, precede the subclass's method or event code with the DODEFAULT( ) function or the scope resolution operator (::).
Tip
When you use the DODEFAULT( ) function, you do not need to know the name of the parent class. However, to determine all the classes in an object's class hierarchy, use the ACLASS( ) function. For more information, see ACLASS( ) Function.
For example, suppose you have a class named cmdGoBottom based on the CommandButton class that has the following code in its Click event:
GO BOTTOM
THISFORM.Refresh
This code moves the table record pointer to the bottom of the table. When you create a command button, for example, cmdGoBottom1, based on the cmdGoBottom class and add it to a form, you might decide to execute the code in parent class's Click event and display a message indicating that the table record pointer is positioned at the bottom of the table. Suppose you add only the following line of code to cmdGoBottom1's Click event to display the message, "At the Bottom of the Table":
WAIT WINDOW "At the Bottom of the Table" TIMEOUT 1
When you run the form, the message displays; however, the record pointer does not move because the code in cmdGoBottom1's Click event overrides that of the parent class. To make sure that the code in the parent class's Click event also executes, include the following lines of code instead in cmdGoBottom1's Click event:
DODEFAULT( )
WAIT WINDOW "At the Bottom of the Table" TIMEOUT 1
The following lines of code in the cmdGoBottom1's Click event show how to accomplish the same task using the scope resolution operator (::) and the name of the parent class:
cmdGoBottom::Click()
WAIT WINDOW "At the Bottom of the Table" TIMEOUT 1
As another example, the Buttons.vcx visual class library in the Visual FoxPro ...\Samples\Classes directory contains two command button classes: cmdOK and cmdCancel. Suppose a command button created from the cmdOK class exists on a form. The code in the Click event of the command button releases the form when you click the button. However, the cmdCancel command button class is a subclass of the cmdOK class. Suppose you wanted to discard changes made to a table when you click a command button created from the cmdCancel class. You can add functionality to the Click event of the cmdCancel class that calls the method code in the cmdOK class by using the following sample code:
IF USED( ) AND CURSORGETPROP("Buffering") != 1
TABLEREVERT(.T.)
ENDIF
DODEFAULT( )
The code added to the cmdCancel class reverts changes to the table using the TABLEREVERT( ) function before it uses the DODEFAULT( ) function to call the code in cmdOK class to release the form.
Note
You do not need to add the TABLEUPDATE( ) function to the cmdOK class because changes are written to a buffered table by default when the table closes.
For more information, see DODEFAULT( ) Function and :: Scope Resolution Operator.