Inheritance
In Visual Basic 2005, the specialization relationship is implemented using inheritance
. Saying that ListBox inherits from (or derives from) Window indicates that it specializes Window. Window is referred to as the base class, and ListBox is referred to as the derived class. That is, ListBox derives its characteristics and behaviors from Window and then specializes to its own particular needs.
Implementing Inheritance
In Visual Basic 2005, you create a derived class by using the key word Inherits followed by the name of the base class:
Public Class ListBox Inherits Window
This code declares a new class, ListBox, that derives from Window. The derived class inherits all the members of the base class, both member variables and methods.
Calling Base Class Constructors
ListBox derives from Window and has its own constructor. The ListBox constructor invokes the constructor of its parent by calling MyBase.New and passing in the appropriate parameters:
Public Sub New( _
ByVal top As Integer, _
ByVal left As Integer, _
ByVal theContents As String)
MyBase.New(top, left) ' call base constructor
mListBoxContents = theContents
End Sub 'NewBecause classes cannot inherit constructors, a derived class must implement its own constructor and can only make use of the constructor of its base class by calling it explicitly.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access