Monday, February 7, 2011

Interfaces vs. Abstract Classes




Feature
Interface
Abstract class
Multiple inheritance
A class may implement several interfaces.
A class may extend only one abstract class.
Default implementation
An interface cannot provide any code at all, much less default code.
An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
Constants
Static final constants only, can use them without qualification in classes that implement the interface. On the other paw, these unqualified names pollute the namespace. You can use them and it is not obvious where they are coming from since the qualification is optional.
Both instance and static constants are possible. Both static and instance intialiser code are also possible to compute the constants.
Third party convenience
An interface implementation may be added to any existing third party class.
A third party class must be rewritten to extend only from the abstract class.

No comments:

Post a Comment