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,
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,
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.
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,
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,
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,
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.
First we are clicking option number one to Implement interface and then see what happens as shown in the below figure,
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,
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,
Now we are adding our project namespace in program main method as shown in below figure,
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.
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OPPs.Interface
- {
- interface Ivehicle
- {
- void Run();
- }
-
- class car : Ivehicle
- {
- public void Run()
- {
- Console.WriteLine("Run Car");
- }
- }
-
- class Bus : Ivehicle
- {
- public void Run()
- {
- Console.WriteLine("Run Bus");
- }
- }
- }
Main Method Code: Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OPPs.Interface;
-
- namespace OPPs
- { class Program
- {
- static void Main(string[] args)
- {
- Ivehicle vehicle = new car();
- vehicle.Run();
-
- vehicle = new Bus();
- vehicle.Run();
- }
- }
- }
Output
Now we are discussing the second part of the interface and explicitly implementing 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,
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,
Now we are ready to test the above code.
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,
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.
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,
Output
Full code of class: Ivehicle.class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OPPs.Interface
- {
- interface Ivehicle
- {
- void Run();
- }
-
- interface IToyVehicle
- {
- void Run();
- }
-
- class car : Ivehicle,IToyVehicle
- {
- void Ivehicle.Run()
- {
- Console.WriteLine("Run Car");
- }
-
- void IToyVehicle.Run()
- {
- Console.WriteLine("Run Toy Car");
- }
- }
- }
Main method Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OPPs.Interface;
-
- namespace OPPs
- { class Program
- {
- static void Main(string[] args)
- {
- car car = new car();
-
- Ivehicle vehicle = car;
- vehicle.Run();
-
- IToyVehicle Toyvehicle = car;
- Toyvehicle.Run();
- }
- }
- }
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.