2.8 Inheriting from Other Classes

 <  Day Day Up  >  

You want to use a new class that is similar to another class and inherit some of the original base class's implementation.


Technique

Following the class declaration, insert a colon followed by the name of the class whose behavior your new class will inherit. Finally, override any of the base class's virtual functions to change any behaviors to more appropriately model your class. Any method in the base class that can be overridden should be marked virtual . Likewise, when overriding a method, use the override keyword in the derived class, as shown in Listing 2.7.

Listing 2.7 Using Inheritance to Define a Related Set of Objects
 using System; namespace _8_Inheritance {     // base class definition     public class MediaDevice     {         protected bool on = false;         virtual public void TurnOn()         {             on = true;         }         virtual public void TurnOff()         {             on = false;         }     }     public class Television : MediaDevice     {         private int curChannel = -1;         // turn on tv and set to channel 2         public override void TurnOn()         {             base.TurnOn ();             curChannel = 2;         }         // turns tv off         public override void TurnOff()         {             base.TurnOff ();             curChannel = -1;         }         public int Channel         {             get             {                 return curChannel;             }             set             {                 // can't change channel if tv is off                 if( on == false )                     return;                 curChannel = value;             }         }     }     class Class1     {         [STAThread]         static void Main(string[] args)         {             Television tv = new Television();             tv.TurnOn();             tv.TurnOff();         }     } } 

Comments

Inheritance is a fundamental feature of Visual C# .NET and appears everywhere within the .NET Framework. It allows you to extend the capabilities of an object by inheriting the implementation of some other base object. Object-oriented programming uses inheritance to further refine an abstract object by defining more specialized objects until a complete hierarchical system of objects is created. The .NET Framework, for instance, defines a System.Object type, which in itself is a rather abstract entity. Every object within the Framework is ultimately derived from this single object. When designing a system of objects, find the object traits that are the same throughout and create a base class from those traits. Continue organizing new objects based on this base class and further extract similar information until what you have left is a collection of derived objects that share the same commonality but are specialized based on their implementation details.

Listing 2.7 demonstrates creating a base class that you can use as a stepping stone to more specialized classes. A MediaDevice is something that provides some sort of electronic information delivery. It can be a radio; a computer; or, as shown in Listing 2.7, a television. Within the MediaDevice class, you can see two methods both marked with the virtual keyword. By using this keyword, you are allowing a derived class to override the implementation of this method. This step allows the more specialized class to further refine and add additional functionality for that method. The Television class uses the TurnOn and TurnOff methods to control the current state of the channel variable.

To override a virtual method, create a method with the same signature as the method you are overriding, but replace the virtual keyword with the override keyword. Furthermore, if you also need to call the base class's implementation of an overridden method, you can use the implied base member that exists within your class. Whereas the this keyword represents the current instance of a class, the base keyword represents the current instance of the base class a class is derived from.

A simple way to override a method is to begin typing the method signature within the source code window. When you finish the override keyword, an IntelliSense window lists all the possible virtual methods within the base class that you can override. Using the arrow keys, select the method you want to override and press Enter. The method signature will then be created, and a call to the base class implementation will be added to the method body.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net