Gambas/syntax/variables/classes/inheritance

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< Gambas‎ | syntax‎ | variables‎ | classes
Revision as of 22:48, 26 April 2015 by Woozle (talk | contribs) (note about "Super"; moved acknowledgements template to bottom)
Jump to navigation Jump to search

Notes

To call the ancestor's version of a method, refer to the parent as "Super".

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.