// Base Class class Control { // Properties // 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) { //The this keyword refers to the current instance of the class this.top = top; this.left = left; } // Simulates drawing the window public void DrawControl() { Console.WriteLine("Drawing Control at {0}, {1}", top, left); } } // DERIVED Class //TextBox class inherits from class Control //The derived class "Textbox" has the properties and methods of the base class (Parent class) Control class TextBox : Control { //TextBox has the following properties: // top and left that are inherited from the class Control // only accessed via accessors (set and get) // mTextBoxContents which is defined in the class TextBox private string mTextBoxContents; // new member variable // Constructor adds a parameter public TextBox( int top, int left, string theContents) : base(top, left) // call base constructor (constructor of Control) { mTextBoxContents = theContents; } // A new version (note keyword) because in the // derived method we change the behavior //When an instance of type "TextBox" calls the method DrawControl, this method will be executed // and not the one in the Control class public new void DrawControl() { base.DrawControl(); // invoke the base method Console.WriteLine ("Writing string to the textbox: {0}", mTextBoxContents); } } //TESTING 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(); } }