Coding to interfaces? [duplicate]

Just one possible correction:

To use the interface one can simply call the methods on an instance of the concrete class.

One would call the methods on a reference of the type interface, which happens to use the concrete class as implementation:

List<String> l = new ArrayList<String>();
l.add("foo");
l.add("bar");

If you decided to switch to another List implementation, the client code works without change:

List<String> l = new LinkedList<String>();

This is especially useful for hiding implementation details, auto generating proxies, etc.

You’ll find that frameworks like and encourage programming to an interface. It’s the basis for ideas like programming, auto generated proxies for transaction management, etc.

Leave a Comment