Interfaces are types that enable you to define multiple classes that have similar functionality. Let's suppose you had a class named Escort. The Escort class has methods like Run, Stop, PlayMusic, etc. You're happy with the Escort class because it enables you to come to and from work. At some point, however, your friends begin to make fun of your Escort class, and you would like to replace the Escort class with the Ferrari class. Before you replace the Escort class with the Ferrari class, you want to make sure that the Ferrari class at least has the same functionality as the Escort class. It should Run, Stop, and PlayMusic. It may do all those things differently than the Escort class, but the point is that it at least has to have the same methods. The combination of the methods is the interface. The interface may be called ICar for example. All cars have to implement the methods of the ICar interface in order to be considered cars (in my opinion). Our world is based on the principle of interfaces. People, for example, implement similar interfaces. A lot of us breathe and eat. (If you don't agree, please stop reading this book immediately.) Every instance of the human class may have a different way of breathing and eating , but companies like McDonald's rely on the IHuman interface having an Eat method in order to succeed. In C# terms, interfaces are types. They're different from interface definitions in C++ for example in that they're not specialized classes; they're actually a separate type altogether. In its basic definition, an interface is a set of function definitions (just the definition without any implementation code). By themselves these functions do nothing. They're like having an IHuman interface without an instance of a human ”it's just a concept and not the actual implementation. The interface must be implemented in a class to be useful. When a class implements an interface it advertises to the world that it supports all the methods defined in the interface. If a used-car salesman tells you that his cars implement the ICar interface, then he is saying that they at least Run, Stop, and PlayMusic. If they don't implement one of the methods of the interface, then the C# compiler would stop them. With interfaces a developer can write code to use the interface rather than the implementation of the interface. This enables starting with one implementation, then substituting it at a later time without rewriting all the code. This is true for the ICar interface, for example. Drivers don't learn how to drive just one car; they learn to drive all cars because cars all follow the same basic interface. For a real-world application, consider a technology like ADO.NET. ADO.NET enables you to connect to a database. Microsoft, believe it or not, doesn't want to force you into a single database product. Rather than making everyone use one set of classes, they define an interface of functions for connecting to the database. Database vendors then write classes that implement those interfaces. The idea is that if you code using the interfaces, then you can change what database package you use without having to rewrite your application. ![]() |