When we use interface and when we use abstract class

By | September 18, 2017

Use an abstract class

  1. When creating a class library which will be widely distributed or especially to clients, use an abstract class in preference to an interface; because, it simplifies visioning. This is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)
  2. Use an abstract class to define a common base class for a family of types.
  3. Use an abstract class to provide default behavior.
  4. Subclass only a base class in a hierarchy to which the class logically belongs.

Use an 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 behaviour without sub-classing and to model multiple inheritance a specific type to support numerous behaviours.
  3. Use an interface to design a polymorphic hierarchy for value types.
  4. Use an interface when an immutable contract is really intended.
Category: C# Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *