2.11 Abstract Classes and Interfaces

 <  Day Day Up  >  

You want to create a set of methods and properties that a derived class must implement.


Technique

To enforce the requirement that only some methods or properties need to be implemented, add the abstract keyword to the class and each method and property that must be implemented as shown in the VolumeLevel property in Listing 2.9.

Listing 2.9 Abstract Classes Requiring Implementation public abstract class MediaDevice
 {     bool on = false;     virtual public void TurnOn()     {         on = true;     }     virtual public void TurnOff()     {         on = false;     }     public abstract int VolumeLevel     {         get;         set;     } } 

If all methods and properties need to be implemented by a derived class, design and create an interface declaration. An interface declaration is similar to a class declaration, but instead of using the class keyword, use the interface keyword as shown in Listing 2.10. Furthermore, methods and properties cannot use accessibility modifiers and cannot contain a definition.

Listing 2.10 Interfaces Are Purely Abstract
 public interface IMediaDevice {     void TurnOn();     void TurnOff();     int VolumeLevel     {         get;         set;     } } 

Comments

When you derive from a base class, you are not required to override any of the virtual methods of the base class. A derived class can expand on the functionality of a base class while still defaulting to the base-class implementation of virtual methods and properties. When designing a base class, you might need to create a requirement that derived classes must contain an definition of some or all of the methods or properties of a base class. Depending on the level of requirement, you can do so using an abstract base class or using an interface.

An abstract class is a class that contains some methods or properties that must be implemented by a derived class. In Listing 2.9, you can see that the MediaDevice class is an abstract class. It contains an implementation for the two methods TurnOn and TurnOff . A class that derives from this class is not required to override these methods. However, the class contains an abstract property named VolumeLevel . Because this property is abstract, there is no definition for it in the base class, and any derived class must implement it.

One thing to understand when creating an abstract base class is that it cannot be instantiated itself. In other words, you cannot use the new keyword to create an object of that type. Because an abstract method does not contain a definition, attempting to call that method would undoubtedly have undesirable results. It is still possible to create a reference to an abstract base class by instantiating a derived class and casting that object to the same type as the base class. Calling the abstract method on this base class, however, will result in a call to the method implementation of the derived class due to polymorphism.

 
 MediaDevice md = (MediaDevice) new Radio(); md.VolumeLevel = 2; 

You use interfaces when you require that all methods and properties be overridden. You can almost think of an interface as a contract. If a class derives from an interface, it must honor the contract and implement all methods and properties. One area where you will see interfaces pervasively within the .NET Framework is the collection class. A class that wants to act as a container or collection of objects must support the required collection interfaces. This requirement allows someone unfamiliar with the actual class itself to still access it using the known collection-interface methods.

Another area where interfaces play a major role is in various plug-ins for different technologies. If you want to create an application that allows plug-ins to extend the behavior of your application, you can require that anyone wanting to create a plug-in must implement certain interfaces. You can then access those plug-ins using an API that you know about, regardless of the implementation details.

There are two ways to implement an interface with a class. The first is when you initially create the class using the Add Class dialog. As shown in Figure 2.2, you can choose to have your class implement an interface by selecting the Inheritance tab within the dialog and selecting the interface you want to inherit. Unfortunately, using this method to implement an interface can be cumbersome because the generated code does not contain stubs of the methods you must override. Immediately attempting to compile your code after creating a class will result in an error for each method or property that is not implemented from the interface.

Figure 2.2. Select the Inheritance tab within the Add Class dialog to implement an interface.

graphics/02fig02.jpg

You might find it easier to simply use the source code window after you create your class to implement an interface. The reason is that IntelliSense is able to create the necessary method and property stubs that you must implement. This feature can be a real time-saver for large interfaces. Figure 2.3 shows the result of using the source code window to implement an interface, and Listing 2.11 shows the result of the generated code.

Listing 2.11 Stubs Can Be Generated Automatically
 using System; namespace _11_Interfaces {     public class Television : IMediaDevice     {         #region IMediaDevice Members         public void TurnOn()         {             // TODO:  Add Television.TurnOn implementation         }         public void TurnOff()         {             // TODO:  Add Television.TurnOff implementation         }         public int VolumeLevel         {             get             {                 // TODO:  Add Television.VolumeLevel getter implementation                 return 0;             }             set             {                 // TODO:  Add Television.VolumeLevel setter implementation             }         }         #endregion     } } 
Figure 2.3. Using the source code window to implement an interface allows you to easily create the method and property signatures you must implement for an interface.

graphics/02fig03.jpg

 <  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