using System; public class Control { // 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; // Constructor takes two integers to // fix location on the console public Control(int top, int left) { this.top = top; this.left = left; } // Simulates drawing the window public void DrawControl() { Console.WriteLine("Drawing Control at {0}, {1}", top, left); } } // TextBox derives from Control public class TextBox : Control { private string mTextBoxContents; // new member variable // Constructor adds a parameter public TextBox( int top, int left, string theContents) : base(top, left) // call base constructor { mTextBoxContents = theContents; } // A new version (note keyword) because in the // derived method we change the behavior public new void DrawControl() { base.DrawControl(); // invoke the base method Console.WriteLine ("Writing string to the textbox: {0}", mTextBoxContents); } } public class Tester { public static void Main() { // Create a base instance Control contr = new Control(5, 10); contr.DrawControl(); // Create a derived instance TextBox txt = new TextBox(20, 30, "Hello world"); txt.DrawControl(); Console.ReadLine(); } }