Re: When would you use abstract classes over interfaces
Daniele Futtorovic wrote:
On 29/07/2008 03:30, sasha allegedly wrote:
When would you use abstract classes over interfaces
Hardly ever. If anything, use both.
I'll agree with this. Check out the way Lists are done in the
Collections API. First, there's a List interface. Second, there's an
AbstractList, which is intended to allow a programmer to create a list
object by inheritance by overriding only a few methods.
new AbstractList<Integer>() {
public Integer get(int i) {};
public Integer set(int i, Integer val ) {};
public int size() {};
}
You only need to override three methods to make a new List, thanks to
AbstractList. The list interface has many more methods, but
AbstractList handles them for you. That's a big convenience in an API
designed for extension by inheritance.
So the pattern here is to use abstract classes in addition to
interfaces. The interface is kind of the primary specifier of a class's
contract, and the abstract class is a helper to make the programmer's
job easier.