Getting Started With Interfaces In .NET

Interface
 
Interface in a simple term -- 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 the .NET interface, it is nothing but a pure abstract class. It may contain the 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 for 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 classes 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. The same thing can be done with the Animal class and inherit Dog class from it.
 
But we cannot do this type of inheritance (multiple inheritances) in C#. Dog and Parrot both classes are inheriting more than one class.
 
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 the 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 an Organism class:
 
code
 
This is the Animalia interface:
 
code
 
This is the 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 the 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 situations where we have the same method in two interfaces and a class is implementing both interfaces. This causes ambiguity in our code. For example, in the 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 a compile-time error that the Dog class already defines a member called ‘Movement’ with the same parameter types. Let’s have a look at the following code.
 
This is the 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 give us the following error:
 
error
 
To prevent this we can implement an interface explicitly using the name of the interface in the function signatures in the derived class as follows:
 
code
 
One important point here is that we cannot use a public modifier with explicitly 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 here the 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 structures called Student and Teacher and we want to use the code defined in another 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 the capital ‘I’ with the name of the interface.
  • All the methods in an 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 the interface.
  • The interface cannot inherit from structures and classes but they can implement other interfaces.
  • We cannot define fields, constructors,s or destructors inside the interface.
  • Interfaces cannot be instantiated but they can hold reference of all the types that implement them.
  • Defining the methods in the derived types must be public. But when we explicitly implement an interface in a type then we can’t specify public access modifier in the method’s header.
  • A class implementing interface can specify any method of an interface as virtual and then can override by the derived class.
  • If a class or structure implements an interface then it has to implement each and every method defined in an 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 has 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 an alternative approach to multiple inheritances 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:


Similar Articles