The Basics of Object-Oriented Programming (VB.NET)
by Jacob Croft, MCT
Just about everything in the .NET Framework derives from the System.Object root class. So, almost everything you do in Visual Basic.NET is associated with objects. If you are new to object-oriented programming, the following terms and concepts will help you get started.
The words "class" and "object" are used so much in object-oriented programming that it is easy to get the terms mixed up. Generally speaking, a CLASS is an abstract representation of something, whereas an OBJECT is a usable example for the thing the class represents. I like to make an analogy that a "class" is the blueprint for a house, and the "object" is the house itself. The one exception to this rule is shared class members, which are usable in both instances of a class and object variables declared as the type of the class.
Classes are made of fields, properties, methods, and events. Fields and properties represent information that an object contains. Fields are like variables in that they can be read or set directly. For example, if you have an object named House, you could store its color in a field named Color.
Properties are retrieved and set like fields, but are implemented using Property Get and Property Set procedures, which provide more control on how values are set or returned. The layer of indirection between the value being stored and the procedures that use this value helps isolate your data and allows you to validate values before they are assigned or retrieved.
Methods represent actions that an object can perform. For example, a House object could have OpenGarage, RingDoorbell, and SetAlarm methods. You define methods by adding procedures, either Sub routines or functions, to your class.
Events are notifications an object receives from, or transmits to, other objects or applications. Events allow objects to perform actions whenever a specific occurrence takes place. An example of an event for the House class would be a Smoke_Alarm event. Because Microsoft Windows is an event-driven operating system, events can come from other objects, applications, or user input such as mouse clicks or key presses.
Fields, properties, methods, and events are only one half of the object-oriented programming equation. True object-oriented programming requires objects to support three qualities: encapsulation, inheritance, and polymorphism.
Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object. Objects can control how properties are changed and methods are executed. For example, an object can validate values before allowing property changes. Encapsulation also makes it easier to change your implementation at a later date by letting you hide implementation details of your objects, a practice called data hiding.
Inheritance describes the ability to create new classes based on an existing class. The new class inherits all the properties and methods and events of the base class, and can be customized with additional properties and methods. For example, you can create a new class named Apartment based on the House class. The Apartment class inherits the Color property from the House class and can have additional properties such as NumberOfTenants.
Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways. Polymorphism is essential to object-oriented programming because it allows you to use items with the same names, no matter what type of object is in use at the moment. For example, given a base class of House, polymorphism enables the programmer to define different OpenGarage methods for any number of derived classes. The OpenGarage method of a derived class name BillGatesHouse may be completely different from the method with the same name in the base class. Other procedures or methods can use the OpenGarage method of the derived classes in exactly the same way, no matter what type of House object is being used at the time.
Overloading, overriding, and shadowing are similar concepts that can be easy to confuse. Although all three techniques allow you to create members with the same name, there are some important differences.
Overloaded members are used to provide different versions of a property or method that have the same name, but that accept a different number of parameters, or parameters with different data types.
Overridden properties and methods are used to replace an inherited property or method that is not appropriate in a derived class. Overridden members must accept the same data type and number of arguments. Derived classes inherit overridden members.
Shadowed members are used to locally replace a member that has a broader scope. Any type can shadow any other type. For example you can declare a property that shadows an inherited method with the same name. Shadowed members cannot be inherited.