In the previous part we particularly discussed about the difference between Abstract classes and an Interface. Also what kind of properties makes them distinct and where are they used.

So now in this part we discuss more about the interfaces and about its types which are very much important to understand.

Interface:

Now as we know the definition of interface, this says that it is a contract between the two or many program codes. And no program code can violate the contract as there are some guidelines which they need to follow.

So each and every interface is created with one goal which is to make sure that the two interfaces or program code unanimously follow the rules or guidelines for the better working of the application. But sometimes what happens is that there are multiple parent classes and their respective child classes and due to that multiple inheritances occur. To achieve that we use interfaces.

But then what happens if more than one interface is present in a program code, how we will know that which interface is working first and which one is working second? How to know that if there is a conflict between these interfaces and how it will be resolved?

To answer these questions we have to use types of interfaces which will work under their own condition to resolve the conflict arising due to multiple interfaces.

There are mainly two types of interfaces defined according to their functions, they are:

  • Implicit Interface
  • Explicit Interface

Implicit Interface:

This type of interface we can say that it is by default applied or implemented in our program code. Suppose we have two interfaces with their own methods and then we have a normal class. So the normal class cannot access any of the methods of the two interfaces.

For example we have two interfaces such as Interface 1 and interface 2. Also Interface 1 contains Add method and Interface 2 contains Delete method. So now we have a normal class named Maths, and this class cannot access the Add & Delete method because it is not binding with any of the interfaces.

Here we do not have to implement this interface because while creating the class and the interfaces it is by default implied. We understand this by a simple code,

Interface IAdd;
{
      return Add ();
}
Interface IDelete;
{
      return Delete();
}

public class Maths
{
    void ();
}

Explicit Interface:

This type of interface, we can say that they have to be implied manually and are not implemented by default. This type of interfaces are used when we want to make sure that our class looks through or we can say access only the methods of the particular interface when implied. We can also say that to access the methods of particular interface we have to explicitly implement that interface.

We can take a general example to understand this concept.

Suppose there is an Air Conditioner, now a normal person when looks at the AC he will see the buttons on it and instructions about the AC. On the other hand when a technician looks at the AC he will see the mechanism of the AC and the internal parts which needs to be repaired. So here the normal person sees the AC according to his own perspective and the technician sees it according to his own perspective. Both have their own exclusive way of seeing the AC.

So in similar way if there are two interfaces in a program code, we have to exclusively implement the particular interface so that the normal class can access the methods of that interface. It is like the two interfaces, Interface1 & Interface2 have their own methods like Add & Delete. So we can assume that these interfaces are like binoculars, when a normal class has to access the Add method it has to look through the binoculars of Interface1 and if it has to access Delete method it has to look through the binoculars of Interface2.

In this way if there are multiple interfaces in a program code they can be implemented through explicit interface type.

We take a simple sample code to understand this,

Interface IAdd;
{
     return Add ();
}
Interface IDelete;
{
       return Delete();
}
public class Maths : IAdd
{
    void ();
}

Here in public class Maths : Interface, we have to implement the explicit interface so that the conflict is resolved as in which methods to access. We see a screen shot of how explicit interface is implemented, from where do we select the option of interface implementation.

1

Here when we inherit the normal class with an interface and right click on the interface we can see two options. First is the implement interface option which we can also say as implicit interface type. Second is the explicit interface type option which we can see in red underline. We can select any of the option to implement multiple interfaces.

Advertisements