Using Object-Orientation in ASP.NET: InheritanceBy Ian Stallings
|For More Information...|
|To learn more about ASP.NET be sure to check out the ASP.NET Article Index. To learn more about object-oriented programming check out Ian's first two articles:|
Well I've come out from under my stone called Undernet to finish this article. People weren't clamoring for it, but nonetheless I think it will prove useful to people trying to grasp just why .Net is better than the existing environment. It's not necessarily about using XML or the CLR so much as it's about code reuse, ease of use, and shortening time of development. Understanding Object Oriented Programming and being able to apply it directly will help you utilize .Net to the fullest of it's capabilities.
In the previous articles (Using Object-Orientation in ASP.NET : Overview and Using Object-Orientation in ASP.NET : Encapsulation) I've discussed an overview of what OOP is and what encapsulation offers. If you haven't read those, go check them out to get a better sense of what I'm talking about. In this article I will discuss Inheritance using C# examples. I've decided to use C# in the web examples because IMHO it offers a much cleaner syntax than VB, and it's very similar to Java, which I already use. I was going to add VB examples of inheritance to the download, but after starting them I realized that VB simply does not lend itself to OOP. If I'm gonna go forward with .Net talking about OO, it's gonna be in C#. (To learn more about C#, Microsoft's new programming language introduced with ASP.NET, be sure to check out the C# Article Index!)
What is Inheritance?
Inheritance is a relationship between classes where one class is the parent class of another. Sometimes people refer to the parent class as a base class, superclass, ancestor, etc. When a subclass inherits from a base class it's a "is-a-kind-of" relationship. For example, suppose we have a superclass named
Cat with a subclass named
This type of relationship
allows the subclass to inherit all of the superclass' attributes and extend upon them or
add others. Our
Cat class may have two simple actions (methods) it can perform:
Lion subclass inherits these
actions and it can then extend, change, or add additional actions such as
Ok, So How Do We Use This?
For this example I decided to create a simple
Profile class to store a user's
Profile class has three properties (fields):
_lastName. We set values for these fields using set
and get Methods.
Now we are able to store a user's first name, last name, and phone number. But suppose we
come back to this application in another software design iteration and want to extend the
user's profile by also storing their address, city, state, postal, and maybe a description.
So we create a subclass using inheritance and add these additional properties and also
set/get methods for each property. For this example I have created a class named
You can see in the above example how we use the colon (
:) (on the first line) to denote that
ExtendedProfile is a subclass of the
Profile superclass. We
then add the properties to hold the additional fields: address, state, city, description, etc.
We then added methods for setting and getting the additional properties.
We then compile this
Profile.cs file, in the command line C# compiler, using
the following line:
You will need to have the .NET Framework SDK installed on your machine to have the C# compiler on your computer. You can download the .NET Framework SDK Beta at: www.ASP.NET.
In this case I have the source files stored in the
C:\Inetpub\wwwroot\asp.net\inheritance directory, a web directory, and the
output dll is generated one directory up in my
Now that we've looked at what inheritance is and how to use it to extend the functionality of a class, let's see how we can use these classes in an ASP.NET Web page. Part 2 examines this very topic!