O'Reilly logo

Visual Basic 2005 Jumpstart by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Controlling How Classes Are Implemented

Although you can generally create custom versions of any of classes that you write or find in the .NET Class Libraries, there will be times when you'll want to control the outcome, especially when others will use your work. There are also times when you'll want to prove to .NET that your class has fulfilled the terms of a contract that promises a certain level of functionality and is therefore qualified to handle particular assignments.

Allowing or Preventing Overridable Methods

In the earlier part of this chapter, you saw how to use the Stack class in the System.Collections namespace and how you can extend its functionality by inheriting from it. You were also able to override and overload some of its methods to suit your own requirements.

In this section, you will learn how you can create classes from which others can inherit. You will also learn how to specially allow or prevent subclasses from changing your methods.

Using the StackClass defined in the last section, suppose you want others, including yourself, to be able to reuse the class and override its methods. In this case, you would do the following:

Public Class MyStackClass
	Inherits StackClass

End Class

To override the Push and Pop methods in the base class, you would use the Overrides keyword, as shown in Example 3-12.

Example 3-12. Overriding the Push and Pop methods of MyStackClass

Public Class MyStackClass 
	Inherits StackClass
	Public Overrides Sub Push(ByVal item As Object) … MyBase.Push(item) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required