Abstract Methods and Classes

Each type of Window has a different shape and appearance. Drop-down list boxes look very different from Buttons. Clearly, every subclass of Window should implement its own DrawWindow( ) method—but so far, nothing in the Window class enforces that they must do so. To require subclasses to implement a method of their base, you need to designate that method as abstract.

You designate a method as abstract by placing the MustOverride keyword at the beginning of the method definition, as follows:

MustOverride Public Sub DrawWindow ( )

An MustOverride method has no implementation. It creates a method name and signature that must be implemented in all derived classes. Furthermore, making one or more methods of any class MustOverride has the side effect of making the class abstract; an abstract class must be marked with the keyword MustInherit .

Classes marked with MustInherit establish a base for derived classes, but it is not legal to instantiate an object of a class marked MustInherit. Once you declare a method with MustOverride, you prohibit the creation of any instances of that class.

If one or more methods of the class are MustOverride, the class definition must be marked MustInherit, as in the following:

MustInherit Public Class Window

Thus, if you were to designate DrawWindow( ) MustOverride in the Window class, the Window class would thus become MustInherit. Then you could derive from Window, but you could not create any Window objects/instances. ...

Get Programming Visual Basic .NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.