Article

Object Oriented C# for ASP.NET Developers

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Next

Advanced Inheritance: Overriding Methods

I covered inheritance earlier in this article, but I left out one advanced issue for the sake of brevity that I'd like to cover now: overriding methods. As you know, an object of class CoconutTree inherits all of the features of the Tree class, on which it is based (i.e. CoconutTree is a subclass of Tree). Thus, CoconutTrees have Grow methods just like Trees do.

But what if you wanted CoconutTrees to sprout new coconuts when they grew? Sure, you could call the GrowNut method every time you caused a CoconutTree to grow, but it would be nicer if you could treat Trees and CoconutTrees exactly the same way (i.e. call their Grow method) and have them both do what they're supposed to do when objects of their type grow.

To have the same method do something different in a subclass, you must override that method with a new definition in the subclass. Put simply, you can re-declare the Grow method in the CoconutTree class to make it do something different! Here's a new definition for grow that you can add to your CoconutTree class:

 public void Grow() {          
   height = height + 1;          
   GrowNut();          
 }

Simple, right? But what if you added new functionality to the Grow method in the Tree class (e.g. to grow leaves)? How could you make sure that this was still inherited by the CoconutTree class without having to make the change in both places? Like in our discussion of overloaded methods, where we implemented a simple method by calling a special case of the more complicated method, we can implement a new definition for a method in a subclass by referring to its definition in the base class (also called the parent class or superclass):

 public void Grow() {          
   base.Grow();          
   GrowNut();          
 }

The base.Grow() line invokes the version of Grow defined in the base class (Tree), thus saving us from having to reinvent the wheel. This is especially handy when you are creating a class that extends a class for which you do not have the source code (e.g. a class provided by another developer, or built into .NET). By simply calling the base class versions of the methods you are overriding, you can ensure that your objects aren't losing any functionality.

Constructors may be overridden just like normal methods, but calling the version of the constructor in the base class is slightly different. Here's a set of constructors for the CoconutTree class, along with the new declaration of the numNuts field without an initial value:

 private int numNuts;          
         
 public CoconutTree() : base() {          
   numNuts = 0;          
 }          
         
 public CoconutTree(int height) : base(height) {          
   numNuts = 0;          
 }          
         
 public CoconutTree(int height, int numNuts) : base(height) {          
   if (numNuts < 0) this.numNuts = 0;          
   else this.numNuts = numNuts;          
 }

The first two constructors override their equivalents in the Tree class, while the third is completely new. Notice that we call the constructor of the base class as base(), but instead of putting this call inside the constructor body, we add it to the end of the constructor declaration, using the : operator. All three of our constructors call a constructor in the base class to ensure that we are not losing any functionality.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links