Inheritance in C# - Inheritance
(Page 3 of 4 )
In 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 thatListBoxinherits from (or derives from)Windowindicates that it specializesWindow.Windowis referred to as the base class, andListBoxis referred to as the derived class. That is,ListBoxderives its characteristics and behaviors from Window and then specializes to its own particular needs.
You’ll often see the immediate base class referred to as the parent class, and the derived class referred to as the child class, while the topmost class,Object, is called the root class.
Implementing Inheritance In 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 fromWindow. 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 keywordnew. (Many C# programmers advise never hiding base class methods as it is unreliable, hard to maintain, and confusing.)
This is a different use of the keywordnewthan you’ve seen earlier in this book. In Chapter 7,newwas used to create an object on the heap; here,newis used to replace the base class method. Programmers say the keywordnewis overloaded, which means that the word has more than one meaning or use.
Thenewkeyword indicates that the derived class has intentionally hidden and replaced the base class method, as shown in the Example 11-1. (Thenew keyword is also discussed in the section “Versioning with new and override,” later in this chapter.)
Example 11-1. Deriving a new class
using 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();
}
}
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 classWindow. This class implements a constructor and a simpleDrawWindow()method. There are two private member variables,topandleft. The program is analyzed in detail in the following sections.
Next: Calling Base Class Constructors >>
More C# Articles
More By O'Reilly Media
|
This article is excerpted from chapter 11 of Learning C# 2005, Second Edition, written by Jesse Liberty and Brian MacDonald (O'Reilly, 2006; ISBN: 0596102097). Check it out today at your favorite bookstore. Buy this book now.
|
|