Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "implementing an interface" exactly mean?

Last days I meet the term "implementing an interface" really often...I have an idea what is it about but I would like more information and some resources about it. When does a class implement an interface?

like image 925
rabidmachine9 Avatar asked Dec 12 '22 18:12

rabidmachine9


2 Answers

An interface is a contract that specifies a required set of methods that a class MUST create.

For example:

public interface ITest
{
   public void DoSomething(int someInt);
}

public class MyClass : ITest
{
   public void DoSomething(int someInt)
   {
      ... Do some stuff
   }
}

If you did not include the DoSomething method, the compiler would throw an error.

like image 152
Scottie Avatar answered Jan 03 '23 06:01

Scottie


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:

  1. Must sell Big Macs.
  2. Restaurants must have a Big M
  3. Must pay franchisee fees.
  4. 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.

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 follow it.

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:

  1. Two wings
  2. an engine
  3. and it must fly

Therefore, if a Boeing 737 follows these rules, as a customer, you a 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!");
    }
}

I hope that helps you folks.

So what's the use of this?

This becomes very handy if you're a passenger wanting to book a plane for example. You're not sure what planes will be available for you. And you don't exactly care, so long as they are planes, and so long as they fly, and have two wings and an engine. When you're at the airport, you might get a Boeing, or you might get an airbus, or you might get a Stig - but you know that no matter what actual plane you get, that they will do a certain job. They might fly in different ways, but they all fly.

This allows you to write your code in such a way so as to minimise maintenance costs and to make it significantly easier to modify without causing bugs.

like image 23
BenKoshy Avatar answered Jan 03 '23 07:01

BenKoshy