18 September, 2014

Why are interfaces used?


1) In programming languages, Interfaces are like a contract which states what methods would be available by Implementing Classes. This is really helpful, when we are concerned about specific functionality only.

2) Child classes implementing an interface follow IS-A relationship with that interface.


Adding 1 and 2 let us assume Mr. A is developing a framework which accepts some object which is expandable. Now, the term 'expandable' may vary depending upon object, as some might expand in length, others may expand in volume and some might not expand at all under some conditions.

To implement such a scenario, Mr. A will create an interface lets call it Expandable.

interface Expandable{
    public void expand();
}
And in his framework he wrote something like (using static for simplicity of writing the code here..)

class ExpansionService{
    public static expandObject(Expandable expObj){
        ...
        //check weather framework can expand
       // do some more stuff like logging
        ...
        expObj.expand();
        ...
        // some more stuff
    }
}

Now, Mr. A doesn't need to worry about how the object expands, he can continue writing his framework related work.


On the other hand, Ms B is helping on writing the implementation. At some point she realized that her TheBigComplex class needs to be expanded using framework's ExpansionService.
class TheBigComplex extends someclass implements someinterface{
    ...
    some complex coding
    ...
}
She tries to invoke
TheBigComplex bObj= new TheBigComplex();
ExpansionService.expandObject(bObj) // would not compile due to type mismatch.

Because TheBigComplex class doesn't implements Expandable interface, this is not possible. So she implemented the interface and everything works fine!

class TheBigComplex extends someclass implements someinterface,Expandable{
    ...
    some complex coding
    ...

    public void expand(){
        // do some validations
        // expand object if possible
    }
}

Now, since the class contains the definition of expand method and follows IS-A hierarchy, our mission is accomplished.

Did we observe what Mr. A did here? He wrote a code which is dependent on some object definition, but is not yet created. He delegated the task, and wrote only the code which was utter necessary. Everything else was postponed until required. He concentrated only on specific functionality that he wanted (what happens around object expansion).

And did you see, what Ms B did? She implemented an interface which was not written by herself . To make use of framework's code. She implemented the interface, and provided the definition of required methods (because she is the only one who knows how the object expands).

Similar thing can be achieved by using an abstract class as well, but in Java, there is a major limitation of abstract classes. Multiple inheritance is not supported through classes, but is supported through interfaces.

Reconsidering the class that Ms B wrote. It already extends a class. If Expandable were a Class instead of interface, Ms B would never have been able to make her TheBigComplex class as Expandable.