18.15. 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.

18.15.1. 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.

18.15.2. 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 'New 

Because 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.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net