Interfaces
Interfaces are a very important point of C# and OOP so we will take a look at them here.
So what is an interface?
Well in everyday terminology an interafce defines the ways we can interact with an object. For example we can use a remote control object to switch on our TV. So the On/Off button is part of the interface. Under the hood when we press this button there will be lots of electronical whizzy things happening which we don't need to worry about but it will result in a signal being sent to the TV telling it to switch itself on or off accordingly.
Other common interafces are the interface to a car. They all(mostly) have a brake, accellerator, clutch, steering wheel, indicator switch etc.
So an interface is the set of ways by which we can use an object. In a software object this would correspond to all of the public methods, properties, events etc.
Interfaces are very important in building flexible and reusable code. Consider again the real world example of a car. Most cars have the same common interface and so when we buy a new car, or use a hire car, or borrow a friend's car we can just get in and drive as they all share the same interface. If they didn't then it be very difficult to adapt and use other cars.
Software can be very simillar. We can use interafces to make lots of different objects behave in simillar ways making our code and objects flexible and interchangable.
So in C# we are able to define an interface. An interface looks quite like a class but uses the word interface instead of class. Also we do not have any actual code as we are only defining what the interface should look like. Finally we don't use access modifiers like public, private, protected etc since by definition all interface members must be public of course!
So for our car interface we may have something like this - and please note this is a very simple and contrived example to illustrate the point.
By convention we always name our interfaces with a capital I and then follow this usually with an adjective or noun. So common examples of interfaces are as follows:
- IComparable
- ICloneable
- IConvertable
- IDispoable
- IQueryable
- IEnumerable
- IEnumerator
This convention leads to intuitive interface names. Something which has an IDrivable interface can be driven. Something which has an ICloneable interface can be cloned and so on.
So once we have an interface defined we can make a class implement this interface. The way we do this is very simillar to inheritance. For example to use inheritance we might do this:
Here we are saying that the class Mondeo inherits from the class Automobile.
We could instead have done this:
Here we are saying that the class Mondeo implements the IDrivable interface.
We could also have said the following:
So here we are saying that the class Mondeo inherits from the class Automobile and also implements the IDrivable interface.
Within C# we are not allowed to have multiple inheritance - ie a class can only inherit from one other class. However with interfaces we may implement as many as we want. So for example we could have the following.
So here we are saying that the class Mondeo inherits the class Automobile but also implements the interfaces IDrivable, ICloneable and IDisposable.
Now with class inheritance, our subclass will automatically inherit the methods etc from the parent class. However with interfaces there is no code defined in th interface so our class has to write the methods corresponding to the interface which it is implementing. If this isn't done then the code won't compile. Therefore having a class implement an interface is aguarentee that it will expose the methods and properties of that interface.
Lts back to this basic scenario:
If we try and compile this then we will get the following errors:
So we must therefore write code to supply these methods and properties.
A quick shortcut to get you going is to right click on the Interface name and then click on Implement interface. This will fill out the stub methods and properties for you so all you have to do is and fill in the missing code. Doing this with our above example gives the following:
We can see that this won't actually do anything useful at the moment but the code will now compile. We would only get errors if we ran the code and tried to do something with it but the idea is that you now and fill in the methods.
So the real point about interfaces then is that client code can see that if an object implements a particular interface then it can expect that it will implement certain methods and be able to do certain things.
We can also create instances of interfaces like this:
In this case then the only methods that would be available to the car object would be those defined in the interface.
We can therefore use this approach to construct a collection of lots of different cars - Mondeos, Golfs, Escorts, Fiestas etc as long as they all implement the IDrivable interface.
Now suppose that we have a car driving simulator or even a car driving game all using different cars. As a software developer if you want to create a new car for such a system then all you need to do is make sure that your class implements the IDrivable interface and then you can plug your code in and it should all work automatically. Some such systems even have a folder where you can drop new dlls and these will be picked up automatically and used via reflection so its pretty much a zero impact installtion or true plugin experience!
So in summary, interfaces are all about standards, contracts and expectations. We can enforce a class to implement interface methods but we cannot guarentee what code is actually in those methods - that requires human agreement and common sense.
There are many existing interfaces which are used within the .net framework.
For example IDisposable is a common interface used by classes which use unmanaged resources - files, network connections, database connections, com objects etc. There is a good article on IDisposable found here:
http://www.audacs.co.uk/ViewPage.aspx?PageID=497
There are other common interfaces too such as IComparable - this is used for comparing one instance of an object to another and is used commonly when collections of objects are sorted.
A good explanation of this can be found at http://www.c-sharpcorner.com/UploadFile/prasadh/IComparablePSD12062005010125AM/IComparablePSD.aspx
And there are many more which you will have to look out for as you are coding now you know more about them.
Any comments or questions always welcome via the form below.