Visual Basic has had powerful object-oriented capabilities since the introduction of version 4.0 VB.NET carries that tradition forward. VB.NET simplifies some of the syntax and greatly enhances these capabilities, and now supports the four major defining concepts required for a language to be full object-oriented:
Abstraction – VB has supported abstraction since VB4. Abstraction is merely the ability of a language to create "black box" code – to take a concept and create an abstract representation of that concept within a program. A Customer object, for instance, is an abstract representation of a real – world customer. A Recordset object is an abstract representation of a set of data.
Encapsulation – This has also been with us since version 4.0. It’s the concept of a separation between interface and implementation. The idea is that we can create an interface (Public methods in a class) and, as long as that interface remains consistent, the application can interact with our objects. This remains true even if we entirely rewrite the code within a given method – thus the interface is independent of the implementation.
Encapsulation allows us to hide the internal implementation details of a class. For example, the algorithm we use to compute Pi might be proprietary. We can expose a simple API to the end user, but we hide all of the logic used by our algorithm by encapsulating it within our class.
Polymorphism – Likewise, polymorphism was introduced with VB4. Polymorphism is reflected in the ability to write one routine that can operate on objects from more than one class – treating different objects from different classes in exactly the same way. For instance, if both Customer and Vendor objects have a Name property, and we can write a routine that calls the Name property regardless of whether we’re using a Customer or Vendor object, then we have polymorphism.
VB, in fact, supports polymorphism in two ways – through late binding (much like Smalltalk, a classic example of a true object-oriented language) and through the implementation of multiple interfaces. This flexibility is very powerful and is preserved within VB.NET.
Inheritance – VB.NET is the first version of VB that supports inheritance. Inheritance is the idea that a class can gain the pre-existing interface and behaviors of an existing class. This is done by inheriting these behaviors from the existing class through a process known as sub classing. With the introduction of full inheritance, VB is now a fully object-orientated language by any reasonable definition.
Additionally, because VB.NET is a component-based language, we have some other capabilities that are closely related to traditional concepts of object-orientation:
Multiple interfaces – Each class in VB.NET defines a primary interface (also called the default or native interface) through its Public methods, properties and events. Classes can also implement other, secondary interfaces in addition to this primary interface. An object based on this class then has multiple interface, and a client application can choose by which interface it will interact with the object.
Assembly (component) level scooping – Not only can we define our classed and methods to be Public (available to anyone), Protected (available through inheritance) and Private (only available local), but we can also define them as Friend – meaning they are only available within the current assembly or component. This is not a traditional object-oriented concept, but is very powerful when designing component based applications.
Topics under this section:
- Objects, Classes, and Instances
- Creating Classes
- The Class Keyword
- Member Variables
- Methods
- Methods that Return Values
- Indicating Method Scope
- Method Parameters
- Properties
- Read-only properties
- Write-only properties
- The Default property
- Constructor methods
- Parameterized Constructors
- Constructors with Optional Parameters
- Combining Overloading and Optional Parameters
- Overloading the Constructor Method
- Shared Methods, Variables, and Events
