Implementing Multiple Inheritance via Interfaces






Implementing Multiple Inheritance via Interfaces

As Listing 7.3 demonstrated, a single class can implement any number of interfaces in addition to deriving from a single class. This feature provides a possible enhancement to work around the lack of multiple inheritance support in C# classes. The process uses aggregation as described in the previous chapter, but you can vary the structure slightly by adding an interface to the mix, as shown in Listing 7.10.

Working around Single Inheritance Using Aggregation with Interfaces

public class PdaItem
{
  // ...
  protected Guid ObjectKey
  {
      get { return _ObjectKey; }
      set { _ObjectKey = value; }
  }
  private Guid _ObjectKey;

  public virtual string Name
  {
      get { return _Name; }
      set { _Name = value; }

  }
  private string _Name;

  public string DateTimeLastUpdate
  {
      get { return _DateTimeLastUpdate; }
      set { _DateTimeLastUpdate = value; }
  }
  private string _DateTimeLastUpdate;
}

interface IPerson
{
    string FirstName
    {
        get;
        set;
    }

    string LastName
    {
        get;
        set;
    }
}

public class Person : IPerson
{
  // ...
  public string FirstName
  {
      get { return _FirstName; }
      set { _FirstName = value; }
  }
  private string _FirstName;

  public string LastName
  {
      get { return _LastName; }
      set { _LastName = value; }
  }
  private string _LastName;
}

public class Contact : PdaItem, IPerson
{
  private Person Person
  {
      get { return _Person; }                        
      set { _Person = value; }                       

  }
private Person _Person;


  public string FirstName
  {
      get { return _Person.FirstName; }                    
      set { _Person.FirstName = value }                    
  }

  public string LastName
  {
      get { return _Person.LastName; }
      set { _Person.LastName = value; }
  }

  // ...
  public string Address
  {
      get { return _Address; }
      set { _Address = value; }
  }
  private string _Address;

  public string Phone
  {
      get { return _Phone; }
      set { _Phone = value; }
  }
  private string _Phone;
}

IPerson ensures that the signatures between the Person members and the same members duplicated onto Contact are consistent. The implementation is still not synonymous with multiple inheritance, however, because new members added to Person will not be added to Contact.

Beginner Topic: Interface Diagramming

Interfaces in a UML-like figure take two possible forms. First, you can show the interface as though it is an inheritance relationship similar to a class inheritance, as demonstrated in Figure between IPerson and IContact. Alternatively, you can show the interface using a small circle, often referred to as a lollipop, exemplified by IPerson and IContact in Figure.

1. Working around Single Inheritances with Aggregation and Interfaces


In Figure, Contact derives from PdaItem and implements IContact. In addition, it aggregates the Person class, which implements IPerson. Although the Visual Studio 2005 Class Designer does not support this, interfaces are sometimes shown as using a derivation-type arrow to a class. For example, Person could have an arrow to IPerson instead of a lollipop.




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows