18 - Interfaces
In a previous tutorial on Inheritance, we learned how to extend a base classes functionality in a derived class. Extending functionality works good when you are creating code, but what if you want to ensure that the classes that are deriving from a base class are implementing methods from your base class? This is where interfaces come in. Interfaces enforce classes that derive from it to implement each method, events, indexer, and/or properties that the interface holds. You may be asking why you would want to do that. The reasoning goes back to the idea that you will be working with others on a project, or you are creating an architecture before implementing a product. Interfaces show explicitly what methods, event, indexers, and/or properties must have implementations in a derived class. They allow for architecture to be edited at a higher level which in turn supports better programming down the line. In this tutorial we will look at how to define an interface, implement an interface in a derived class, and finally how to inherit multiple interfaces in a single derived class.
The definition of an interface does not contain any implementations; it is simply a blueprint in which a derived class should follow. Common practice is to put a capital I before the name of an interface to denote that it is an interface.
Notice that there is no access modifier before the interface keyword or any methods in the interface. Interfaces do not require an access modifier to use them, however, they can have the protected and internal modifier to limit the access. Another thing to notice is that there is no definition for the method in the interface, only a signature. This is how methods within an interface will be written.
Implementing an interface looks similar to how you extend base class from a derived class. You will utilize a semicolon and then the interface name. Some programming languages such as Java use the Implements keyword to use an interface. Remember that every method inside of a interface that is being implemented by a class must be defined inside of the implementing class.
Another thing to note is that you can add access modifiers to methods inside of the implementing class. In our example, the MyInterfaceMethod is now public and can be access to anyone with access to an instance of the implemented class.
Interfaces can inherit other interfaces too. Classes that implement an interface with multiple inherited interfaces must implement every interface that the implemented interface inherits as well as the interface.
Another feature of interfaces is that class can implement multiple different interfaces. Just like interface inheritance, classes that implement multiple interfaces must implement each method from the implemented interfaces. A comma is used to separate each additional interface that is being implemented.
In this tutorial we learned what interfaces are and how to use them to design code. We learned how to define and implement an interface as well as multiple interfaces which may have inherited other interfaces. After this tutorial you should be able to use interfaces to create blueprints from your classes.