Article
Object Oriented C# for ASP.NET Developers
Inheritance
One of the strengths of object-oriented programming is inheritance. This feature allows you to create a new class that is based on an old class. Let's say your program also needed to keep track of coconut trees, so you would need a new class called CoconutTree that kept track of the number of coconuts in each tree. You could write the CoconutTree class from scratch, copying all the code from the Tree class that is responsible for tracking the height of the tree and allowing the tree to grow, but for more complex classes that could involve a lot of duplicated code. What would happen if you later decided that you wanted your Trees to have non-integer heights (like 1.5 metres)? You would have to adjust the code in both classes!
Inheritance allows you to define your CoconutTree class as a subclass of the Tree class, such that it inherits the fields and methods of that class in addition to its own. To see what I mean, here's the code for CoconutTree:
1 /**
2 * CoconutTree.cs
3 * A more complex kind of tree.
4 */
5
6 public class CoconutTree : Tree {
7 public int numNuts = 0; // Number of coconuts
8
9 public void GrowNut() {
10 numNuts = numNuts + 1;
11 }
12
13 public void PickNut() {
14 numNuts = numNuts - 1;
15 }
16 }
The code : Tree on line 6 endows the CoconutTree class with a height field and a Grow method in addition to the numNuts field (yes, this example was fiendishly conceived to produce a childishly amusing variable name -- what of it?) and the GrowNut and PickNut methods that are declared explicitly for the class. The diagram in Fig. 5 shows the relationship of our two classes.
Fig. 5 CoconutTree is a subclass of TreeBy building up a hierarchical structure of classes with multiple levels of inheritance, you can create powerful models of complex Objects with little or no duplication of code (which makes for less typing and easy maintenance). We'll see a very important example of the power of inheritance for ASP.NET developers before the end of this article.
Compiling Multiple Classes
You should now have two C# source files in your working directory -- Tree.cs and CoconutTree.cs. Tree.cs can still be compiled on its own as before, but since CoconutTree.cs refers to the Tree class, the compiler needs to know where to find that class to successfully compile CoconutTree.cs.
In most cases, the easiest way to proceed is to compile all your related classes into a single DLL (also known as an assembly in .NET terminology). To compile Tree.cs and CoconutTree.cs into a single DLL file called trees.dll that will contain the definitions of both classes, type the following command in the directory that contains the C# source files:
csc /target:library /out:trees.dll Tree.cs CoconutTree.cs
If the files you want to compile are the only C# source files in the directory, you can use a wildcard as a shortcut:
csc /target:library /out:trees.dll *.cs
You can then just plop the trees.dll file in the bin directory of your Web application and it will have access to both classes.