This documentation is archived and is not being maintained.

Overloads and Overrides Keywords Sample 

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

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

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

Show: