Sunday, 8 May 2011

Abstract classes v/s Interfaces

This article explain the scenerios when to use abstract classes and when interfaces.


Use an abstract class



  1. Use an abstract class to define a common base class for a family of types.

  2. Use an abstract class to provide default behavior.

  3. When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning.

  4. Subclass only a base class in a hierarchy to which the class logically belongs.

Use and Interface



  1. When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.

  2. Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.

  3. Use an interface to design a polymorphic hierarchy for value types.

  4. Use an interface when an immutable contract is really intended.

  5. A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

No comments: