Implementing Interface Members Implicitly


Implementing Interface Members Implicitly

Classes implement interfaces. To implement an interface means to provide code for each one of the methods defined in the interface. The compiler forces you to add code for every single method in the interface. This is because a developer using the interface expects that the class that implements the interface will have every method in the interface defined. There are two mechanisms for implementing interfaces in a class: an implicit mechanism and an explicit mechanism. I will show you the implicit way first.

To implement an interface implicitly:

  1. After the name of the class that will implement the interface type a colon followed by the name of the interface you wish to implement.

  2. Add a member for each member in the interface.

  3. Mark the implementation method public.

  4. Provide code for the member ( Figure 8.8 ).

    Figure 8.8 Implementing interfaces is a lot like inheriting from a class. With the implicit method all you have to do is implement the members of the interface as public methods.
     public interface IHuman {    string Name { get; }   void Sleep(short hours); } public class Person :  IHuman  {  public string Name  {      get      {         return "Paul";      }    }  public void Sleep(short hours)  {      for (short counter=0;      counter < hours; counter++)      {         HttpContext context =         HttpContext.Current;         context.Response.Write("Snore!");      }    } } 

graphics/tick.gif Tips

  • This mechanism of implementing the interface has the side effect that every implementation function must be marked public.

  • If you wish to implement more than one interface, separate each interface name with a comma ( Figure 8.9 ).

    Figure 8.9 The Person implements both IHuman and IManager. Implementing the interface establishes an "is a" relationship. In other words, this Person is a Human, and a Manager.
     public interface IHuman {    string Name { get; }    void Sleep(short hours); } public interface IManager {    void SpyOnEmployee(string name,    bool IsUnaware); } public class Person  : IHuman, IManager  {  public string Name  {      get      {         return "Paul";      }    }  public void Sleep(short hours)  {      for (short counter=0;      counter < hours; counter++)      {         HttpContext context =         HttpContext.Current;         context.Response.Write("Snore!");      }    }  public void SpyOnEmployee(string name,   bool IsUnaware)  {      while (IsUnaware)      {         LookOverShoulder(name);      }    } } 
  • If your class derives from another class (not an interface) you list the non-interface class first, and then the interfaces you are implementing, all separated by commas ( Figure 8.10 ).

    Figure 8.10 This code defines a class called Person that implements IHuman. Then, since not every person is a manager, it defines a second class just for Managers. However, since all Managers are Persons it inherits all of its functionality from Person (code inheritance) then implements the IManager interface.
     public interface IHuman {    string Name { get; }    void Sleep(short hours); } public interface IManager {    void SpyOnEmployee(string name,    bool IsUnaware); } public class Person : IHuman {    public string Name    {      get      {         return "Paul";      }    }    public void Sleep(short hours)    {      for (short counter=0;      counter < hours; counter++)      {         HttpContext context =         HttpContext.Current;         context.Response.Write("Snore!");      }    } } public class Manager :  Person, IManager  {  public void SpyOnEmployee(string name,   bool IsUnaware)  {      while (IsUnaware)      {         LookOverShoulder(name);      }    } } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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