How many times have you been working with an object or ASP.NET control and wanted it to behave a little differently
or do something that it couldn't? What if you could make a new version that automatically got
all the features of the old version and only changed or added what you wanted? You can... and it's all
thanks to the fact that .NET is object oriented.
Like last time, the goal of this lesson is not to try and explain object oriented programming (OOP).
There are entire books and classes dedicated to that. Nor is it to try and convince you that OOP is
any better or worse then procedural programming. That's a debate I'm simply not going to get involved in.
We're simply trying to teach you enough to be dangerous. ;) All kidding aside, if
you're looking to learn more about OOP, I'm sure there are far better resources and authors who are much more
adept at explaining it elsewhere. The goal of this lesson is simply to introduce you to inheritance and
the benefits it can offer you as an ASP.NET developer.
Inheritance
Everything in the .NET world is an object. While this is relatively obvious in some cases (like with controls)
it's not so obvious in others. Even simple types like integers and strings are implemented as objects in .NET.
This means that everything in .NET shares a common base. More complex objects are built based on simpler existing
ones. The coolest part of all this is that you too can create new objects based upon existing ones and these new
objects will automatically get all the functionality of the object they're built upon. This feature is called
inheritance. In the same way you inherit your good looks and winning personality from your parents,
your new object inherits its default properties and methods from it's parent object.
Continuing Our Dog Example...
Just to keep things simple, I'm going to illustrate inheritance using the same example we used in our
Creating and Using Objects lesson. If you haven't yet read it, I suggest
you do so now.
In our objects lesson, we built a class that enabled us to create and manipulate the properties of an object
that represented a dog. In the real world I can't think of too many applications for that except maybe for
a dog shelter or veterinarian. The analogy gets sort of weak from here on in, but bear with me.
Now let's assume that we're dealing with a very specialized vet that only deals with one type of dog...
I'm going to use Dalmations (mainly because they're cute!).
Now, since we know we're going to be dealing with Dalmations a lot, wouldn't it be nice if we didn't have to
specify that the dog's breed is Dalmation every time we created a dog object... and maybe Dalmations sound a little
different then your average dog... maybe it's more of an 'Arf Arf!' as opposed to the more generic 'Woof Woof!'.
Okay... I give... enough of this... let's see some code.
Public Class Dalmation : Inherits Dog
Public Dim NumberOfSpots As Integer
Public Sub New(Name As String, BirthDate As DateTime, Weight As Integer)
MyBase.New(Name, BirthDate, Weight, "Dalmation")
End Sub
Overrides Public Function Speak()
Speak = "Arf Arf!"
End Function
End Class
There are four main things I want you to notice in the class listing above:
First of all, notice the Inherits Dog declaration after the class name. This tells us that this
new object is based on our dog object. As a result it gets all the attributes of a dog automatically.
A good way to think of this relationship is to use the "is a kind of" test. A Dalmation is a kind
of dog. If you're trying to stretch things too far, this test will fail. After all, it
doesn't make sense to try and build a cat based on our model for a dog. A tabby is not a kind of dog. You'd
want to go farther up to a more generic (hypothetical) animal object if you wanted to start building a cat.
I've hard coded the breed type. Since we're dealing with a Dalmation object, there's no question as to the
breed of dog involved and so there's no need for us to be setting it every time we create a dog.
I've added a new attribute. Since Labradors and Poodles rarely have spots, it doesn't make sense to build
an attribute to count them into our base dog object. Dalmations, on the other hand, do have spots, so I've added a
way to keep track of them.
Finally, I've overridden the default Speak method and replaced it with a version modified to more accurately
reflect, the way our dog sounds. (Please note that I've never owned a Dalmation so while I know they have
spots... I've got no clue if they go 'Woof Woof!', 'Arf Arf!', or make any noise at all.)
Enabling Reuse Via a Namespace
Just like last time, one of the main reasons to create objects at all is so we can reuse them.
If we've got to include the code that tells us how to create them every time, then what good is it?
So just like in our object lesson, we'll create a library (.dll file) and add it to our application's
/bin directory. Once our object is there, we have access to it from any page in our ASP.NET application.
The command is a little more complex then last time because we need to reference
the existing Dog object that our new Dalmation object is based upon. The
compiler can't build a Dalmation which is based on a Dog unless it knows about
a Dog. So in order to compile our new Dalmation sucessfully, we've got to tell it
where it can find out about our Dog. For VB the command will look something like this:
That assumes you're at a command line prompt in the directory containing the source file
and that the VB compiler is in your path. If not you'll need to adjust your commands appropriately.
After compiling, all you need to do is move the resulting inheritance_namespace.dll file to your
application's /bin directory where it will automatically be picked up and available to be included
by your .aspx pages.
Once the libary is in place, your code can be as simple as this:
<%@ Page Language="VB" %>
<%@ Import Namespace="Dogs" %>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim myDog As Dalmation
myDog = New Dalmation("Spot", New Date(2000, 11, 12), 65)
...
End Sub
</script>
Get The Code
You can download the code that accompanies this lesson from here: inheritance.zip (8.3 KB).
It includes the code listed above as well as all supporting files. For the libraries, I've included
the source and compiled versions in case you have any trouble compiling it.
A quick note... once again my naming conventions for these files are less then stellar.
I'm just trying to keep my files from each lesson grouped together. In the real world
files should be named for what they are. Sorry about that...