Classes
As you learned in the preceding section, an object is an entity with a well-defined boundary, state, behavior, and identity. Classes define an object's functionality, characteristics, and identity. You can think of a class as a pattern or template from which to build objects.
When you create an instance of a class, you create an object of that class type. The new object has all the functionality, data, and characteristics defined by the class.
A fundamental and powerful feature of classes is the capability to build a new class from another class. This process is known as deriving a class from another class. In Visual Basic .NET, a class is derived by inheriting a class with the Inherits keyword. You learned a little about inheriting in the preceding chapter with the SimpleMsg and SmartMsg classes, shown here again in Listing 1.
Listing 1SimpleMsg and SmartMsg Classes from the Task Project
Class SimpleMsg 'member variable to store the message text Private strMessage As String 'property to allow get/set functionality on 'strMessage member variable Public Property MsgText() As String ... End Class Class SmartMsg Inherits SimpleMsg Private FileName As String Public Sub New(ByVal FileName As String) ... Public Sub Dispose() ... Public Function Write() As Boolean ... Public Function Read() As Boolean ... End Class
With class derivation, you can build progressively more complex classes from a simple base. A good example in Windows applications is building a simple class that represents a basic window. Figure 1 illustrates how inheriting the window class builds a progressively more complex user interface window class.
Figure 1 Class inheritance example.
One advantage to building applications from simple classes and inheriting them to build more complex classes is that you can focus your development on a class's unique characteristics. For example, if you inherit a window class to make a button class, the button class doesn't have to deal with anything the window class already does. The button class can focus on what a button does and leave what a window does to the window class.