Gambas/syntax/variables/classes/inheritance
Notes
- To call the ancestor's version of a method, refer to the parent as "Super".
- If ClassB inherits from ClassA, and ObjectB is declared as ClassA, it will use ClassA's methods even if it receives an object of ClassB. To make use of virtual methods (in this case, methods in ClassB), declare ObjectB as a "Variant" instead of "ClassA".
- If ClassB inherits methods from ClassA, and ObjectB is declared as ClassB, and ObjectB is asked to execute a method that is defined only in ClassA, and that method calls another method that is defined in both ClassA and ClassB, it will apparently call the ClassA method unless invoked as "Me.method". (This needs to be verified... because how would you then call a private method?)
Documentation
To specify that your class should inherit from the ParentClass class, insert at the beginning of the class file:<gambas>INHERITS ParentClass</gambas> Note that if you are inheriting from a virtual class, you need to leave off the period (e.g. to inherit from .ListViewItem, use "INHERITS ListViewItem").
What is inherited?
The class inherits from its parent every method, property, constant and event.
Which class can be a parent class?
You can inherit any class, even a native one!
For example, you can create a custom MyListBox class that inherits ListBox but allows association a tag with each list item.
Note that you can't use INHERITS in a form class file, because forms already inherits the Form class.
Inheritance and constructor
Unlike many (or most) object-oriented languages, in Gambas each class in the inheritance hierarchy consumes the parameters passed to the constructor.
Let's suppose we have the following inheritance tree:
- MyListBox ---inherits--> ListBox ---inherits---> Control
- Control._new() does not exist.
- ListBox._new() takes one parameter: the parent control.
- MyListBox._new() takes one parameter: a name - It is just an example.
So NEW MyListBox() will take two parameters. The first will be sent to MyListBox._new(), the second to ListBox._new(). But the ListBox._new() will be called first. This way, you are sure that the ListBox control exists when you are in MyListBox._new().
You then create a MyListBox control this way:
- aMyListBox = NEW MyListBox ( "Name", aContainer )
Errors
Acknowledgements
This documentation was originally copied form the F1 Help ("help browser" contents) included with Gambas version 1.0.13, but may contain transcription errors and will probably be rewritten extensively over time. It may also not reflect later revisions of the Gambas documentation. |