Home > Patterns and Practices > Interface vs Abstract Base Class

Interface vs Abstract Base Class

MSDN:
  1. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
  2. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
  3. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
  4. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

 

Interface

Abstract Base Class

A class may implement several interfaces.

A class may inhertis from only one abstract class.

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.

Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects. In other words interfaces model a can do relationship.

An abstract class defines the core identity of its descendants. If you defined a Dog abstract class then Damamation descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is. In other words abstract classes are useful in modelling an is a relationship.

If all the various implementaions share is the method signatures, then an interface works best.

If the various implementations are all of a kind and share a common status and behaviour, usually an abstract class works best. Another issue that’s important is what I call "heterogeneous vs. homogeneous." If implementors/subclasses are homogeneous, tend towards an abstract base class. If they are heterogeneous, use an interface. (Now all I have to do is come up with a good definition of hetero/homo-geneous in this context.) If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they share is a set of method signatures, then tend towards an interface.

If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.

If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.
 
Update: Via an Anonymous pal:
When it comes to implementation / realization of a feature) Interfaces allow maximum runtime variation wheras Abstract base classes allow controlled variation.

Advertisements
  1. Unknown
    October 14, 2006 at 6:15 pm

    Interface allow maximum runttme variation while abstract class allow controlled variation.
     

  2. Unknown
    November 23, 2008 at 10:06 am

    您需要二手液晶显示屏、废旧液晶屏么?我们是不折不扣的二手液晶屏、旧液晶屏大批发商,长期大量供应可再利用的旧液晶屏。我公司提供的各种尺寸的二手液晶屏, 不同厚薄如笔记本屏,均已经过我们严格的分类,检验,测试流程。请访问协力液晶屏www.sceondhandlcd.com[bbibbadecehhihce]

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: