Interface class in C#.NET
Whenever I tried to learn about what is Interface class the only answer I found was - Interface class is the contract between the parent and its child class where the parent is the interface class and its child class makes a promise to its parent that it will implement all of interface class features in the explanation. That is something like
interface class IClass
{
public void SomeMethod();
}
public class MyClass : IClass
{
public void SomeMethod()
{
//Detail Implementation of the method
}
public void SomeOtherMethod()
{
//Detail Implementation of the method
}
}
In the above example, the first character I in the name of the Interface class is the naming convention or let's say a rule that the interface class name should start with I.
The above example is an explanation of what we said in the initial definition of the interface class - and we also have an extra method name SomeOtherMethod() in MyClass that is to tell you guys that we can implement some extra methods as well in the child class but it is necessary to define Interface class methods in the child class when you are implementing the interface or It will give you an error.
In Short:
"We declare the function in the Interface class then implement that interface on the actual class get an error and then provide the explanation of the function in the actual class to avoid that error".
Here are a few questions ๐ :
Q: Can't we directly implement the method into the actual class without the involvement of the interface class?
Ans: Yes we can, and this is what we do all the time - but there are some special scenarios where we involve the interface class.
Q: Why do we use interface class?
Ans: I think I should clear it up with an example first -- Let's say there is the rule of thumb no matter what kind of machine you have it should have a button to start it and the user should be unaware of all the details of a button that how it works internally so here we will create an interface IMachine with the method StartMachine() now whenever we will create something which have a machine we will like to impose this rule on that thing although that thing can have a lot of different features, having a StartMachine() system is important. To implement this feature we will implement this interface class on that machine class. Now look I am going to create two new things a Car and a WaterPump both have a machine, internally they work in different ways but both have the option to start a machine so How Interface will implement it and how things will become easy look at the example ๐
interface IMachine{
public void StartMachine();
}
public class Car : IMachine
{
public void StartMachine()
{
StartCarEngine();
}
public void StartCarEngine()
{
Console.WriteLine("This Will start the car");
}
}
public class WaterPump: IMachine
{
public void StartMachine()
{
PlugMotorIntoSwitch();
}
public void PlugMotorIntoSwitch()
{
Console.WriteLine("This Will start the Waterpump");
}
}
//-------------------------------------------------------------------------
public class Program
{
public static void Main()
{
IMachine myMachine = new Car();
myMachine.StartMachine();
//OR
IMachine myOtherMachine = new WaterPump();
myOtherMachine.StartMachine();
}
}
Code above has an IMachine interface, a Car class and a WaterPump class. The Car and WaterPump classes are implementing the interface so these classes must define the StartMachine() method and they have other methods as well we don't care about those for now.
Look how interface class helped us in the Main() method we are simply putting the objects of WaterPump and Car class into the Interface class variable and calling the same function for both classes through Interface without knowing about how car and waterpump start internally - The point is we dont need to learn different ways
StartCarEngine()
OR
PlugMotorIntoSwitch()
to Start the machine of the car and waterpump we simply use our interface to Start machine and interface is taking care of rest of the things we are simply calling StartMachine() for both car and waterpump although the method of getting start is different.
Subscribe to my newsletter
Read articles from Hammad Maqbool directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hammad Maqbool
Hammad Maqbool
Focused and enthusiastic Software Engineer with a keen interest in Software, Web Application Development and Teaching Programming. I am a passionate individual, Specialize in thinking outside the box to find unique solutions to difficult engineering problems.