Search in Help for developer site.

Monday 26 December 2016

When to use Abastract Class over Interface in C#

Recommendations for Abstract Classes vs. Interfaces

An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes.

An interface, by contrast, is a totally abstract set of members. The implementation of an interface is left completely to the developer.


Some recommendations to help you to decide whether to use an interface or an abstract class

  • 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.

  • 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.

  • If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

  • 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. We can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Source : msdn

5 comments:

  1. The blog gave me idea when we should use abstract class over the interface my sincere thanks for sharing this post
    Dot Net Training in Chennai

    ReplyDelete
  2. you have been shared very informative post. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    dot net training in chennai

    ReplyDelete
  3. Thanks for your valuable feedback

    ReplyDelete