Inheritance in VB.NET

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 5.  Inheritance and Exceptions in VB.NET


VB.NET supports a single inheritance model. Thus a class may derive from a single base class, and not from more than one. (In fact, as we saw in the previous chapter, every class in VB.NET ultimately derives from the root class System.Object . [3] This single inheritance model is simple and avoids the complexities and ambiguities associated with multiple inheritance, as in C++. Although a VB.NET class can inherit only from a single base class , it may inherit from several interfaces , a topic we will discuss in the next chapter.

[3] In previous versions of VB, the Variant type represented the universal data type. In VB.NET, the Object class is the universal data type. The Variant type is no longer supported in VB.NET. Variant is still a reserved word, but it has no actual syntactical use in VB.NET.

In this section we discuss inheritance in connection with a further elaboration of our hotel reservation case study. In the following section we will cover additional features of inheritance in VB.NET, illustrated by an employee class hierarchy.

Inheritance Fundamentals

With inheritance, you factor the abstractions in your object model and put the more reusable abstractions in a high-level base class. You can add or change features in more specialized derived classes, which "inherit" the standard behavior from the base class. Inheritance facilitates code reuse, extensibility, and maintainability. A derived class can also provide a more appropriate interface to existing members of the base class.

Consider Reservable as a base class, with derived classes such as Hotel . All reservables share some characteristics, such as an ID, a capacity, and a cost. Different kinds of reservables differ in other respects. For example, a hotel has a City and a HotelName .

VB.NET Inheritance Syntax

You implement inheritance in VB.NET by specifying the derived class in the Class statement with the keyword Inherits followed by the base class. The file HotelBroker.vb in the CaseStudy folder illustrates deriving a new class Hotel from the class Reservable .

 ' HotelBroker.vb Imports System Namespace OI.NetVb.Acme  [4]   Public Class Hotel   Inherits Reservable  Public City As String       Public HotelName As String       Public Sub New(_        ByRef city As String, _        ByRef name As String, _        ByVal number As Integer, _        ByVal cost As Decimal)          MyBase.New(number, cost)          Me.City = city          HotelName = name       End Sub       Public ReadOnly Property HotelId() As Integer          Get             Return m_unitid          End Get       End Property       Public ReadOnly Property NumberRooms() As Integer          Get             Return Capacity          End Get       End Property       Public ReadOnly Property Rate() As Decimal          Get             Return Cost          End Get       End Property    End Class 

[4] We discuss creating a namespace with the Namespace directive later in the chapter.

The class Hotel automatically has all the members of Reservable , and in addition has the fields City and HotelName .

Changing the Interface to Existing Members

The base class Reservable has the Protected member m_unitid . Private and Protected members are intended for internal use and are not exposed as such to the outside world. A Protected member, while not accessible to the outside world, is accessible to derived classes. In the Hotel class, which inherits from Reservable , we provide the public property HotelId to give clients read-only access to the Protected member m_unitid . When we implement a property in this way, we can choose a name that is meaningful, such as HotelId , in place of a more abstract name, such as m_unitid , used in the base class. Also, the implementation details of such an encapsulated member may be changed without breaking existing code that accesses the public property.

Invoking Base Class Constructors

If your derived class has a constructor with parameters, you may wish to pass some of these parameters along to a base class constructor. In VB.NET you can conveniently invoke a base class constructor by calling MyBase.New , with the desired parameter list.

 Public Sub New(_  ByRef city As String, _  ByRef name As String, _  ByVal number As Integer, _  ByVal cost As Decimal)  MyBase.New(number, cost)  Me.City = city    HotelName = name End Sub 

Note that the syntax allows you to explicitly invoke a constructor only of an immediate base class. There is no notation that allows you to directly invoke a constructor higher up the inheritance hierarchy.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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