    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Introduction to C# - Part 2</title>
        <link>https://members.accu.org/index.php/articles/685</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 16, #4 - Aug 2004</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c101/">164</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-101/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+101/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Introduction to C# - Part 2</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 August 2004 13:16:06 +01:00 or Tue, 03 August 2004 13:16:06 +01:00</p>
<p><strong>Summary:</strong>&nbsp;<p>Welcome to the second in a series of articles introducing the C# programming language. In the previous issue the basics of the language such as variables, methods and classes were covered. In this issue classes are covered in more detail highlighting an important feature called inheritance.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a>Welcome
Back</h2>
</div>
<p>Welcome to the second in a series of articles introducing the C#
programming language. In the previous issue the basics of the
language such as variables, methods and classes were covered. In
this issue classes are covered in more detail highlighting an
important feature called <span class=
"emphasis"><em>inheritance</em></span>. If you are just tuning in,
and have some programming experience, this article should still be
digestible.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>Classes,
Objects and Types</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e31" id="d0e31"></a>A Touch of
Class</h3>
</div>
<p>One of the most common descriptions of classes that I have found
is, &quot;<span class="quote">A class is a template for creating objects
frequently used to model real world entities such as a bank account
or automobile.</span>&quot; This is an excellent and concise description
of a class but for me it helped to relate the idea of an &quot;object
template&quot; to something I was already familiar with. Another place
templates are found is in word processing applications and as it
turns out this is a good analogy.</p>
<p>Document templates specify fonts for different sections, nice
borders and graphics as well as the overall layout. A resume
template might define sections for contact information, objective,
work history and education, including appropriate formatting for
headings and body text. When I need to whip up a new resume I open
my word processing application and create a new document from the
resume template. Now all that's left to do is fill in the
information that is unique to me and I have a shiny new resume.</p>
<p>A class can also be created that specifies the different
sections, borders, graphics and layout for a resume.</p>
<pre class="programlisting">
public class Resume {
  public string Name, PhoneNumber, Objective;
  ...
  public void Print() {
    ...
  }
  ...
}
</pre>
<p>This <tt class="classname">Resume</tt> class contains <tt class=
"type">string</tt> member variables that store text for the same
sections that the document template provides. It also has a
<tt class="methodname">Print</tt> method that might print the
resume to the screen for review or to a printer.</p>
<p>Now that we have our template we follow a process similar to
creating our resume with the word processor. First we need to
create a new object from the class just as we created a new
document from the word processor's resume template.</p>
<pre class="programlisting">
Resume myResume = new Resume();
</pre>
<p>Now that we have our handy new object we need to fill in our
information.</p>
<pre class="programlisting">
myResume.Name = &quot;Mike Bergin&quot;;
myResume.PhoneNumber = &quot;555-2234&quot;;
myResume.Objective = &quot;Make tons of money!&quot;;
</pre>
<p>The resume now contains all of my relevant personal information
so now it's time to print.</p>
<pre class="programlisting">
myResume.Print();
</pre>
<p>There are a few important points to note about this example. The
first point is that when we modify an object, or document, it does
not modify the template. The second point is if we create two
objects, or two documents, from the same template, changes to one
of them are not reflected in the other. For example if I enter my
name into one object, or document, it does not appear in yours, the
two are unique entities.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e70" id="d0e70"></a>What Type of
Person Are You?!</h3>
</div>
<p>Classes are also called <span class=
"emphasis"><em>types</em></span>. This use of the word type is
similar to how people use it to categorize each other. For example
a person confronting someone they are upset with might say, &quot;I
didn't know you were that type of person.&quot; They are referring to
the actions, feelings or thoughts of someone, just as calling a
member variable a type is a categorization of the methods, actions,
and data of that piece of software. Assuming that the argument is
between a wife and her cheating husband the following example
provides a simple representation of &quot;that type of person&quot; in
software.</p>
<pre class="programlisting">
class Cheater {
  string Name, SpouseName, MistressName;
  void PerformUnfaithfulAct() {
    // Outside the scope of this article
  }
}
</pre>
<p>This snippet of code defines a new class, or type, named
<tt class="classname">Cheater</tt> containing three <tt class=
"type">string</tt> member variables and one method. Instances of
this class could be created to represent all the cheating husbands
of the world, all created from the same template, but each
unique.</p>
<pre class="programlisting">
void CreateCheatingHusbands() {
  Cheater bob = new Cheater();
  bob.Name = &quot;Bob Smith&quot;;
  Cheater john = new Cheater();
  john.Name = &quot;John Doe&quot;;
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e90" id="d0e90"></a>Inheritance</h3>
</div>
<p>Consider a scenario where you need to write a textbook
describing each star in the solar system for an advanced astronomy
course. The descriptions must be extremely precise, covering
information that might even change during the lifetime of the book
due to new discoveries in modern science. The first thing you would
most likely do is identify the characteristics shared by all stars.
Next you might categorize the different types of stars based on
some common characteristics, for example by size and temperature
grade. Now that the generic characteristics and groups of similar
stars have been identified you might begin writing.</p>
<p>The first chapters of your book would most likely cover the
common characteristics of all stars, which you identified in the
initial phases of analysis. These chapters would contain very
general information such as the chemical elements that stars are
composed of, however it would not contain the amounts because they
are different for each star. Next you would pick a category of
star, such as white dwarfs, describing only what makes that group
of stars unique. You would be careful not to provide any redundant
information simply by referring back to previous sections. Once you
had covered each category you would cover each specific star,
providing information about what makes it unique among its group
such as its name, coordinates and precise chemical composition.</p>
<p>As new discoveries are made some of the information in the
textbook may become invalid. For example an astronomer may discover
a new element present in all stars. Updating your textbook to
reflect this information would be relatively simple because the
topic of composition was only covered once and then referred back
to, so the text only needs to be updated in one place. Another side
effect of this approach is that it is much quicker to describe each
star in relation to a set of common characteristics instead of
describing each in isolation.</p>
<p>Source code is essentially a description of the data and
behavioural composition of an application. When a program is
written using a language that allows software to be expressed by
modelling real world entities, such as C#, developers follow the
same basic process we did in writing the textbook. In order to
illustrate this let's apply the same steps to the development of an
address book application used to manage contact information.</p>
<p>The first step is to identify the common characteristics of all
contacts managed by the application. We perform our analysis by
examining a few examples of contacts the application will need to
manage. One example is our Aunt Marilyn, who we find has a name,
phone number address, birthday and email address. Another example
we examine is our favourite pizza restaurant, and we find that it
has a name, phone number, address and website. Comparing these two
examples we find that they both have a name, phone number and
address. Now that we have identified the characteristics common to
all contacts we can write this description in C#.</p>
<pre class="programlisting">
public class Contact {
  public string Name, PhoneNumber, Address;
}
</pre>
<p>The next step that we took in writing our textbook was to
identify groups of similar items so we do that next. By performing
our analysis of the characteristics shared by all contacts we have
actually identified these groups, <span class=
"emphasis"><em>people</em></span> and <span class=
"emphasis"><em>businesses</em></span>. Before writing our
description of these two groups we must keep in mind that we don't
want to provide any redundant information, we only provide what
makes them unique.</p>
<pre class="programlisting">
public class Person : Contact {
  public string EmailAddress, BirthDay;
}

public class Business : Contact {
  public string WebSite;
}
</pre>
<p>You may notice that the name of the Contact class follows the
name of each new class separated by a colon. In C# this notation is
used to indicate that the reader should refer to another section of
the source code for more information. In this case we are saying
that the description of <tt class="classname">Person</tt> is the
sum of the description provided and the description of <tt class=
"classname">Contact</tt>. In our textbook we would have appended a
superscript numeral to a word and then indicated at the bottom of
the page that the reader is to refer back to a particular section
to provide this same indication.</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>The sun is composed1 of 91.2% Hydrogen, 8.7% Helium &hellip; 1.
Refer back to Chapter 12 Composition for more information.</p>
</blockquote>
</div>
<p>Now that we have identified the common characteristics of all
contacts and the groups, let's review exactly what we have. First
we found that all contacts have a name, phone number and address.
Once we had identified these traits we described them in the
<tt class="classname">Contact</tt> class. The next product of our
analysis was the identification of two groups of contacts, namely
people and businesses. We found that all people have an email
address and birthday in addition to what was already described in
the <tt class="classname">Contact</tt> class, and described this in
the <tt class="classname">Person</tt> class. Businesses were found
to all have a website in addition to what was already described in
the Contact class, so we described this in the <tt class=
"classname">Business</tt> class. Whew! Now that we have the hard
part done its time to describe each unique entry in our address
book.</p>
<p>In software written in C#, classes are used to describe the
groups of items we are working with and objects are used to
represent the members of those groups. So in this particular case
we now create instances of the <tt class="classname">Person</tt>
class for each person in our address book and instances of the
<tt class="classname">Business</tt> class for each business.</p>
<pre class="programlisting">
Person auntMarilyn = new Person();
auntMarilyn.Name = &quot;Marilyn Bergin&quot;;
auntMarilyn.EmailAddress =
                  &quot;marilyn@somewhere.com&quot;;
...
Business pizzaPlaza = new Business();
pizzaPlaza.Name = &quot;Pizza Plaza&quot;;
pizzaPlaza.WebSite = &quot;www.pizzaplaza.com&quot;;
...
</pre>
<p>The execution of the code in the last snippet deserves a bit of
explanation so let's run through it line by line to see what's
happening. On the first line a new instance of the <tt class=
"classname">Person</tt> class is created and stored in a variable
named <tt class="varname">auntMarilyn</tt>. The next line stores
the string <tt class="literal">Marilyn Bergin</tt> in the
<tt class="varname">Name</tt> member variable of the <tt class=
"varname">auntMarilyn</tt> object. When the computer reads this
line of code it looks at the <tt class="classname">Person</tt>
class for a description of a member variable named <tt class=
"varname">Name</tt>. The computer doesn't find it there but the
class indicates that the computer should also look at the
<tt class="classname">Contact</tt> class for more information,
where it does find a member variable named <tt class=
"varname">Name</tt>. Next the string <tt class=
"literal">marilyn@somewhere.com</tt> is stored in a member variable
named <tt class="varname">EmailAddress</tt>. Again the computer
looks back to the <tt class="classname">Person</tt> class for a
description of a member variable and this time it finds it. The
same process is followed for the lines that deal with the instance
of the <tt class="classname">Business</tt> class. The computer
looks in the <tt class="classname">Business</tt> class for
information and if it doesn't find it refers to the <tt class=
"classname">Contact</tt> class as indicated.</p>
<p>Inheritance is the ability to describe a section of code in
relation to another section of code. As we saw in the <tt class=
"classname">Person</tt> and <tt class="classname">Business</tt>
classes, C# provides the inheritance notation, the colon followed
by the name of the generic class, to indicate that everything from
that class should be pulled into a new class that also adds a few
items of its own. So when working with an instance of the Person
class we know it not only contains the member variables defined in
its own class definition but also those defined in the <tt class=
"classname">Contact</tt> class's definition as well. This is
referred to as one class inheriting from another class. In this
scenario the <tt class="classname">Contact</tt> class is referred
to as the <span class="emphasis"><em>base class</em></span> or
<span class="emphasis"><em>superclass</em></span> and the
<tt class="classname">Person</tt> class is called the <span class=
"emphasis"><em>subclass</em></span> or <span class=
"emphasis"><em>derived class</em></span>.</p>
<p>Inheritance is an extremely powerful feature that provides a
number of benefits when used properly. The two main aspects of
inheritance to consider are that it cuts down on the sheer amount
of source code and that many classes can inherit from the same
class. Cutting down on the amount of source code helps us to
realize the one of the big promises of OOP, code reuse. When we
reuse code we cut down on development time because we don't need to
write as much, and maintenance is also cut down, which has been
shown to be more expensive than development in many cases. The
second point to note is that many classes can share the same base
class, and as we will see in the following sections this is another
extremely powerful aspect of inheritance.</p>
<p>There is a fair amount of terminology used when discussing
inheritance and software that supports this feature so before we
continue it may be helpful to provide some definitions. As we have
seen a class that is inherited from is referred to as a
<span class="emphasis"><em>base class</em></span> or a <span class=
"emphasis"><em>superclass</em></span>. A class that inherits from
another is called a <span class="emphasis"><em>derived
class</em></span> or a <span class=
"emphasis"><em>subclass</em></span>. Also in both of these
scenarios the word type might be used interchangeably with the word
class such as <span class="emphasis"><em>derived type</em></span>
instead of <span class="emphasis"><em>derived class</em></span>.
When a class inherits from another class the act of doing so is
referred to as <span class="emphasis"><em>subclassing</em></span>.
Inheriting from another and providing additional members or other
customizations is called <span class=
"emphasis"><em>specialization</em></span>. You don't need to commit
all of these to memory, just know they are here if you see some
confusing jargon later.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e254" id="d0e254"></a>Leveraging
Inheritance</h3>
</div>
<p>Now that we have explored the basic concept of inheritance and
how it is supported by C# we can move on to some other features
that are implemented through the use of inheritance.</p>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e259" id=
"d0e259"></a>Polymorphism</h4>
</div>
<p><span class="emphasis"><em>Polymorphism</em></span> is the
ability of a variab le to reference an instance of the variable's
delcared type <span class="emphasis"><em>or a subclass of
it</em></span>. For example, a variable of type <tt class=
"classname">Contact</tt> could be used to store an instance of
either the Person or <tt class="classname">Business</tt>
classes.</p>
<pre class="programlisting">
Contact contact = new Person();
</pre>
<p>When an instance of a subclass is referenced using a variable of
its base class's type, only the members declared in the base type
are accessible. In the preceding example if we had tried to
reference the <tt class="varname">EmailAddress</tt> member variable
we would have received an error when compiling the code. The
reasons for this are purely mechanical; only instances of
<tt class="classname">Contact</tt> or one its subclasses may be
stored in the variable because we are only guaranteed that the
members defined in the <tt class="classname">Contact</tt> class are
supported.</p>
<p>This makes sense because <tt class="classname">Person</tt>
inherits all members defined in the Contact class so it exposes the
same methods, properties, variables, etc. One catch to this is that
only the members defined by the variable's type may be used no
matter what type of object is actually stored in the variable.
Simply put, if we store an instance of the <tt class=
"classname">Person</tt> class in a <tt class=
"classname">Contact</tt> variable we can only access members
declared in the <tt class="classname">Contact</tt> class. You might
be thinking that the compiler knows we are storing a <tt class=
"classname">Person</tt> object in the variable because the code is
on the same line. This is a valid point in this situation but
consider if we used polymorphism when calling a method.</p>
<pre class="programlisting">
public void Close(Contact contact) { ... }
public void Main() {
  Close(new Person());
}
</pre>
<p>In the body of the <tt class="methodname">Close</tt> method we
couldn't know what type had actually been passed in, just that it
supports all of the members defined in the <tt class=
"classname">Contact</tt> class. This is compounded by the fact that
code might be introduced at a later time that calls this method,
passing an instance of a class that didn't exist when the code
containing the <tt class="methodname">Close</tt> method was
written.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e318" id=
"d0e318"></a>Specialization</h4>
</div>
<p>Inheritance allows us to pull one class's implementation into
another, which as we've seen can be extremely useful. There are
many times however when the inherited implementation doesn't quite
meet our needs, luckily C# allows us to effectively replace
inherited members with our own implementations. Not all members may
be replaced, C# requires class authors to declare that a member may
be replaced using the <tt class="literal">virtual</tt> keyword.
Subclasses may replace <tt class="literal">virtual</tt> members by
providing one with the same signature but replacing <tt class=
"literal">virtual</tt> with the <tt class="literal">override</tt>
keyword.</p>
<pre class="programlisting">
public class Contact {
  public virtual void Save(Connection conn) {
    // Save member variable values
  }
}

public class Person : Contact {
  public override void Save(Connection conn) {
    base.Save();
    // Save extended member variable values
  }
}
</pre>
<p>In this code snippet we added a new <tt class=
"literal">virtual</tt> method named <tt class="varname">Save</tt>
to the <tt class="classname">Contact</tt> class that saves its
member variables. Classes that inherit this method must replace it
with their own, which saves any new members they introduce in
addition to the inherited members. In order to simplify this task,
C# allows subclasses to explicitly refer to members of their
superclass using the base keyword, even when it's been overridden.
The base keyword allows subclasses to not just replace a member but
to reuse it, in effect customizing the existing member for its
specific purpose.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e348" id="d0e348"></a>Combining
Polymorphism and Specialization</h4>
</div>
<p>Polymorphism and specialization can be combined in powerful ways
to develop extremely dynamic software. Here we will make use of the
new <tt class="methodname">Save</tt> method introduced in the
previous example in an attempt to showcase some of the power
available to C# programmers.</p>
<pre class="programlisting">
public void Persist(Contact contact) {
  Connection conn = GetConnection();
  Contact.Save(conn);
  CloseConnection(conn);
}

public static void Main() {
  Person auntMarilyn = new Person();
  auntMarilyn.EmailAddress =
                    &quot;marilyn@somewhere.com&quot;;
  ...
  Business pizzaPlaza = new Business();
  pizzaPlaza.WebSite = &quot;www.pizzaplaza.com&quot;;
  ...
  Persist(auntMarilyn);
  Persist(pizzaPlaza);
}
</pre>
<p>In the example above we defined a method named <tt class=
"methodname">Persist</tt> that saves any instance of the <tt class=
"classname">Contact</tt> class or one of its subclasses. When the
instance of the <tt class="classname">Person</tt> class is passed
to the <tt class="methodname">Persist</tt> method the <tt class=
"methodname">Save</tt> method implemented in the <tt class=
"classname">Person</tt> class is called, causing all of its
variables to be saved. When the instance of the <tt class=
"classname">Business</tt> class is passed however, only the
variables declared in the <tt class="classname">Contact</tt> class
are saved because the <tt class="classname">Business</tt> class did
not customize the <tt class="methodname">Save</tt> method to
include the variables it introduced. As we can see the <tt class=
"methodname">Persist</tt> method doesn't care what the actual type
stored in the variable is, only that it supports the <tt class=
"methodname">Save</tt> method.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e396" id="d0e396"></a>Interfaces</h4>
</div>
<p>The term <span class="emphasis"><em>interface</em></span> refers
to the mechanisms provided to interact with a class, such as the
member variables, methods and properties it exposes.</p>
<p>An interface is declared in much the same way as a class is
declared, except that it does not provide any implementation, the
parts between the opening { and closing }. A class can declare that
it implements a particular interface and then provide
implementations of all the interface's members. Variables may
declare that they hold instances of a particular interface just as
a variable may declare that it contains an instance of a particular
class. Interfaces cannot be instantiated, but classes that
implement the interface may be stored in one of these
variables.</p>
<p>In the section on polymorphism the <tt class=
"classname">Contact</tt> class defined a method named Save that
saved the values stored in the object's variables to a database.
Instead of this method being declared in the <tt class=
"classname">Contact</tt> class it could have been declared in an
interface instead. This might be the case if you were using a third
party persistence framework for example. The interface provided by
the third party framework would be implemented by objects that need
to be persisted, and the framework would do the rest. In this
scenario the <tt class="classname">Contact</tt> class would most
likely implement the interface and provide default implementations
for each interface member. Let's assume the interface from the
persistence framework is defined as follows.</p>
<pre class="programlisting">
public interface IPersistable {
  public void Save(Connection conn);
  public void Read(Connection conn);
}
</pre>
<p>Assuming this is the interface required by the persistence
framework, we would need to make a few changes to our classes if
they are to take advantage of this new persistence framework. First
we would change the <tt class="classname">Contact</tt> class to
implement the interface.</p>
<pre class="programlisting">
public class Contact : IPersistable {

  ...

  public void Save(Connection conn) {
    // Save to the database...
  }

  public void Read(Connection conn) {
    // Read from the database...
  }
}
</pre>
<p>There are a few points to note about this example. First, the
notation used to declare that a class implements an interface is
exactly the same as for declaring inheritance. If a class both
inherits from a class and implements an interface then the
interface name follows the base class name separated by a comma.
For example if the <tt class="classname">Contact</tt> class
inherited from a class named <tt class="classname">Widget</tt> then
it would be modified as shown below.</p>
<pre class="programlisting">
public class Contact : Widget, IPersistable {
  ...
}
</pre>
<p>It is legal for a single class to implement multiple interfaces,
in which case each additional interface would be listed in the same
comma delimited fashion. Interface members implemented by a class
are implicitly virtual. Now that we have implemented this interface
in our <tt class="classname">Contact</tt> class, let's look at the
changes we need to make to the <tt class="classname">Person</tt>
class.</p>
<pre class="programlisting">
public class Person : Contact {
  public override void Save(Connection conn) {
    base.Save(conn);
    // Save members defined in Person
  }

  public override void Read(Connection conn) {
    base.Read(conn);
    // Read members defined in Person;
  }
}
</pre>
<p>Notice that we did not explicitly declare that the <tt class=
"classname">Person</tt> class implements the <tt class=
"classname">IPersistable</tt> interface. The reason for this is
that interface implementation is transitive, meaning that because
the <tt class="classname">Person</tt> class inherits from the
<tt class="classname">Contact</tt> class it implicitly implements
the interface, so declaring it again would be redundant. In the
methods that override the inherited implementations of the
interface members we again make use of the base class's
implementation, being careful to not reproduce any code we can
reuse.</p>
<p>Now that we have examined how an interface is implemented, let's
take a look at how these changes might be used in the persistence
framework. The following code snippet might exist in a class that
facilitates the persistence of objects using the framework.</p>
<pre class="programlisting">
public class PersistenceManager {
  public void Persist(IPersistable subject) {
    Connection conn = GetConnection();
    Subject.Save(conn);
    ReleaseConnection(conn);
  }
}
</pre>
<p>This illustrates that the important point that interfaces, like
inheritance, allow for polymorphism. The <tt class=
"classname">PersistenceManager</tt> class doesn't care how the
<tt class="methodname">Save</tt> method is implemented by the
instance passed into the method, or even what type it actually is.
All that matters is that the parameter implements the proper
interface.</p>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e472" id="d0e472"></a>Abstract
Classes</h2>
</div>
<p><span class="emphasis"><em>Abstract classes</em></span> are a
mix between using inheritance to reuse code and leveraging
polymorphism with interfaces. An abstract class may contain both
concrete members and interface style members that don't declare any
implementation. The concrete members are inherited by derived
classes just as normal methods are and can use the <tt class=
"literal">virtual</tt>/<tt class="literal">override</tt> keywords
in the same way as well. The interface style members require that
subclasses provide an implementation for them, just as implementing
an interface require the implementers to do. Essentially that's all
there is to it, so let's go over a quick example.</p>
<p>The example we reviewed in the section on interfaces could have
also been implemented using abstract classes. Implementing the
persistence framework using inheritance allows more power to be
embedded into the <tt class="classname">Contact</tt> class itself.
In many cases frameworks that use abstract classes instead of
interfaces take this approach and sometimes are able to minimize
the amount of work on the part of the user of the framework. In
this new example we will be inheriting from an abstract class
defined as shown below.</p>
<pre class="programlisting">
public abstract class DatabaseObject {
  public void Save(Connection conn) { ... }
  public void Read(Connection conn) { ... }
  public abstract bool IsDirty();
  public abstract void ClearDirty();
}
</pre>
<p>The declaration of this class includes a keyword that we haven't
encountered before, abstract. The abstract keyword must appear in
the declaration of a class that has any abstract members. This same
keyword appears in the two interface style member declarations
<tt class="methodname">IsDirty</tt> and <tt class=
"methodname">ClearDirty</tt>. Just like with interfaces, no
implementation is provided, requiring subclasses to provide it. The
<tt class="literal">abstract</tt> keyword must be used in these
declarations because this is not an interface, so it must be
clearly stated if a member is abstract.</p>
<p>In this new framework the <tt class="methodname">Save</tt> and
<tt class="methodname">Read</tt> methods are actually implemented
for us, all that we as the users of the framework need to do is
implement a method that indicates if the object is dirty, meaning
that data has been modified since it was last saved to the
database. The framework might periodically check to see if the
<tt class="methodname">IsDirty</tt> method returns true and if so
call the <tt class="methodname">Save</tt> method, followed by a
call to the <tt class="methodname">ClearDirty</tt> method to
indicate that the values have been saved. Tracking when changes
were made to the object is left up to the object itself, however
they are now automatically saved to the database by simply
returning true from a method call. In closing, the following
example shows how the <tt class="classname">Contact</tt> class
might look if it used this inheritance based framework.</p>
<pre class="programlisting">
public class Contact : DatabaseObject {

  public bool hasChanged;

  public override bool IsDirty() {
    return hasChanged;
  }

  public override void ClearDirty() {
    hasChanged = false;
  }
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e525" id="d0e525"></a>See You Next
Time</h2>
</div>
<p>In this issue we discussed some of the fundamental features of
classes. In the next issue we will cover a few more class-related
features, as well as the type system supported by C#. If you read
the first article and are wondering what happened to the Address
Book application, we will eventually get to incorporate everything
we've covered. See you next time!</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
