Monday, 5 March 2012

What is Interface? Why should you use Interface?

A few days ago, there was a question in a programming forum. The question was very common though. A newbie wanted to know what is interface and why should it be used?

My answer was something like below:


"See, in real life, when you interact with some objects, you don't really care about the internal implementation or complexity of that object. For example, when you use your computer, all you care about is how to power on or off by switching the button, how to input data (Using keyboard or mouse) and operate it. As a user, you really don't care about how the computer is internally operating by processing your inputs using complex logic and show the output in the screen.


So, the computer has not exposed the internal complexities to you, or, to the outside world. All it provides is an "Interface" to the outside world with the information that the outside world needs to know to use and operate the computer. As a user, you are happy to know about the "Interface" only, rather then the Internal implementation and complexity


In the programming world also, when you need to develop a class (Which may do many complex things), you need to expose the capabilities and operation of the class to other applications or class libraries (That may use this class) so that they can understand and use your class accordingly. What you do is, you create an Interface that contains the method signatures and you implement those methods in your class. So, the client applications and libraries can develop their codes based upon the Interface you defined, and while at runtime, the method invocation on the interface actually turns out to be a method invocation on the object of the class that implements the Interface.


Use of interface gives you extreme flexibility. For example, suppose you have an interface (IComputer) that exposes the functionality to the outside world and you have a class (WindowsPC) that implements the Interface method. Now, based upon some requirement, you can develop another class (MacPC) that implements the interface methods in a different way (Because, a MAC pc is basically a Computer, that roughly has the same kind of features, but, internally operates diffrently). Now, if the client application codes are developed based upon the Interface (IComputer)and, not based upon the implementation classes (WindowsPC or MacPC), there is very little chance that, they would need to change their code when the internal implementation of WindowsPC or MacPC might need to be changed.

WindowsPC and MacPC both implements the IComputer interface. So, if your client codes are developed using the IComputer interface and they accept an object of type IComputer as parameters in their methods, you can send them an WindowsPC object instead of a MacPC object any time and the client code will run perfectly without any problem (As long as your implementation classes are implemented correctly).


So, use of Interface this gives you lots flexibility to your code that you can manage easily and you can extend it without much change.


Basically, use if interface is part of the basic Object Oriented Principles. You may take a look at this article
Post a Comment