Introduction

In this lesson, you will learn how to create and implement your own interfaces with a concrete example and you will get an idea of why interfaces are useful especially when our classes don’t have a relationship with each other and therefore they don’t share a base class.

Example

Imagine that we are working on a video game where the player can destroy stuff, But everything we destroy will have different behavior depending on what is destroyed.

for example, destroying a Chair will play some destruction sounds and will spawn destroyed chair parts, but destroying a car will play an explosion sound, create fire and destroy other objects nearby.

For our game we have the following classes:

  • Chair that inherits from class Furniture
  • Car that inherits from class vehicle

Now what is the best approach to add this destruction feature?

If we think about it, we can’t add a class to make our classes inherit because C# is a single inheritance language and not multi-inheritance. We already inherited from one class.

Plus, there is no concrete relationship between a car and a chair to share one base class anyways.

If we implement normal methods called Destroy in each class, it will be hard to maintain. What if we decide to change the destruction system entirely or add visual effects to our destructible objects? It will be a pain actually to go through each class and modify it.

What we need is a way to enforce our destructible behavior on every class that uses it just like a contract :)

The best approach, in this case, is to use an Interface we will call it IDestroyable, and any class that implements this interface will be forced to follow our destruction requirements and customize it at the same time depending on the class itself.

This interface will define two things

  • A property called DestructionSound which will store the audio file for the destruction sound
  • A method called Destroy which each class will implement differently

Now let’s make our classes implement this interface and implement the interface’s content

This way, if we decide to add a property or a method to our interface, for example: Delete destroyed objects, after some time, all classes that implement this interface will be forced to follow the new requirements since now they are part of the interface’s contract :).

Now let’s go to our main method and create few objects of each class and use try the new destruction feature

Important note: Interfaces are used for Communication between 2 similar/nonsimilar classes, which do not care about the type of the class implementing the Interface, just like how our car communicated with the chair to call the Destroy method, all because they implemented the same interface.

While interfaces might seems hard to understand at first glance, they provide us with great perks, for example:

  • Code readability: An interface constitutes a declaration about intentions. It defines the capability of your class, what your class is capable of doing. If you implement ISortable, you’re clearly stating that objects of your class can be sorted.
  • Code semantics: By providing interfaces and implementing them, you’re actively separating concepts. An interface defines a behavioral model, a definition of what an object can do. Separating those concepts keeps the semantics of your code more clear.
  • Code maintainability: Interfaces help reduce coupling and allow you to easily interchange implementations for the same concept without the underlying code being affected.
  • Design Patterns: It’s the bigger picture of using contracts, abstraction, and interfaces pivotal for OOP, human understanding, and complex system architectures.
  • Multiple inheritance: complex Using interfaces can be our gateway to use multiple inheritances in C#

Outro

Suppose you write a library and want it to be modifiable by users. You write the interface and its class implementation. Other developers who will use your library can still write their own implementation class, which may use different technology/algorithms that achieve the same result. This is also why we meet so many interfaces in libraries we use but rarely feel the need to write our own interfaces because we don’t write libraries.

On a final note, the important thing is to understand how interfaces are implemented at this point of the course. As we go through different technologies like WPF and ASP.Net, we will start using interfaces more, which will help us better understand them in the future.

Complete Code