<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - INHERITS
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="inc+en">Previous</a>&nbsp;
<a href="input+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
INHERITS
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>INHERITS</b> <u>ParentClass</u></pre><p>

Put this on the beginning of a class file to tell <a href="../def/gambas+en">Gambas</a> that
the current class inherits the ParentClass class.
<p>
<h2>What is inherited ?</h2>
<p>
The class inherits from its parent every method, property, constant
and event.
<p>
<div class="warning"><table class="none" border="0"><tr><td width="40" valign="top"><img border="0" src="../../img/warning.png" align="center"></td><td valign="top">
You must use the <a href="me+en">ME</a> keyword to access the inherited elements from the class inside.
</td></tr></table></div>
<p>
<h2>Which class can be a parent class ?</h2>
<p>
You can inherited any class, even a native one!
<p>
For example, you can create
a custom MyListBox class that inherits <a href="../comp/gb.qt/listbox+en">ListBox</a>
but allows to associate a tag with each list item.
<p>
Note that you can't use <tt><b>INHERITS</b></tt> in a form class file, because forms already
inherits the <a href="../comp/gb.qt/form+en">Form</a> class.
<p>
<h2>Virtual dispatching</h2>
<p>
When calling a method or accessing a property from an <a href="../def/object+en">object</a> reference, <a href="../def/gambas+en">Gambas</a> always
use <i>virtual dispatching</i>. It means that the real class of the <a href="../def/object+en">object</a> is always used, and
not the type of the variable that references the <a href="../def/object+en">object</a> - As it was in <a href="../def/gambas+en">Gambas</a> 1.0.
<p>
<h2>Inheritance and constructor</h2>
<p>
Contrary to all the <a href="../def/object+en">object</a> language I know, each class in the inheritance
hierarchy consumes the parameters passed to the constructor.
<p>
Let's suppose we have the following inheritance tree:
<p>
<pre class="code">MyListBox ---inherits--> ListBox ---inherits---> Control
</pre><p><p>
<ul>
<li><a href="../comp/gb.gtk/control+en">Control</a>._new() does not exist.
<li><a href="../comp/gb.qt/listbox+en">ListBox</a>._new() takes one parameter: the parent control.
<li>MyListBox._new() takes one parameter: a name - It is just an example.
<p>
</ul>

So <tt><a href="new+en">NEW</a> MyListBox</tt> will take two parameters.
<p>
<ul>
<li>The first will be sent to MyListBox._new().
<li>The second to <a href="../comp/gb.qt/listbox+en">ListBox</a>._new().
<p>
</ul>

But the <a href="../comp/gb.qt/listbox+en">ListBox</a>._new() will be called first. This way, you are sure that the <a href="../comp/gb.qt/listbox+en">ListBox</a> control exists
when you are in MyListBox._new().
<p>
You will create a MyListBox control this way:
<p>
<pre class="code">hMyListBox = NEW MyListBox("Name", hContainer)
</pre><p><p>
<hr><b>See also</b><br>
<a href="../cat/object+en">Object &amp; Class Management</a>&nbsp;

</div>
</body>
</html>

