Using Inheritance to Make New Classes

The other major concept I introduced at the beginning of this chapter is inheritance. Like many computing words, inheritance is borrowed from the nontechnical world, but computing types added new significance to the term. The basic idea of inheritance in object-oriented programming is similar to the genetic meanings of inheritance: You have characteristics of your parents and your grandparents. Objects also have a family tree. Nearly every object in C# has exactly one parent. That parent might have a parent, and this class might also have a parent. A class can inherit characteristics from each member of its family tree, just as you might have your grandmother’s nose and your mother’s eyes. Inheritance in C# is much simpler than in genetics, however, because each class has only one parent.

Some OOP languages (such as C++) allow an object to have more than one parent class, but multiple inheritance causes many more problems than it solves. C# uses an inheritance model that is simpler and less prone to error.

Creating a Class to View the Clone

To illustrate inheritance, I’ll show you the simplest example of inheritance I know.

Figure 5.8 illustrates the output of another version of the Critter program. From the output, this looks very much like the other programs you have seen in this chapter. However, when you look at the code that creates the Clone class, you’ll find a surprise.

Figure 5.8. The output looks very familiar, but Dolly is actually a clone!


Note that the code for CritViewer is also familiar:

using System;

namespace CritClone
{
  /// <summary>
  /// Another Critter Viewer
  /// This one demonstrates inheritance by making a clone
  /// Andy Harris, 12/21/01
  /// </summary>
  class CritViewer
  {
    static void Main(string[] args)
    {
      CritViewer cv = new CritViewer();
    } // end main

    public CritViewer(){
      Clone myClone = new Clone();
      myClone.name = "Dolly";
      Console.WriteLine(myClone.talk());

      Console.WriteLine("Please press Enter to continue");
      Console.ReadLine();
    } // end constructor
  } // end CritViewer class
} // end namespace

This program never directly invokes the Critter class. Instead, it makes an instance of the Clone class. The clone acts much like a critter. When you type it into the editor, you get the same list of properties as when you are working directly with a critter. Also, the clone has a talk() method that works just like the Critter class.

Creating the Critter Class

From all the evidence, a clone would seem to be almost exactly like a critter. Here’s the code for the clone:

using System;

namespace CritClone
{
  /// <summary>
  /// The Clone is a very simple class
  /// Illustrates basic inheritance
  /// Andy Harris, 12/21/01
  /// </summary>
  public class Clone: Critter
  {
     // there's nothing here!
  } // end class
} // end namespace

The most startling part of the Clone class is what isn’t there. The clone has no properties, methods, or constructors, yet it acts as if it has them. The Critter Viewer program uses the talk() method and changes the name property. The Clone class acts much like the Critter class, but I didn’t have to rewrite the critter’s characteristics. The key is in the way the class is derived. Look at this line of code:

public class Clone: Critter

This line defines Clone, but the colon followed by a class name indicates that the Clone is derived from Critter. In other words, I am starting from the Critter class, so the Clone class will start with all the characteristics of the Critter class. Without writing a single new line of code, I’ve made a new class related to an existing class.

Having an exact duplicate of an existing class is pointless without modification. I wanted to show you the concept of inheritance with a clear example. In the rest of this chapter you will learn how to make modifications to your copy so that it has new behavior, methods, and properties.

Because Clone is derived from Critter, it inherits all of Critter’s characteristics. Clone has a talk() method because Critter does, and Clone is derived from Critter. Clone also has access to all of Critter’s other properties and methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.165.62