When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Tuesday, February 20, 2001

Using Object-Orientation in ASP.NET: Inheritance

By 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 Lion. A Lion is-a-kind-of Cat.

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: eat() and sleep(). The Lion subclass inherits these actions and it can then extend, change, or add additional actions such as play() or roar().

Ok, So How Do We Use This?
For this example I decided to create a simple Profile class to store a user's information. This Profile class has three properties (fields): _phoneNumber, _firstName, _lastName. We set values for these fields using set and get Methods.

public class Profile 
	private String _firstName;
	private String _lastName;
	private String _phoneNumber;

	public Profile() 
		_phoneNumber = "(703)-549-7991";
		_firstName = "Shedao";
		_lastName = "Shai";

	public void setPhoneNumber(String phoneNumber)
		_phoneNumber = phoneNumber;
	public String getPhoneNumber() {
		return _phoneNumber;
	public void setFirstName(String firstName)
		_firstName = firstName;
	public String getFirstName() 
		return _firstName;

	public void setLastName(String lastName) 
		_lastName = lastName;
	public String getLastName() 
		return _lastName;

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 ExtendedProfile:

public class ExtendedProfile: Profile 
	private String _address1;
	private String _address2;
	private String _state;
	private String _city;
	private String _postal;
	private String _description;
	public ExtendedProfile() 
		_address1 = "3800 Park Avenue";
		_address2 = "Apt 1010";
		_city = "Alexandria";
		_state = "VA";
		_postal = "22302";
		_description = "Software Engineer";

	public void setAddress(String address1, String address2) 
		_address1 = address1;
		_address2 = address2;
	public String getAdress1() 
		return _address1;			
	public String getAdress2() 
		return _address2;	

	public void setCity(String city) 
		_city = city;
	public String getCity() {
		return _city;

	public void setState(String state) 
		_state = state;
	public String getState() {
		return _state;

	public void setPostal(String postal) 
		_postal = postal;
	public String getPostal() 
		return _postal;			

	public void setDescription(String description) 
		_description = description;
	public String getDescription() 
		return _description;

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:

C:\Inetpub\wwwroot\asp.net\inheritance>csc /t:library /out:..\bin\Profile.dll Profile.cs

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 bin directory.

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!

  • Read Part 2!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article