Creating a Base Class

Virtually any class you create can act as a base class from which other classes can be derived. In fact, unless you specifically indicate in the code that your class cannot be a base class, you can derive from it (you will come back to this later).

Create a new Windows Forms Application project in Visual Basic by selecting File O New Project and selecting Windows Forms Application. Then add a class to the project using the Project O Add Class menu option and name it Person.vb. Begin with the following code:

Public Class Person End Class

At this point, you technically have a base class, as it is possible to inherit from this class even though it doesn't do or contain anything. You can now add methods, properties, and events to this class as you normally would. All of those interface elements would be inherited by any class you might create based on Person. For instance, add the following code:

Public Class Person

Public Property Name() As String Public Property BirthDate() As Date

End Class

Code snippet from Person

This provides a simple method that can be used to illustrate how basic inheritance works. This class can be represented by the class diagram in Visual Studio, as shown in Figure 3-2.

Person

Class

In this representation of the class as it is presented from Visual Studio, the overall box represents the Person class. In the top section of this box is the name of the class and a specification that it is a class. The section below it contains a list of the instance variables, or fields, of the class, with their scope marked as Private (note the lock icon). The bottom section lists the properties exposed by the class, both marked as Public. If the class had methods or events, then they would be displayed in their own sections in the diagram.

PI Fields mBirthDate gj? mName

E=jJ BirthDate g|jJ Name

FIGURE 3-2

Download from Wow! eBook <www.wowebook.com>

0 0

Post a comment