An interface defines a contract. Classes can Implement from multiple Interfaces. Although the syntax looks like it's "Inheriting", it's not the case.

  • https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface
  • https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/

Contract Analogy

If I hire you for a job, we agree that you get $200 when you complete it. In the end, what do you expect? Obviously $200 but how do you expect the money? The contract didn't specify the details of the $200! You might be expecting cash but I instead decide to write you a check. You can turn it into cash by taking it to the bank and cashing it. I can also decide to give you a prepaid credit card loaded with $200. Still works for you! You can slide that card anywhere to use as currency. There's a number of other methods to get you the $200:

The point is that the contract doesn't have to spell out the "implementation" or the details.

Interfaces work the same way. The only requirements is that you provide:

  • Method Name
  • Parameters
  • Return type

The details of how it works don't matter to the calling object. If it mattered then those classes are very tightly coupled and would be difficult to replace in the future. Keeping them decoupled provides flexibility to change out the implementation.

In the next video we'll see a demo of how this works.