11.2 InheritanceIn C#, the specialization relationship is implemented using a principle called inheritance. This is not the only way to implement specialization, but it is the most common and most natural way to implement this relationship. Saying that ListBox inherits from (or derives from) Window indicates that it specializes Window. Window is referred to as the base class, and ListBox is referred to as the derived class. That is, ListBox derives its characteristics and behaviors from Window and then specializes to its own particular needs. 11.2.1 Implementing InheritanceIn C#, you create a derived class by adding a colon after the name of the derived class, followed by the name of the base class: public class ListBox : Window This code declares a new class, ListBox, that derives from Window. You can read the colon as "derives from." The derived class inherits all the members of the base class (both member variables and methods). These members can be treated just as if they were created in the derived class. The derived class is free to implement its own version of a base class method. This is called hiding the base class method and is accomplished by marking the method with the keyword new.
The new keyword indicates that the derived class has intentionally hidden and replaced the base class method, as shown in the Example 11-1. (The new keyword is also discussed in Section 11.3.2, later in this chapter.) Example 11-1. Deriving a new classusing System; public class Window { // constructor takes two integers to // fix location on the console public Window(int top, int left) { this.top = top; this.left = left; } // simulates drawing the window public void DrawWindow() { Console.WriteLine("Drawing Window at {0}, {1}", top, left); } // these members are private and thus invisible // to derived class methods; we'll examine this // later in the chapter private int top; private int left; } // ListBox derives from Window public class ListBox : Window { // constructor adds a parameter public ListBox( int top, int left, string theContents): base(top, left) // call base constructor { mListBoxContents = theContents; } // a new version (note keyword) because in the // derived method we change the behavior public new void DrawWindow() { base.DrawWindow(); // invoke the base method Console.WriteLine ("Writing string to the listbox: {0}", mListBoxContents); } private string mListBoxContents; // new member variable } public class Tester { public static void Main() { // create a base instance Window w = new Window(5,10); w.DrawWindow(); // create a derived instance ListBox lb = new ListBox(20,30,"Hello world"); lb.DrawWindow(); } } Output: Drawing Window at 5, 10 Drawing Window at 20, 30 Writing string to the listbox: Hello world Example 11-1 starts with the declaration of the base class Window. This class implements a constructor and a simple DrawWindow() method. There are two private member variables, top and left. The program is analyzed in detail in the following sections. 11.2.2 Calling Base Class ConstructorsIn Example 11-1, the new class ListBox derives from Window and has its own constructor, which takes three parameters. The ListBox constructor invokes the constructor of its parent by placing a colon (:) after the parameter list and then invoking the base class constructor with the keyword base: public ListBox( int theTop, int theLeft, string theContents): base(theTop, theLeft) // call base constructor Because classes cannot inherit constructors, a derived class must implement its own constructor and can only make use of the constructor of its base class by calling it explicitly. If the base class has an accessible default constructor, the derived constructor is not required to invoke the base constructor explicitly; instead, the default constructor is called implicitly. However, if the base class does not have a default constructor, every derived constructor must explicitly invoke one of the base class constructors using the base keyword. The keyword base identifies the base class for the current object.
11.2.3 Overriding Base MethodsNotice in Example 11-1 that ListBox implements a new version of DrawWindow(): public new void DrawWindow() The keyword new indicates that the programmer is intentionally creating a new version of this method in the derived class. In Example 11-1, the DrawWindow() method of ListBox hides and replaces the base class method. When you call DrawWindow() on an object of type ListBox, it is ListBox.DrawWindow() that is invoked, not Window.DrawWindow(). Note, however, that ListBox.DrawWindow() can invoke the DrawWindow() method of its base class with the code: base.DrawWindow(); // invoke the base method 11.2.4 Controlling AccessYou can restrict the visibility of a class and its members through the use of access modifiers, such as public, private, and protected. (See Chapter 8 for a discussion of access modifiers.) As you've seen, public allows a member to be accessed by the member methods of other classes, while private indicates that the member is visible only to member methods of its own class. The protected keyword extends visibility to methods of derived classes. Classes as well as their members can be designated with any of these accessibility levels. If a class member has a different access designation than the class, the more restricted access applies. Thus, if you define a class, myClass, as follows: public class myClass { // ... protected int myValue; } the accessibility for myValue is protected even though the class itself is public. A public class is one that is visible to any other class that wishes to interact with it. |