August 19th, 2013, 07:28 AM
Uses of interface
It is not just about sharing codes with other programmers that you need interfaces for defining the API contract, but it also helps to simplify your coding effort in many scenarios.
Further to the answer provided by bullet, here is another example to give you an idea of how important the usage of interface is:
Imagine that you are working on an application that needs to send contents to client's printer. As we all know, there are a variety of printers available in the market, consisting of different brands and for each brand, there could be many different models. Each model could have different implementation of firmware and capabilities controlled by sets of parameters specific to the particular printer model for particular brand.
In such case, would you be able to guess what is the brand and model your client would be using in the environment your application is deployed to? If you could not, without using the concept of interface,
- how would you implement the codes to send the data contents to the client's printer?
- Would you find out all the possible printer brands and their respective models and then implementing a huge if-else block to call to those printer firmware API methods for each of them?
- Then how much code you need to write and how much time you need to implement the codes to send your application's data to each of these printers just in case your client is using either one of them (as you do not know what printer your client is using)?
- What if after you have published your application, one of the leading printer brand introduces a new model of printer with different API and new capabilities?
When the programming world turns decent, the real world will turn upside down.