Stephan van Hulst wrote:Personally, I think you should almost always go with an interface. Abstract classes have their place, but when in doubt, use an interface.
When you have some standard implementation for a method, you can then make an abstract class implementing an interface. This leaves classes free to either implement the interfaces in a completely new way, or to extend the abstract class and inherit the standard implementation.
I agree. I usually create an interface, then if appropriate create one abstract class with basic implementations of the methods. Similar to List and AbstractList; Set and AbstractSet; Map and AbstractMap.
One of the greatest mistakes in the standard API in my opinion, is that InputStream and OutputStream were made abstract classes, instead of interfaces.
#1 on my rant list on the
Java API; java.util.Observable is #2 (indexing based on when I came up with the items). InputStream and OutputStream should have been made interfaces, with the current implementations being renamed to AbstractInputStream / AbstractOutputStream. But of course that's not possible anymore, with too many classes (both in the API and created by others) already extending InputStream / OutputStream.