When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

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:

Overview
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!


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES