From Zero to Master — C# Interfaces in under 15 minutes.
You Must have some knowledge of C#
This is a practical read and to get most out of it I encourage you to replicate and practice the programs on your own too. We will see how Interfaces work and why do we use them ? I would also explore Concrete and Base classes. So grab your coffee, adjust your chair and clean your glasses.
A little bit of theory
As a developer I want my code to be maintainable, extensible and easily testable. Interfaces helps me achieve all this. If it is not clear enough what is an Interface ? then think of it as a contract consisting of the public members like properties, methods and events. I Will get into more details of this later in this read but if you are thinking about why only public members ? that’s because any contract must be openly available and seriously would you want to hide some part of your leasing contract from your lawyer ?
Lets say we want to create an “Animal” class. Every animal will do at least two things eat and make some sounds and every animal will have at least one property the type or the name of the animal.
Lets say the animals are Cat and Dog. We can create a method called “Feed” which will feeds cat or dog but when we create a method for “Speak” the things get little complicated because the dog will bark and the cat will meow.
You may want to serve dog with dog food and cat with cat food but lets keep the things simple for learning purpose and assume that both Cat and Dog or any other animal in this read would eat the same food :)
Lets Start Programming With a Standard or Concrete Class
So lets look at the child Class Dog
now lets create the object of Dog class
when we run this program we will get
An abstract member is a member which is declared but not implemented and the classes which have abstract member (s) are abstract classes
Lets create an AbstractAnimalClass
Driving Cat.cs from AbstractAnimalClass.cs
Lets create the object of Cat class and run the program
and here is the output
Comparing Standard or Concrete Classes with Abstract Classes
In abstract classes we don’t implement any abstract member implementation goes into child classes. It means that there is no shared code we have to override the abstract class method in all of the child classes which drives from that abstract class. In Abstract classes if we don’t implement the abstract member in all of the child classes we will get a compile time exception
In Standard Classes we have shared Implementation if we don’t over ride a virtual method in child classes it will use the implementation of parent class
If you are clear with above concepts then we are ready to jump into learning interfaces if not you can seek my help leave a comment and i will try to explain with a few more ways :)
Getting Started with Interfaces
Now as you know what the abstract class is a pure abstract class will be a class in which none of its objects are implemented. Interfaces are similar to the pure abstract classes its member don’t implement anything at all in that interface.
Lets create or Animal Interface.
Lets introduce a new animal “Loin” and implement this interface
lets create the Loin object in program.cs
and here is the Output
We have seen here that how to create a concrete class, abstract class and an Interface. Same like abstract class the Interface implementation requires every member of the interface to be implemented if we don’t implement any member of our Interface it will give a compiler error.
Abstract Classes Vs Interface Vs Concrete Class
Concrete Classes or Standard Classes does not enforce us to Implement and over ride the virtual methods.
Abstract Class can have some non abstract member methods which can contain implementation while interfaces won’t contain any implementation at all.
Abstract Classes and Interfaces requires the Implementation of abstract members.
Class can only inherit from a single base class but a class can implement any number of interfaces.
Interfaces can not include Constructor, Destructors and Fields.
Abstract Class Members have access modifiers but Interfaces are always public.
Wow now you know what are interfaces next thing to read will be that how Interfaces help us produce testable and extensible code.
Thank you for reading :) please leave your comments if you have any questions or suggestions.
download the code from here : https://github.com/naveedcs/understanding-Interfaces