Coding to an interface rather than to implementation. This makes your software/application easier to extend. In other words, your code will work with all the interface’s subclasses, even ones that have not been created yet.
Anytime if you are writing code that interacts with subclasses, you have two choices either; your code directly interacts with subclasses or interacts with interface. Like this situation, you should always favor/prefer to coding to an interface, not the implementation.
I have created a simple example with JDK classes & interface which demonstrates its benefits.
CodingToAnInterface.java
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
public class CodingToAnInterface {
public static void main(String[] args) {
// Coding to a class : Creating new instance of Hashtable & Assign it to the class.
Hashtable subject = new Hashtable();
CodingToClass c2c = new CodingToClass();
subject = c2c.addSubject(subject);
System.out.println("CodingToClass : Hashtable : subject : "+subject);
// Coding to an interface : Creating new instance of Hashtable & Assign it to the Map interface object.
Map subj = new Hashtable();
CodingToIntf c2i = new CodingToIntf();
subj = c2i.addSubject(subj);
System.out.println("CodingToInterface : Instantiated Hashtable : subject : "+subj);
// Now, we have a requirement to show subjects in the order it was inserted. Let’s make a simple change from Hashtable to LinkedHashMap.
subj = new LinkedHashMap();
subj = c2i.addSubject(subj);
System.out.println("CodingToInterface : Instantiated LinkedHashMap : subject : "+subj);
}
}
class CodingToClass{
// Method argument & return type are defined with class name Hashtable. This allows only Hashtable class as parameter.
public Hashtable addSubject(Hashtable subject) {
subject.put("Subject1", 50);
subject.put("Subject2", 70);
subject.put("Subject3", 60);
subject.put("Subject4", 80);
return subject;
}
}
class CodingToIntf{
// Method argument & return type are defined with interface Map. This allows to send various possible interface subclasses.
public Map addSubject(Map subject) {
subject.put("Subject1", 50);
subject.put("Subject2", 70);
subject.put("Subject3", 60);
subject.put("Subject4", 80);
return subject;
}
}
Output
CodingToClass : Hashtable : subject : {Subject4=80, Subject3=60, Subject2=70, Subject1=50}
CodingToInterface : Instantiated Hashtable : subject : {Subject4=80, Subject3=60, Subject2=70, Subject1=50}
CodingToInterface : Instantiated LinkedHashMap : subject : {Subject1=50, Subject2=70, Subject3=60, Subject4=80}
In the above example, initially assigned Hashtable object to interface Map object instead of Hashtable class. Later, we have a requirement to show the subjects in the order we have inserted into it. So we need to change the code and this can be easily achieved here by simply changed the instantiation part from Hashtable to LinkedHashMap without touching addSubject part.
However in coding to class, we have to change all the places where Hashtable is being used.
Instead of your code being able to work with only specific subclass called Hashtable, you are able to work with more generic Map interface. In future, it’s easier to change into some other sub class of Map interface.
Advantages
- App/Software is easier to extend
- Adds Flexibility to your App.
- Helps to maintain the loose coupling of code.
Tags: advantage of coding to interfaces, class, code to interface, code to interface benefits, coding to an interface, coding to interfaces, interface, interface benefits, interface implementation, interface implementation adds flexibility, interface vs class implementation, interfaces are easier to extend, java interface, loose coupling of code
August 21, 2011 at 2:03 am |
With all the overhead involved in coding to the interface, I sometimes feels it is theoretically great but very rarely useful.
BTW, it was great post.
April 20, 2012 at 11:17 pm |
Sometimes, when I start to architect a new project, the initial design using interfaces is a bit daunting, especially if the object is something different than I’m used to. For example, you are used to a Book store where you have books, magazines, ebooks, etc. The interfaces are easier once you develop them. The problem is when I have to create a shopping cart that includes shipping, tax rates, coupons, etc. Now I need new interfaces that sometimes takes a bit of time to develop (i.e. shipping has carrier, address, location, destination, cost per unit, etc.).
The map example above is pretty easy to understand but rarely are my interfaces that easy:-)