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), and methods of the derived class have access to all the public and protected members of the base 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 . (Many C# programmers advise never hiding base class methods as it is unreliable, hard to maintain, and confusing.)
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 the section "Versioning with new and override," later in this chapter.) Example 11-1. Deriving a new class
The output looks like this: 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 as the object is constructed . 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. 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. If you create a new class, myOtherClass , that derives from myClass , like this: public class MyClass : MyOtherClass { Console.WriteLine("myInt: {0}", myInt); } MyOtherClass can access myInt , because MyOtherClass derives from MyClass , and myInt is protected. Any class that doesn't derive from MyClass would not be able to access myInt .
|