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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Tuesday, February 20, 2001

Using Object-Orientation in ASP.NET: Inheritance, Part 2 - 4GuysFromRolla.com

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!

    - continued -

    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


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