The course is part of this learning path
C# is a single inheritance language, meaning a class can only have one direct parent. Often single inheritance can be a limiting factor or obstacle when designing an application's class hierarchy. This course explores how C# interfaces mimick multiple inheritance, enabling a class to take on the properties and methods of other unrelated classes.
Intended Audience
This course is intended for anyone who wants to take their C# object-oriented programming to the next level..
Prerequisites
To get the most out of this course, you should have a basic understanding of C# classes. If you are unfamiliar with C# classes, please take our Introduction to object orientation and C# Class course first.
Resources
The source code used in this course is available at https://github.com/cloudacademy/csharp-interfaces.
The dictionary defines an interface as “a point where two systems, subjects, organizations, etc., meet and interact.” In C#, the interface concept can be applied to the interaction between classes where one class implements an interface. An interface can be thought of as a class specification in that it declares properties and methods, but you can’t instantiate an instance of an interface. An interface is declared with the keyword interface and, by convention, has an “I” prefixed to the name. A class implements an interface by appending the interface name to the class definition, similar to class inheritance. However, a class can implement multiple interfaces. In turn, interfaces can inherit properties and methods from multiple parent interfaces. A class must declare all the properties of all the interfaces it implements, including those inherited indirectly. The car class implements properties of IIceVehicle and ITransport. Interfaces can have methods with code called default interface methods, so a class doesn’t have to implement those methods but does have to implement declaration-only methods. Interface properties and methods implemented by a class are public.
The most common interface use case is mimicking multiple inheritance. Interface types can be used as method parameters or in property declarations. You can assign any object to a variable or parameter declared as an interface type, just as long as, at the very least, the object has the properties and methods of that interface, with the exception of default interface methods.
You could think of interfaces, like costumes, where you aren’t allowed entry to a theme party unless you wear the correct themed costume. Once at the party, you can only behave in a way consistent with the costume. If you go to a Star Wars themed party as R2D2, you must behave exactly as R2D2, i.e., no eating, drinking, or talking. Suppose you have a suitcase with multiple costumes, analogous to implementing multiple interfaces. In that case, you can leave the Star Wars party, put on a gorilla suit, and go to a Planet of the Apes party, where you must eat vegetarian food, drink and beat your chest.
My name is Hallam Webber, and I hope you’ve found this course on C# interfaces instructive and enjoyable. Please give this course a rating, and if you have any questions or comments, please let us know.
Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard.