View Single Post
 
Old 12-14-2006, 01:03 AM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,747
Default Classes vs. Objects

I know I said this was object-oriented programming. So why am I talking about classes? The answer to this question is that all objects are classes. (Mind-blowing, huh?) The way to separate the difference between objects and classes is quite easy. An object has an IS-A relationship, while a class has a HAS-A relationship.

A car IS-A vehicle, while a dog HAS-A tail. The car would be an object declared as type Vehicle, while the dog would have a member that IS-A tail. However, Fido IS-A dog. The dog class is the base template for all dog objects, so when we declare Fido as a dog, Fido is an object. Since Fido IS-A dog, Fido HAS-A tail.

IS-A = object
HAS-A = class

"Class" and "object" are sometimes used interchangeably. However, classes describe the structure of objects, while objects are instances of classes. Each instance is an exact copy of its base class. Because an object is an "instance" of a class, the act of creating an object is called instantiation.
To describe it better, a class is a blueprint, and an object is a building based on that blueprint.


To understand it, we can use an example. Open up Visual Studio, and create a new Windows Application. Go to the File menu, click Add New Item, and select "Class". A blank class window should show up. Change the name from Class1 to Vehicle. This should be what comes up:
Code:
Public Class Vehicle 'This is an empty class End Class
We will start by creating a constructor. A constructor is a special method inside each class that tells the class what to do when it is instanced. The instantiation of a class occurs when a new instance of a class is declared. In VB.NET, this is accomplished with the New sub.

Change your class so it looks like this:
Code:
Public Class Vehicle #Region " Constructors " 'The default constructor Public Sub New() 'Tell the user that the new sub is being called. MessageBox.Show("This is a new vehicle object", "Object Oriented Programming") End Sub #End Region End Class
Notice two things. The first is the Region called "Constructors". I usually create these different regions to keep the code window nice and neat, and as you can have multiple constructors (see below), it's nice to keep them all in one place. The second is the code in the New sub. This should fire every time a new object is declared.

To test this out, we can add some code to the MainForm_Load() method on our form:
Code:
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Declare a vehicle called Car and instance it Dim Car As Vehicle Car = New Vehicle() ' 'Declare a vehicle called Truck and instance it Dim Truck As Vehicle Truck = New Vehicle() End Sub
Run it. You'll notice that you get two identical MessageBoxes that tell you that you have created a new vehicle object. That's nice, right? Wouldn't it be better to at least let it tell you what type of vehicle it is? We can: just overload a new constructor.

Change the class to:
Code:
Public Class Vehicle Protected _VehicleType As String #Region " Constructors " 'The default constructor Public Sub New() 'There was no parameter passed, so let's say the vehicle is a "vehicle" _VehicleType = "vehicle" ' 'Alert the user MessageUser() End Sub ' 'Another constructor that accepts a parameter Public Sub New(ByVal VehicleType As String) 'We've been passed a parameter, so let's store that in a private variable 'for later use _VehicleType = VehicleType ' 'Alert the user MessageUser() End Sub #End Region Private Sub MessageUser() 'Tell the user what type of class it is MessageBox.Show("This object is a " & _VehicleType, "Object Oriented Programming") End Sub End Class
That's a lot! What does it mean? The first line "Protected _VehicleType As String" means that this is a variable that can be used anywhere inside the class, or any subclass (we'll get to this topic later). The default constructor has been modified to take into account the storage variable, and if there's no parameter passed, it sets the variable to "vehicle".

What about that second constructor? It's known as an overloaded constructor because it is a New sub, but has a parameter attached to it. We take the passed value and assign it to our protected variable. Let's see what this new class does. Modify MainForm_Load to be the following:
Code:
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Declare a vehicle called Car and instance it Dim Car As Vehicle Car = New Vehicle("car") ' 'Declare a vehicle called Truck and instance it Dim Truck As Vehicle Truck = New Vehicle("truck") ' 'Declare a vehicle called Van and instance it Dim Van As Vehicle Van = New Vehicle() End Sub
Notice how the MessageBox has been changed to reflect this addition. Car and Truck are shown to be their respective types, but because nothing was passed to the Van instantiation call, it defaults to "vehicle".
Attached Files
File Type: zip ClassesVsObjects.zip (3.7 KB, 33 views)
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote