Getting Started With Interfaces In .NET

Interface

Interface in a simple term -- it is a medium of communication among people. In computing it can be a GUI interface or CLI interface to interact with the computer.

In the world of .NET interface it is nothing but a pure abstract class. It may contain only declaration of:

  • Methods
  • Properties
  • Events
  • Indexers
It is the responsibility of the derived types to define all the abstract members declared in the interface.

Why we need interfaces

To understand the need of interfaces let’s do an example. Suppose we have a Dog class and a Parrot class. Both can grow, move, eat food, and have some other characteristics. Now instead of writing all the code separately in both classes what we can do declare a class Organism and put all the common characteristics in that class and inherit both class from this class because Dog and Parrot are living organisms and have all the characteristics of this class.

Now we want to add some other characteristics of Dog and Parrot:  that they are multicellular and warm blooded. Because all the organisms are not multicellular and warm blooded we cannot add these characteristics in the Organism class. So we can declare a new class Animalia and put all the characteristics in this class, and inherit both Dog and Parrot classes from Animalia class.

Now what if we want to add some special characteristics of both Dog and Parrot classes? For example, Dog can run but Parrot can fly. Dogs are heterotrophs and Birds are vertebrates. We may add these characteristics in both classes. But in the future we may want to create another Bird class, maybe Sparrow and write all these characteristics again. This leads to code duplication.

The best approach is that we declare a new class called Bird and put all the characteristics in this class and inherit all the birds from this class. Same thing can be done with Animal class and inherit Dog class from it.
  
But we cannot do this type of inheritance (multiple inheritance) in C#. Dog and Parrot both classes are inheriting more than one classes.

This can be viewed in the following diagram:

diagram

To overcome this problem we have Interfaces. We can define Animalia, Animal and Bird as interfaces. And since both Animal and Birds are multicellular and warm-blooded, we can implement Animalia interface in both Animal and Bird interfaces and then implement Animal interface in Dog class and inherit it from Organism class. And implement Bird interface in Parrot class and also inherit it from Organism class.

Above classification can be view in the following diagram:

diagram

Let’s do some code to understand in more detail.

This is Organism class:

code

This is Animalia interface:

code

This is Animal interface implementing Animalia interface:

code

This is Bird interface implementing Animalia interface:

code

This is Dog class inheriting Organism class and implementing Animal interfaces:

code

This is Parrot class inheriting Organism class and implementing Bird interfaces:

code

This is Program class instantiating Dog and Parrot classes:

code

When compile and run this will produce following output:

output

How we implement interfaces


Interfaces are implemented in two ways: 
  • Implicit implementation
  • Explicit implementation

Above example is of implicit implementation. Explicit implementation is used in situation where we have same method in two interfaces and a class is implementing both interfaces.This causes ambiguity in our code. For example in Organism interface we have a movement method that all organism can move and in Animal interface we have another specialized movement method that all animals can move by running or walking. A Dog class implementing these two interfaces will not compile and gives compile time error that Dog class already defines a member called ‘Movement’ with same parameter types. Let’s have a look in the following code.

This is Organism interface:

code

This is Animal interface:

code

This is Dog class implementing both Organism and Animal interfaces:

code

This is our main class Program instantiating Dog class:

code

If we try to run this code, it will gives us following error:

error

To prevent this we can implement a interface explicitly using the name of interface in the function signatures in derived class as follows:

code

One important point here is that we cannot use a public modifier with explicit defined functions. Because they are now not directly visible to Dog class they are part of interfaces in which they are defined. And now we will call them by referencing through the appropriate interface. And does it make sense that if both methods will be public and we try to access them then which method will be invoked? And herethe compiler will be confused and give us an error. So in our main method we will call these methods as follows:

code

And it will produce the following output calling both movement functions without any ambiguity.

output

Another use of interfaces is in structures. Suppose we have a situation where we have two structure called Student and Teacher and we want to use the code defined in other structure called Person. But we cannot inherit Student structure from Person structure. Since inheritance is not supported in Structures. Here interface comes in handy. We can define Person as an interface and implement it in both Student and Teacher structures.

Use of interfaces is very simple and easy but there are some key points that we must put into consideration when using interfaces:

  • It is recommended that we use capital ‘I’ with the name of interface.
  • All the methods in interface are public and abstract by default. We cannot use other access modifiers like private, protected etc.
  • We cannot use nested types like enumerations, structures and classes inside interface.
  • Interface cannot inherit from structures and classes but they can implement other interfaces.
  • We cannot define fields, constructor or destructor inside interface.
  • Interfaces cannot instantiated but they can hold reference of all the types that implements them.
  • Defining the methods in the derived types they must be public. But when we explicit implement an interface in a type then we can’t specify public access modifier in method’s header.
  • A class implementing interface can specify any method of interface as virtual and then can override by the derived class.
  • If a class or structure implement an interface then it has to implement each and every method defined in interface. So we should keep our interface general purpose that other classes can implement.
  • Also make sure the interface should not contain too many methods because the class implementing that interface have to implement all the methods.
When to use Interface
  • Interfaces can be used to provide common functionality in the classes or structures that are not related to each other.
  • Interfaces are alternative approach of multiple inheritance in .NET.
  • Interfaces are used to group objects, based on their common behaviors.
  • Interfaces are used to provide polymorphic behaviors to classes because they can implement more than one interface.
Read more articles on .NET:

Up Next
    Ebook Download
    View all
    Learn
    View all