Difference between revisions of "Gambas/syntax/variables/classes/inheritance"
(→Inheritance and constructor: errors: bad use of vclass) |
m (Woozle moved page inheritance (Gambas) to Gambas/inheritance: reorganizing) |
(No difference)
|
Revision as of 15:23, 28 April 2013
computing: software: programming: Gambas: inheritance
Documentation
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. |
To specify that your class should inherit from the ParentClass class, insert this at the beginning of the class file:
INHERITS ParentClass
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 )