VB.NET: Object-Oriented Programming Concepts
- Abstraction in Visual Basic.NET
- Encapsulation in VB.NET
- Polymorphism in VB.NET
- Inheritance in VB.NET
- Interface-Based Programming
- Summary
In its lifetime, VB has varied from an object-based development tool to an object-oriented development tool, but previous versions of Visual Basic had still been missing a few key features.
Using previous versions of VB, programmers have been able to create classes and use those classes in building applications. But VB was missing key object-oriented development features. This hasn't stopped developers from building robust component-based systems, but it has hampered them in building extensible frameworks.
This isn't the case with Visual Basic.NET. In fact, object-oriented programming is required in the .Net Framework, and VB has become a full-featured, object-oriented development language.
New to the language is support for inheritance, new methods of exposing objects, and support for attribute-based programming. Visual Basic.NET also has improved support for interfaces, properties, and events.
Object-oriented programming is very important when building complex applications. When using past versions of VB, many choose not to explore the use of object-oriented programming techniques. This is a mistake that cannot be repeated in VB.NET. To effectively use VB.NET, a developer must understand and apply object-oriented concepts.
Object-oriented development has four key concepts in relation to programming: abstraction, encapsulation, polymorphism, and inheritance. We'll discuss each of these in this chapter.
Abstraction in Visual Basic.NET
Abstraction is the ability to generalize an object as a data type that has a specific set of characteristics and is able to perform a set of actions. For example, you can create an abstraction of a dog with characteristics, such as color, height, and weight, and actions such as run and bite. The characteristics are called properties, and the actions are called methods.
Object-oriented languages provide abstraction via classes. Classes define the properties and methods of an object type, but it is important to remember that a developer cannot use a class directly; instead, an object must be created from a classit must be instantiated.
VB has supported classes since version 4.0. Up until VB.NET, each class was implemented as a separate file that abstracted an object.
Listing 3.1 shows an instance of a VB6 class, and shows a Visual Basic.NET class. VB.NET changes the way you define your classes. First, in VB 6 a class was defined as a file with a .CLS extension: in VB.NET this is done in code. In the example in Listing 3.2, notice the Public Class Car and the End Class statements.
Listing 3.1 VB6 Class
Public MaximumSpeed as Integer Public ModelName as String Public Sub Accelerate() 'Some code to make the car go End Sub Public Sub Stop() 'Some code to make the car stop End Sub
Listing 3.2 VB.NET Class
Public Class Car Public MaximumSpeed as Integer Public ModelName as String Public Sub Accelerate() 'Some code to make the car go End Sub Public Sub Stop() 'Some code to make the car stop End Sub End Class
These listings show an abstraction of a very simple car. To use this abstraction, an instance of the class must be created. There were multiple ways to create an instance of a class in VB 6. Things have changed in VB.NET. Using New is now the preferred method of creating an object. Listing 3.3 shows three methods for creating an instance in VB 6 and Listing 3.4 shows four methods for doing so in VB.NET.
Listing 3.3 Three Methods to Create a Class Instance in VB 6
Dim oCar as New Car Set oCar = New Car Set oCar = CreateObject("Vehicles.Car")
You will notice that in Listing 3.4 I didn't include CreateObject. CreateObject has been denigrated to only create classic COM components and won't create an instance of a .NET component. The replacement for CreateObject is the System.Activator. CreateInstance method. (The Assembly.CreateInstance method maps to the System.Activator.CreateInstance method.)
Listing 3.4 Four Methods to Create a Class Instance in VB.NET
Dim oCar as New Car oCar = New Car oCar = Assembly.CreateInstance("Car") oCar = System.Activator.CreateInstance("Car","URIToAssembly")