Rob wrote:
In many articles related to VB.net the word "class" is used...
How many meanings are there to this word ?
"possible to derived a class from another"
"forms are full-fledged classes"
"base class"
A class is the definition of a type. Whenever you declare a class, you
introduce a new type in your program (or library, whatever).
Ex:
Class Ancestor
'...
End Class
After having declared this new type (Ancestor) I'm able to create
instances of that type:
Ex:
Dim X As New Ancestor
Now X references an instance of a class named Ancestor. The instance of
the class is called "object". The variable X is called a "reference".
So far so good. Of course, having new types means nothing if I can't
manipulate them. That's why class types may implement behaviour (by way
of Subs, Functions and Operators) and properties (by way of err...
Properties). Besides, they may have internal members just like
variables. So let's redesign our Ancestor class to give it a few
"members".
Ex:
Class Ancestor
Private mName As String
Public Sub DoThis: End Sub
Public Sub DoThat: End Sub
Public Property Name As String
Get: Return mName: End Get
Set(Value As String): mName = Value.ToUpper: End Set
End Property
End Class
Now, if I declare a variable of the Ancestor type, say, Dim X As New
Ancestor, I may manipulate the object through X.
Ex:
Dim X As New Ancestor
X.DoThis
X.DoThat
X.Name = "Francisco"
Notice that the member mName is private, and therefore can't be seen
"from the outside". This is called "Encapsulation" in OOP speak, and
means that classes may hide details of their implementation.
Well, contrary to other types you see in VB.Net (Integer, Double, etc),
class-based types can be extended. This means that I can declare
another class type that extends an existing one.
Ex:
Class Derived : Inherits Ancestor
Sub DoSomething: End Sub
End Class
I just created a new type, this time called Derived. I may declare
variables of type "Derived" from now on.
Ex:
Dim Y As New Derived
The fact that the Derived type inherits from the Ancestor type means
that besides being the "Derived" type, this new type is also an
"Ancestor". In other words, whenever I declare a variable of type
Derived, I'm implicitly saying that it's of type Ancestor also. This
means also that all functionality implemented in the Ancestor type is
still available in the Derived type, together with the functionality
declared in the Derived type itself.
Ex:
Dim Y As New Derived
Y.DoThis 'Inherited from Ancestor
Y.DoThat 'Inherited from Ancestor
Y.DoSomething 'specific of Derived
Y.Name = "Carmen" 'Inherited from Ancestor
This quality of classes is called Inheritance. It allows a great deal
of code reuse, because you create base types (ancestors) with a base
functionality and create derived types that extend or alter that base
functionality.
Nevertheless, the fact that a type inherits from another, means also
that the derived type may be taken as if it was it's ancestor!
Ex:
Dim Z As Ancestor
Z = New Derived 'Wow! what gives???
Z.DoThis
Z.DoThat
Z.Name = "Maria"
The previous example is perfectly legal: derived types may represent
ancestor types (but not the contrary!). Of course, since the variable Z
represents an Ancestor type, even though it is "loaded" with an
instance of a Derived type, you can only use it as if it was an
Ancestor...
Ex:
Dim Z As Ancestor = New Derived
Z.DoThis 'Ok, it's an Ancestor method
Z.DoSomething 'na-na-na... DoSomething doesn't exist in the Ancestor
type
This may seem like a show stoper, but actually there's more things
going on under the curtains. It turns out that a derived type may
change the behaviour of methods declared in the ancestor type. If, for
instance, our ancestor type was declared like this:
Ex:
Class Ancestor
'...
Overridable Sub PrintYourName
Console.Writeln(mName)
End Sub
End Class
Dim W As New Ancestor
W.Name = "Pedro"
W.PrintYourName ' will print "PEDRO"
No we could change the behavior of the PrintYourName method from a
derived class. More over, whenever a variable *of the ancestor type*
(but referencing an instance of the derived type) executed the
PrintYourName method, the executed code would come from the derived
type!
Ex:
Class Derived
'...
Overrides Sub PrintYourName
Console.WriteLn(Name & " derived!")
End Sub
End Class
Dim W As Ancestor = New Derived
W.Name = "Pedro"
W.PrintYourName ' will print "PEDRO derived!"
This characteristic of classes is called Polymorphism.
Although all this may seem far-fetched, notice that you're constantly
applying these concepts in your VB.Net programs. When you declare a
form (or, actually, the framework declares one for you) you'll notice
that it's a new type extending an existing one:
Ex:
Public Class Form1
Inherits System.Windows.Forms.Form
'...
End Class
And many methods of your form are actually overrides of methods
declared in the Form class. Because of this, the framework may treat
objects of your new type (Form1 in the example) as if they were of type
Form. That's how all the magic happens. ;-)
Hope this helped.
Regards,
Branco.