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