Published: Tuesday, February 20, 2001
4GuysFromRolla.com - Using Object-Orientation in ASP.NET: Inheritance, Part 2
By Ian Stallings
Read Part 1
In Part 1 we looked at what, exactly, inheritance is
and how to use it to extend the functionality of a base class. In this part we'll look
at using the example examined in Part 1 and using
via an ASP.NET Web page!
So How Do We Call This Class From ASP.NET?
So we create our superclass and our subclass, big deal. But how do we actually use this?
We can call both classes directly from ASP.NET using the following test written in C#:
<%@ Import Namespace="Shai" %>
<html>
<style>
div
{
font: 8pt verdana;
background-color:cccccc;
border-color:black;
border-width:1;
border-style:solid;
padding:10,10,10,10;
}
</style>
<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs E) {
//we create the base class below:
Profile profile = new Profile();
Message.InnerHtml += "<u>Profile Class</u><br>";
Message.InnerHtml += "First: " + profile.getFirstName() +
"<br>Last: " + profile.getLastName() +
"<br>Phone: " + profile.getPhoneNumber() + "<br><br>";
//then we create an instance of the subclass:
ExtendedProfile extendedProfile = new ExtendedProfile();
Message.InnerHtml += "<u>ExtendedProfile Class</u><br>";
Message.InnerHtml += "First: " + profile.getFirstName() +
"<br>Last: " + profile.getLastName() +
"<br>Phone: " + extendedProfile.getPhoneNumber() +
"<br>Adress1: " + extendedProfile.getAdress1() +
"<br>Adress2: " + extendedProfile.getAdress2() +
"<br>City: " + extendedProfile.getCity() +
"<br>State: " + extendedProfile.getState() +
"<br>Postal: " + extendedProfile.getPostal() +
"<br>Description: " + extendedProfile.getDescription() +
"<br>";
}
</script>
<body style="font: 10pt verdana">
<b><h3>Simple Inheritance Example</h3></b><br><br>
Object Output:<br><br>
<div id="Message" runat="server"/>
</body>
</html>
|
This should produce the following output:
So as you can see inheritance will allow you to keep your original class signature but
still add additional properties/methods and/or modify them using a subclass. I have
attached the example source files, the generated dll, and this article in zip file format.
In my next article I will use the same example to show how polymorphism fits into the
picture by overriding/overloading the superclass' methods/properties allowing even more
programming flexibility!
Happy Programming!
By Ian Stallings
Attachments and Related Articles
Download the support files (in ZIP format)
Visit the ASP.NET Article Index
Visit the C# Article Index
Read Using Object-Orientation in ASP.NET : Overview
Read Using Object-Orientation in ASP.NET : Encapsulation