VB.NET's Object Oriented Features
One of the biggest .NET concepts that existing corporate VB developers and those moving from ASP need to get a handle on is the thoroughly object-oriented nature of VB.NET. Despite the addition of class modules in VB 4.0 and the enhanced ability to create COM components in VB 5.0 and 6.0, VB remained primarily a procedural language, and many developers continue to use it as such. This is not to say that you couldn't use some object-oriented techniques with VB 6.0 (see Chapter 15 of my book Pure Visual Basic for more information). However, what OO features you could use were derived from COM and were therefore limited, and the VB language itself did not contain the keywords necessary to enable a truly OO implementation.
In VB.NET, this changes with the inclusion of keywords that directly support OO features implemented by the common language runtime. VB.NET derives these features from the CTS that, as discussed in Chapter 1, "The Microsoft .NET Architecture," uses System.Object as the ancestor or root of all types in VS.NET. As a result, VB cannot help but be object-oriented to the core, and because the CLR uses the concept of classes to expose functionality, VB.NET developers, unlike in previous versions, cannot avoid the use of classes. Finally, the architecture of the Services Framework mandates that developers use inheritance and polymorphism (which I will discuss later in the chapter) to take advantage of its services effectively. In other words, to take advantage of the power of the CTS to be productive and to use system services provided by the Services Framework, a good understanding of OO is required.
The addition of OO features in VB.NET is both a good and bad phenomenon. First, it is positive because the language is more powerful and as a result you can express more sophisticated designs in your solutions. And for developers like me who have always appreciated VB, this increased power also gives it a more professional air and serves to extend the population of developers who use it. However, this increase in power also comes with responsibility. Designing solutions that use inheritance, polymorphism, and encapsulation requires more discipline and knowledge on the part of the developer. For that reason, developers with formal computer science backgrounds or those willing to learn these concepts will probably feel the most comfortable with the changes. Not to worry, though, VB.NET extends the language in many ways that will feel quite natural so that everyone can exploit these features.
In this chapter, I'll discuss the features of VB.NET that allow it to be a fully OO language.
Before moving to the language syntax, let's formally define the key OO concepts and terms that will be used in this chapter beginning with encapsulation, polymorphism, and inheritance.
Encapsulation means that an object can hide its internal data structures from consumers of the object. Therefore, all of the object's internal data is manipulated through members (methods, properties, events, fields) of the object, rather than through direct references.
The primary benefits of encapsulation are maintainability and reusability. Code that takes advantage of encapsulation is more maintainable because consumers of the code work with the object through its public members. With a fully encapsulated object, for example, code outside the object cannot directly change a variable declared inside the object. By shutting off this direct access, fewer bugs are introduced because consumers of the object cannot inadvertently change the state of an object at run-time.
Abstracting the internal data of the object from consumers also leads to greater reusability. This follows because encapsulation leads to fewer dependencies between the consumer and the class and fewer dependencies is a prerequisite for creating reusable software.
The second characteristic of OO systems is polymorphism. This concept is defined as the ability to write code that treats objects as if they were the same when in fact they are different. In other words, polymorphism allows you to write code that is generic across a set of objects that provide the same public members. Underneath the covers, each object might be implemented differently. However, as far as the consumer is concerned, each object looks the same and can be treated as such. In VB.NET, polymorphism can be created using both classes and interfaces.
The benefits of polymorphism revolve around the central fact that consumers of objects do not have to be aware of how the object performs its work, only that it does so through a specific set of members. This makes writing code that uses objects simpler by allowing the code to treat the object as if it were a black box, which leads to increased maintainability. Along the same lines, polymorphism allows you to write less code because each individual object does not have to be dealt with separately. Finally, polymorphism lends itself to writing code that can be reused because it will not be specific to a particular object.
The final OO concept is inheritance. Inheritance allows objects to share their interfaces (the definition of their members) and/or implementation in a hierarchy. For example, Tyrannosaurus and Velociraptor objects might be derived or inherited from a more generic Theropod object. All three objects share a basic set of members and, possibly, behaviors, such as carnivorousness, although the descendant objects might also include additional members or override members of Theropod. Inheritance allows objects to become more specific further down the hierarchy by adding additional members. In a nutshell, inheritance allows objects to reuse features (either their definition or their code) of other objects to which they are naturally related. The primary benefit of inheritance is, thus, reuse.
Obviously, inheritance and polymorphism are closely related, and, in fact, inheritance is what makes polymorphism possible in OO designs. It is always the case that objects that are in an inheritance relationship can be treated polymorphically. For example, if the Velociraptor object is inherited from the Theropod object, any consumer that is designed to work with Theropod objects will also work with Velociraptor objects.
VB.NET developers can benefit from inheritance in two ways: through interface inheritance and implementation inheritance. Interface inheritance allows only the definition of the object to be reused, whereas implementation inheritance allows the actual code written for the ancestor object (and its ancestors all the way down the line) to be reused. Which one to use is a design decision as mentioned in Chapter 1 and discussed more fully in this chapter.
If developers wanted to use a form of implementation inheritance in previous versions of VB, they had to design classes to take advantage of the concepts of containment and delegation. Basically, these concepts mean that a class accessible by a consumer will contain a private instance of a second class and delegate its services to the consumer through its own interface. Containment and delegation are familiar terms (along with aggregation) to COM programmers.
More OO Terms
Familiarity with the following OO terms will be useful in this discussion.
ClassThe fundamental unit of code reuse. Implemented with the Class statement in VB.NET. Classes can be inherited from and nested, and can contain members.
Base classA class that serves as the ancestor for an inherited class, although this usually means the direct ancestor. VB.NET uses the MyBase keyword to refer to the direct ancestor. As a group, the ancestors are referred to as the base classes. The base class at the top of the hierarchy is referred to as the concrete base class.
Abstract base classA base class that cannot be instantiated at run-time and serves only as the template for derived classes. In VB.NET, this is accomplished using the MustInherit keyword.
InterfaceA well-defined collection of members along with their signatures. Interfaces provide no implementation. They are created in VB.NET with the Interface keyword.
MembersThe methods, properties, events, and fields that make up an interface or class definition.
MethodsNamed blocks of code within a class that can accept arguments and might return a value. VB.NET supports both Sub methods that do not return a value and Function methods that do.
PropertiesData exposed by a class and manipulated through code that runs when the property is set to a value or retrieved. In VB.NET, property creation is unified in a single Property statement.
FieldsData exposed by a class directly to the consumer. A field represents a data location and therefore, its access is not abstracted. Fields are created in VB.NET using public variables within a class.
EventsNotifications fired by a class and captured by a consumer. In VB.NET, the Event and RaiseEvent statements are used to declare and raise events in addition to the use of delegates.
OverloadingThere are two forms of overloading. The first is that a class can contain multiple definitions of the same method, each with different arguments (signatures). The consumer can then choose which method to call. The second is that derived classes can overload methods in a base class to alter the signature of the method. VB.NET uses the concepts of optional parameters, parameter arrays (paramarrays), and the Overloads keyword to implement both forms.
OverridingOverriding a method means that a derived class can implement its own functionality for a method or property. VB.NET uses the Overrides, Overridable, NotOverridable, and MustOverride keywords to specify how methods and properties might or might not be overridden.
Static memberA member that is exposed by the base class, is not allowed to be overridden in a descendant, is available to all instances of the class, and is available even before the instance is created. This is also referred to as a shared member and is implemented with the Shared keyword.
Virtual memberThe opposite of a static member, also referred to as an instance member. This refers to a method that can be overridden in a descendant class. By default, all methods and properties in VB.NET can be made virtual by including the Overridable keyword.