Adapter classes
Listen to this article
Since I ranted about abstract classes a couple of days ago I've read quite a number of other blogs that discuss interfaces versus abstract classes. Most of the discussion seems to be around the fact that adding a method to an interface is a pain because you need to add that method to any class that implements the interface. The solution in some cases (but definitely not all) is to have an "adapter" class that provides empty implementations of all methods. You can then extend this and override only the methods you need. For example:
public interface Foo {
public void somethingHappened();
}
public class FooAdapter implements Foo {
public void somethingHappened() {}
}
This can make a lot of sense if you're implementing some kind of listener/observer. But if the interface is required to return something as in:
public interface Foo {
public int getSomething();
}
then what do we do? Return 0 for integers, null or "" for strings or throw UnsupportedOperationException?
Interestingly, if you don't recompile the implementing class and something attempts to call a method on the interface that you haven't implemented, the JVM will throw a NoSuchMethodError.
The one place where it might bite significantly is if/when you change a public interface in a public API. Once that API is published and used by third-parties, it becomes much harder to change for fear of breaking backwards-compatibility.
At any rate, these "adapter" classes aren't really abstract classes nor are they types. They are once again a convenience of the programming language. Ruby and smalltalk for example will, like the JVM, throw an exception if you attempt to call an unimplemented method on a class. The difference being that Java's strong typing forces you to deal with it up front.
So, putting pandora firmly back in her box, I don't see that any of this really goes against my original proposition that abstract classes are not in fact types.
Comments
I like the idea of providing a convenience class with empty implemtations. However, I don't like the name "Adapter". To me an adapter is just that - an object what "adapts" incompatable interfaces. Not sure why the Java folks chose that naming convention (WindowListerer -> WindowAdapter).
Posted by: Ryan Breidenbach | December 4, 2003 08:17 AM
I must admit I don't like the name either. What was wrong with AbstractXXX? Hehehehe.
Posted by: Simon Harris | December 4, 2003 03:03 PM
Simon,
I am with you that abstract classes are not types; in fact, I find inheritence in general to be more of a convenience than anything else - it certainly isn't a requirement.
I think that some understanding of that disconnect comes to a developer when they spend some time working with a loosely, dynamic typed language, where patterns such as decorator can actually be easier to implement than inheritence.
Either way, inheritence can be a frustrating beast to manage; patterns are so much more flexible!
Regards, R.J.
Posted by: R.J. | December 5, 2003 12:32 PM
Good discussion,It is quite helpfull
Posted by: katya | July 27, 2005 04:00 PM