Use the base operator in an overriding method to call the implementation of that method in its superclass.
Call a superclass implementation
When a subclass overrides a method such as OnCreate(), the overriding method replaces the inherited implementation for that subclass. Use self.base.MethodName() when the subclass must also run the superclass implementation.
For example, an overriding OnCreate() method can perform work before and after the inherited OnCreate() method:
self.OtherCallsBeforeCodeInOverridenClassIsDone();
self.base.OnCreate();
self.OtherCallsAfterCodeInOverridenClassIsDone()
In this example, execution occurs in this order:
OtherCallsBeforeCodeInOverridenClassIsDone()runs on the subclass instance.base.OnCreate()runs theOnCreate()implementation inherited from the superclass.OtherCallsAfterCodeInOverridenClassIsDone()runs on the subclass instance.
Choose where to call base
The position of the base call determines when the superclass code runs.
| Goal | Pattern | Result |
|---|---|---|
| Run superclass initialization before subclass initialization | self.base.OnCreate(); followed by subclass calls
|
The inherited OnCreate() runs first.
|
| Run subclass preparation before superclass initialization | Subclass calls followed by self.base.OnCreate();
|
The subclass preparation runs first. |
| Run subclass code on both sides of superclass initialization | Subclass calls, self.base.OnCreate();, then more subclass calls
|
The superclass implementation runs between the two subclass operations. |
Requirements and limitations
- Use
basefrom the overriding implementation in the subclass. - Call the inherited method by its method name, for example
self.base.OnCreate(). - If you do not include a
basecall, the superclass implementation is not called by the overriding method. - Call
baseonly when the superclass method's behavior is required. Calling it before or after subclass code can change initialization order.
Example: preserve inherited creation behavior
Assume a superclass defines OnCreate(), and a subclass overrides it to add its own setup. To preserve the inherited creation behavior while adding subclass work, call the superclass method explicitly:
self.SetupSubclassState();
self.base.OnCreate();
self.SetupAfterInheritedCreate()
This pattern makes the intended order visible: subclass setup, inherited creation logic, then follow-up subclass setup.
