Using interfaces in APIs - I

13 September 2008

When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.

Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.

Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered:

Interfaces can be implemented by anybody so you can not be sure that a class implementing this interface obeyed the semantics you expect.

It is seen that implementations of APIs that are defined entirely in terms of interfaces often end up cheating and casting objects to the non-public implementation class.

Interfaces cannot have constructors or static methods.

Interfaces cannot evolve. For instance you add a new method to an interface in version 2 of your API. Then user code that implemented the interface in version 1 will no longer compile because it doesn’t implement the new method.

Using abstract class instead of an interface is a better choice since you don’t have this problem.

Do note that Interfaces cannot be serialized.

del.icio.us:Using interfaces in APIs - I  digg:Using interfaces in APIs - I  spurl:Using interfaces in APIs - I  wists:Using interfaces in APIs - I  simpy:Using interfaces in APIs - I  newsvine:Using interfaces in APIs - I  blinklist:Using interfaces in APIs - I  furl:Using interfaces in APIs - I  reddit:Using interfaces in APIs - I  fark:Using interfaces in APIs - I  blogmarks:Using interfaces in APIs - I  Y!:Using interfaces in APIs - I  smarking:Using interfaces in APIs - I  magnolia:Using interfaces in APIs - I  segnalo:Using interfaces in APIs - I  gifttagging:Using interfaces in APIs - I

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Using interfaces in APIs - I

Leave a Reply