Inheritance

In VB.NET, the specialization relationship is implemented using inheritance. This is not the only way to implement specialization, but it is the most common and most natural way to implement this relationship.

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 VB.NET, you create a derived class by adding the Inherits keyword after the name of the derived class, followed by the name of the base class:

Public Class ListBox
   Inherits Window

Or you might combine these two lines onto one as follows:

Public Class ListBox : Inherits Window

This code declares a new class, ListBox, that derives from Window. You can read the Inherits keyword as “derives from.”

The derived class inherits all the members of the base class, both member variables and methods. These members can be treated just as if they were created in the derived class, as shown in Example 6-1.

Example 6-1. Deriving a new class

Option Strict On Imports System Public Class Window ' constructor takes two integers to ' fix location on the console Public Sub New(ByVal top As Integer, ByVal left As Integer) Me.top = top Me.left = left End Sub 'New ' simulates drawing the window Public Sub DrawWindow( ) Console.WriteLine("Drawing Window at {0}, ...

Get Programming Visual Basic .NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.