Overloads and Overrides Keywords Sample

This sample demonstrates how to extend derived classes with Visual Basic language features like overloading and overriding. It also shows how to allow various levels of access to the members of a class, including Public, Private, and Protected.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run this sample

  • Press F5.

Demonstrates

This application simulates a vehicle registration system, supporting automobiles, boats, and bicycles.

The application utilizes a base class called Vehicle, from which the classes Automobile, Boat, and Bicycle are derived. Each derived class extends the base class in some way: by overriding methods of the base class, by implementing new methods or properties of its own, or by replacing (shadowing) members of the base class. There is also a Friend class called DepartmentOfMotorVehicles that simulates reading data from and writing data to a database.

The application demonstrates the use of these statements and modifiers in classes and their members:

Keyword

Use

Inherits Statement

Used on Automobile, Boat, and Bicycle to indicate that Vehicle is the base class.

NotInheritable

Used on the DepartmentOfMotorVehicles class. This class may not serve as a base class.

MustInherit

Used on the Vehicle class. Only classes that derive from Vehicle can be instantiated.

Overloads

The Register method in the Boat class is overloaded to add a maximum number of passengers.

Overridable

Used in the Vehicle class on the ID property. While implemented in the base class, derived classes may implement another version.

Overrides

Used in all three derived classes on the CurrentValue property, to replace the default implementation in the base class.

MustOverride

Used in Vehicle class members (ComputeRegistrationFee and Salary). While these members are not implemented in Vehicle, they must be implemented in derived classes that are not marked MustInherit.

Shadows

Used in the Bicycle class on the Register method.

Public (Visual Basic)

Used on classes and their members to indicate that a class or member is available to the client application.

Protected (Visual Basic)

Used on fields in the Vehicle class. These fields are accessible in the Vehicle class and derived classes, but are not accessible by client applications.

Friend (Visual Basic)

Used on the DepartmentOfMotorVehicles class. This class is used in the assembly, but is not accessible in client applications.

Private (Visual Basic)

Used on class fields. These fields are not accessible in client applications.

Shared (Visual Basic)

Used on methods in the DepartmentOfMotorVehicles class. This allows the DepartmentOfMotorVehicles class to act as a function library.

See Also

Other Resources

Object-Oriented Programming in Visual Basic