[C#] Interfaces

No doubt that you have heard of “Interface-based Programming”. In OOP, polymorphism is one of the key features but inheritance-based programming has its limits.

Interfaces help you define the prototype of the functionality, implement the concrete logic in appropriate classes, and invoke the right implementation in a polymorphic way.

 

1. Interfaces vs Abstract Classes

An interfaces is a set of abstract methods and properties. It does not provide any implementation.

Now you might think that an interface is very similar with an abstract class. But remember! An abstract class is still a class. An abstract class can have non-abstract members, fields, and constructors, which an interface cannot have.

  • A class can support multiple behaviors (interfaces).
  • A class can derive from a single abstract class.
  • You can use the “is” and “as” keywords for interfaces just like classes.

Use your common sense. If the types satisfy the “Is-A” relationship, you can use the “inheritance“. Otherwise, use the “interface“.

 

2. Defining Interfaces

  • Prefix the name of an interfaces with a capital letter “I
  • Define method signatures; do not specify any access modifiers, all methods are public and abstract implicitly
  • Do not provide any implementation; Interfaces are protocols only
  • Constructs and Fields: NOT allowed
  • Properties are ALLOWED without implementing the get or set block; Do not specify any access modifiers either
  • Event definitions and INDEX definitions are ALLOWED
public interface IFlyable
{
  int CurrentHeight { get; set; }
  void Fly();
}

 

3. Implementing Interfaces

Interfaces are useless on their own. They should be implemented in a class.

  • Provide implementation for all methods in an interface
  • Provide access modifiers for methods and properties
  • Do not specify the “override” keyword
public class Airplane : IFlyable
{
  private int height;
  public int CurrentHeight
  {
    get { return height; }
    set { height = (value > 0) ? value : 0; }
  }
  public void Fly()
  {
    Console.WriteLine("Let's fly");
  }
}

 

4. Explicit Interface Implementation

Unlike the inheritance programming, multiple interfaces can be used for a single class. Therefore it is possible that different interfaces have the method with the same name. How can you distinguish which is which?

In the case of naming conflict you need to specify the name of Interface as the part of method declaration.

  • When using explicit implementation of an interface, the members are forced to private in the class itself. Therefore do not specify any access modifiers.
  • The reason of the forced private modifier is that the method cannot be called directly. It should only be called by the interface type.
public interface IHighJumpable
{
  void Fly();
}
public class Airplane : IFlyable, IHighJumpable
{
  ...
  void IFlyable.Fly()
  {
    Console.WriteLine("I'm flying");
  }
  void IHighJumpable.Fly()
  {
    Console.WriteLine("I'm Jumping");
  }
}

When you invoke interface members, the right version of a method is invoked automatically.

IFlyable transport1 = new Airplane();
transport1.Fly(); // I'm flying
IHighJumpable transport2 = transport1 as IHighJumpable;
transport2.Fly(); // I'm Jumping

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s