Interfaces give you the ability to define behavior without actually implementing it. Why would you want to do that? Your classes can then declare support for interfaces which means that they promise to support that behavior. How your classes do this is up to them. Declaring support for an interface signals that they do this. You can have what might be completely unrelated classes all supporting the same interface which then allows you to work with them in the same way.
Some common interfaces in C# are:
Listen to the full episode or you can also read the full transcript below.
I many ways, an interface is really nothing more than a abstract base class that declares only pure virtual methods. In fact, in C++, this is exactly how you implement an interface. Just declare a class, give it some virtual methods, make them all abstract or pure virtual methods, and you have an interface.
Other languages such as C# have a special way of declaring an interface with some special rules about what can go into an interface. This is really just to avoid calling this practice multiple inheritance.
You can then declare a class that implements as many interfaces as you want. In C++, because an interface is nothing more than any other class, you use multiple inheritance to implement many different interfaces. Other languages expose this capability as if it was multiple inheritance. And it sure looks a lot like multiple inheritance. But since C# insists that it does not support multiple inheritance, well, we’ll just smile and let it call that duck a cat. C# has some other really cool abilities that make up for this.
One widely used technology based almost entirely on interfaces is COM which stands for the component object model. COM is notoriously difficult to use but has a very unique feature. The most common interface in COM is called IUnknown. The letter I in IUnknown stands for interface. This interface has a method that lets you ask for another interface. If the object that you’re working with supports this other interface, then it’ll return it to you. Otherwise, you’ll get an error. This allows you to ask an object if it supports an interface that you’d like to use and if it responds with the requested interface, then you can call any of the methods defined for that interface.
The interfaces in COM are well documented and identified with well known identities. This technology has existed in the Windows operating system for more than 20 years. You can program with COM interfaces with the C language or the C++ language. You can also use C# with a bit of extra work. COM really has nothing to do with a specific language. It’s more of a binary standard for how components can relate to each other.
For languages such as C++ and C#, they have the concept of interfaces builtin. Well, for C++, an interface is nothing more than an abstract class with all pure virtual methods. You should never have to guess if a class implements some interface. It’ll be right there in the list of classes or interfaces that the class implements. It’s also important to note that you can’t just instantiate an interface itself. Interfaces are abstract and must be implemented by a class before they can be used.
So if you have a class in C# called gameSettings that’s designed to open a database to read and update various options that the user can change, then you’ll probably want this class to implement IDisposable. The IDisposable interface defines a method called Dispose that the gameSettings class will implement to close the database. Now you could have your gameSettings class declare and implement a method called Dispose that does the same thing. And you could do this without implementing the IDisposable interface. So you don’t need to implement the interface in order to have methods in your class that the interface also declares. Why bother declaring that your class implements IDisposable then? Because this adds a deeper meaning to your class. By implementing the IDisposable interface, you’re declaring to the rest of your code that it should pay attention to the Dispose method. It’s not just any method called Dispose. It’s a method called Dispose that’s there to fulfill your promise that your class implements IDisposable. Interfaces act like a contract where your class promises to uphold the meaning and behavior defined by the interface.
This makes it easy for automated testing and analysis software to examine your code to see if you’re using your classes properly. If your gameSettings class implements IDisposable, then the automated analysis software has enough information to know that you need to be calling Dispose.
It also give you extra clues about how to use a class. Interface names are often well known and follow guidelines to make them similar and easy to understand. By becoming familiar with various interfaces and their intended purpose, you can then look at some new class you’ve never seen before and just from the list of interfaces that it implements, you can get a good idea of how to use the class.