‘What is meant by implementing an interface?’
We shall be implementing the IPlane interface in this post.
What is meant by implementing an interface?
Perhaps it’s best if we start with what an interface is, and then work towards what implementing one means.
Example #1:
Every single McDonald’s in the world has certain commonalities. In fact, if you want to run a McDonald’s franchise, you must follow their franchisee rules:
- Must sell Big Macs.
- Restaurants must have a Big M
- Must pay franchisee fees.
- Restaurants must be clean (sic!?) at all times.
These are their rules. It’s in the contract. Every restaurant is bound to follow that contract. Each restaurant is slightly different, but they are the same when it comes to the above rules. This is also a very good thing for a customer. If you walk into any McDonald’s restaurant in the world, you know for certain that you can purchase a Big Mac or that it will be clean (sic!?)
What does McDonald’s have to do with interfaces?
An “interface” is nothing more than a contract. Any restaurant that “implements” or signs the contract, is bound to follow it. Or in other words, any restaurant that implements the McDonald’s franchisee interface, is bound to sell Big Macs, and have a Big M etc.
Why is it called IMcDonald’s interface instead of McDonald’s interface?
When ever you name an interface, it is common practice for the name to begin with an “I”.
Example #2
All planes in the world have certain commonalities, no matter what type. In other words, they all implement the Iplane interface.
The IPlane interface stipulates that any plane which implements it must have:
- Two wings
- an engine
- and it must fly
Therefore, if a Boeing 737 follows these rules, as a customer who purchases such a plane, you are guaranteed that your purchase will have wings and will fly. Here is an example of a Boeing 737 implementing the above interface:
public interface IPlane
{
void Fly();
void HasTWoWings();
void Engine();
}
class Boeing737 : IPlane // <-------------- the Boeing 737 implements the interface
{
// this means that the Boeing737 MUST have a fly, hastwowings and an engine method.
// the interface doesn't specify HOW the plane must fly. So long as it does fly
// the compiler doesn't care.
public void Fly()
{
Console.WriteLine("Come fly with me, let's fly, let's fly awaaaaaaaay");
}
public void HasTWoWings()
{
Console.WriteLine("I've got two wings");
}
public void Engine()
{
Console.WriteLine("BRrrrrrrrooooooooooooooooooooooooooooooooom!");
}
}
An Interface does not specify implementation
An interface does not give specifics about how something should be done or implemented. All it requires is that a method (or property, event or indexer) follow the contract stipulated. i.e. That a method is named according to the contract (e.g. Fly() or HasTwoWings(),), and takes the inputs (there are no parameters for those methods) and returns the outputs specified by the contract (the output specified is void – meaning there is nothing returned).
Example:
You want to Implement the Iplane interface? You can do so like this. First, let’s remind ourselves of what the Iplane interface looks like. And we needn’t use the interface on a Boeing plane. A duck (the one that quacks) can also “implement” the interface. We can specify how the duck should act when it has the methods that the interface prescribes.
class Duck : IPlane
{
// the duck class implements the IPlane interface
// this means tha the Duck class must have the following three methods:
// (1) Fly() - it must take no parameters and must return void.
// (2) HasTwoWings() - as above: it must take no parameters and must return void.
// (3) Engine() - as above: it must take no parameters and must return void.
// but the interface does not specify what should actually be inside the method.
// That is left to the programmer's complete discretion! When he or she does so
// that is called implementing the interface
public void Fly()
{
Console.WriteLine("https://www.youtube.com/watch?v=zf00mEe9EOs");
}
public void HasTwoWings()
{
Console.WriteLine("Fly by flapping two wings and not with a Rolls Royce engine!");
}
public void Engine()
{
Console.WriteLine("The duck's engine is it's energy reserves which it obtains from eating a diet of bread and marsh :)");
}
}
That is what an interface is, and what it means to implement an interface.
Hope it helps.