An Introduction To Interface

An interface is a very important part of the polymorphism. We know that in polymorphism architecture, it works with base class and child class. In this architecture, base class has some methods and their methods  are implemented by child class and they change their behavior at runtime. So we are creating an Abstract class or method. It means this abstract class is implemented by other child classes. In this base class just provide an abstraction not an execution. But partially in this provide some information details shown in the below Code sample,

Interface

But pure polymorphism avoids this type of information. So you can’t implement  an execution in a base class.

You can see here more detail: What is an abstract class?

That’s for providing Interface with Microsoft Csharp. Interface is a complete abstract environment, because it doesn't implement or execute a method, an event, a property.

So In a Csharp it provides an Interface. An interface is a pure polymorphism. An interface has a complete abstract environment. Therefore you cannot impalement or execute a method, event and property.

What is an Interface?

Interface is like as a class. We create a class the same way that we create an interface. An interface is on;y a type; it doesn't have a UI or design.

First we create a folder with suitable name (like interface) in our project solution explorer and Create Interface inside our newly created folder as shown below,

Interface

In the above figure, I am creating an interface and giving the name Ivehicle.cs. In this name first prefix capital letter as per polymorphism rules and always remember these rules for creating the interface.

Interface

In the above figure we created an Interface. In an Interface we can create a method, a property and an event as per our code requirement. But we cannot implement or execute creating a method, a property and an event.

We can create a method, a property and an event in an interface but not implement it. So we have raised a question, how are those implement edor executed? This type of implementation is possible through inheritance.

When we create any method or variable in an interface then we do not need to any access specifiers. Because all are by default public.

Now I am creating a method as shown in the below image,

Interface

See the above figure --  I am just creating a method not giving details. If you can try to give details then create an error as shown  in the below figure,

Interface

So we do not give any type of details in the interface, because interface is all pure abstract code.

Now we are creating a class shown in the below figure,

Interface

Here we are seeing how to create a car class normally inherited by Ivehicle. Now we are moving our cursor on Ivehicle and we are shown two options of interface as shown in the below figure.

Interface

First we are clicking option number one to Implement interface and then see what happens as shown in the below figure,

Interface

When we are clickig the first option we create base class Run() method implement as shown in the above figure. Here you are noting we are not using an override keyword. Keep in mind an Override keyword is used in two situations, when base method is defined with virtual or abstract keywords. Now write some details in car class run method as shown in below figure,

Interface

Now we are also using one more class, Bus, as similar class car and it also is inherited by Ivehicle.

Now we are moving to program main method as shown in the below figure,

Interface

Now we are adding our project namespace in program main method as shown in below figure,

Interface

In main method define a variable of Ivehicle as shown in below figure.

Note

We know that at compile time we cannot create an object of abstract class but in an abstract class object creates on runtime. But in an interface we cannot create an object on runtime and compile time, because interface is a pure polymorphism or pure abstraction.

Interface

In this above figure, we are creating an object car class and bus class because both are child class and run method is implemented in this child classes. When we run our code the first call car executes and the second time the called bus executes.

Ivehicle.cs Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace OPPs.Interface  
  8. {  
  9.      interface Ivehicle  
  10.      {  
  11.          void Run();     // Create Method  
  12.      }  
  13.      // Create class car   
  14.      class car : Ivehicle        // inherit class  
  15.      {  
  16.          public void Run()      // method implement   
  17.          {  
  18.              Console.WriteLine("Run Car");  
  19.          }  
  20.      }  
  21.      // Create class Bus   
  22.      class Bus : Ivehicle        // inherit class  
  23.      {  
  24.          public void Run()      // method implement   
  25.          {  
  26.              Console.WriteLine("Run Bus");  
  27.          }  
  28.      }  
  29. }   

Main Method Code: Program.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using OPPs.Interface;   // Add our project Namespace  
  7.   
  8. namespace OPPs  
  9. {     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Ivehicle vehicle = new car();    // create object using child class car   
  14.             vehicle.Run();  
  15.   
  16.             vehicle = new Bus();        // create object using child class car in change situation  
  17.             vehicle.Run();  
  18.         }  
  19.     }     
  20. }  

Output

Interface

Now we are discussing the second part of the interface and explicitly implementing interface.

Interface

Why do we need to explicitly implement interface?

We know Csharp doesn’t provide multiple class inheritances. But in this instance we use interface, because Csharp provides multiple interface inheritance.

Let’s move with the above example, we had already created one Interface, Ivehicle. Now create one more interface as shown in the below figure,

Interface

As shown in the above figure we created one more interface IToyVehicle with same Run method and show class car inherited by both interface without error. Now we can create an object car inside program main method as shown in below figure,

Interface

Now we are ready to test the above code.

Interface

Here output is the same.

We don’t need this type of output. But we need different type of output by both Run methods. So we are again modifying it shown in the below figure,

Interface

First we are removing Run method as shown in the above figure and click your cursor on  both interfaces one by one and open a box. Both boxes  explicitly display the implemented Interface but in the last word change.

Now you can click again one by one.

Interface

Now we see that we can create two different Run() methods; one is Ivehicle interface and second is IToyVehicle interface. This means both are different not the same method. It has a separate Run() method.

Now I can write some different message inside both Run() methods as shown in the below figure,

Interface

Output

Interface

Full code of class: Ivehicle.class

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace OPPs.Interface  
  8. {  
  9.      interface Ivehicle  
  10.      {  
  11.          void Run();     // Create Method  
  12.      }  
  13.   
  14.      interface IToyVehicle  
  15.      {  
  16.          void Run();   // create same method  
  17.      }  
  18.      // Create class car   
  19.      class car  : Ivehicle,IToyVehicle   
  20.      {  
  21.          void Ivehicle.Run()  
  22.          {  
  23.              Console.WriteLine("Run Car");  
  24.          }  
  25.   
  26.          void IToyVehicle.Run()  
  27.          {  
  28.              Console.WriteLine("Run Toy Car");  
  29.          }  
  30.      }  
  31. }  

Main method Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using OPPs.Interface;   // Add our project Namespace  
  7.   
  8. namespace OPPs  
  9. {     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             car car = new car();    // create car object  
  14.   
  15.             Ivehicle vehicle = car;  
  16.             vehicle.Run();  
  17.   
  18.             IToyVehicle Toyvehicle = car;  
  19.             Toyvehicle.Run();  
  20.         }  
  21.     }     
  22. }  

Finally we get a different output by explicitly implementing with created single object because in an explicit interface we only call this method or a property not any other.

I hope this article has helped you to understand interface and its importance. 

Have a nice day.


Similar Articles