?

Log in

No Interfaces

Here's another short chapter. I think of these short ramblings as seeds in my garden of literature. With proper watering and enough sunlight, they will grow and bloom into beautiful chapters. But until then, I'm posting them as the shriveled little twigs that they are :-)


Chapter X: No Interfaces

While recently reading an article about interfaces, I thought to myself, "How am I using interfaces?" Then it hit me like a sucker punch; I don't use them at all anymore! There are a few interfaces in our existing system, but they were composed years ago. Nowadays, I use abstract classes instead. And after reviewing the ancient interfaces in the old code base, most of them should have been abstract classes.

Interfaces have two caveats.

First of all, you can't code any logic into them. If you want all the classes that implement a particular interface to behave identically, you have to trust them all to implement that behavior consistently. Whereas if you instead used an abstract superclass, you could enforce that behavior in a final method. All the subclasses would inherit it and could not override it.

Secondly, interfaces give the illusion of multiple inheritance. I think that's the main reason they exist in Java; to ease the transition of the old C++ codgers. I agree that multiple inheritance is more con than pro, but that's a never ending debate among programming language developers. Rambling on about it here would be an exercise in futility.

Now I'm not saying that interfaces should disappear. No. They certainly have their uses. You want all your Runnable classes to have a run method, and all your Comparable classes to have a compare method, and without interfaces you couldn't have a Runnable and Comparable class in Java. But would you ever want one? Rarely, I would surmise, but eventually.

Interfaces work best when they are small. Single method interfaces seem to be the most likely to survive the test of time. Java's Runnable and Comparable are good examples.

The point is a vast majority of the time when I want two or more classes to expose a common interface I also need them to share some common functionality and it makes more sense to abstract that functionality into a superclass. Interfaces have their uses, but I rarely need them.

Copyright (c) 2003 Thomas E. Davis, All Rights Reserved.

Comments

(Anonymous)

Interfaces have uses.

There are places where you need to use interfaces (look at java.lang.reflect.Proxy) and multiple interfaces (elements in java.util.SortedSet would typically use Comparable plus something else).

However, arguing for interfaces on an anecdotal basis is just that, anecdotal. The generic reason for having them is to break cyclic dependencies. You can do this with abstract base classes, but its harder, since the abstract class will depend on some concrete classes for the methods you've implemented. Of course, this is just moving the argument to why breaking cyclic dependencies is a good thing - the answer is that it means you can split out part of a product and reuse it elsewhere. (if you split one jar into two interdependent jars, what is the point?)

June 2005

S M T W T F S
   1234
567891011
12131415161718
19202122232425
2627282930  
Powered by LiveJournal.com