Codementor Events

WHEN TO USE ABSTRACT CLASSES AND WHEN INTERFACES. (C#, INTERVIEW QUESTIONS)

Published May 24, 2018

This is one of the major questions that gets asked during a dot net interview . The fact is that majority of the dot net developers out in the market are not quite clear about the answer . This is one of the topics in dot net that can debated on for a while and the output will just be lots of confusions. In reality an applications can be done with or without using them. Below are some of my observations that can give some idea but its not enough to conclude the debate.

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.

There are two main category which can be used to differentiate interface and abstract class

1. Versioning
An abstract class can contain an interface plus implementations. This simplifies versioning. An abstract class can be extended by adding new nonabstract methods with default implementations. Also, a convenience method is easily added to an abstract class.

An interface cannot be modified without breaking its contract with the classes which implement it. Once an interface has been shipped, its member set is permanently fixed. An API based on interfaces can only be extended by adding new interfaces.

2. Design flexibility
Interfaces offer more design flexibility; precisely because, they can be implemented by any class regardless of its type hierarchy.

USE AN ABSTRACT CLASS
1). 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. 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 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.

Above are my observations and informations that I found from various blogs and informative sites. I have the same article on my blog http://kandynote.blogspot.in/2014/03/when-to-use-abstract-classes-and-when.html

Discover and read more posts from Vipin Cheriyanveetil
get started
post commentsBe the first to share your opinion
Manish Gupta
3 years ago

Hi Vipin,

This is so far the best tutorial i got for the abstract class and interface usage though i have bit doubt and wanted you to please clarify if possible. and my doubt is below

“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”

can you please explain the above point in simpler manner so that i can can make my self clear.

I just don’t know how all the inheriting class will get updated if I will add anything in base class.

Please give an example code if possible.

Thank you.

Extreme Java
4 years ago

This is a good article. Abstract classes have been around since C++ days. Then all major OOPS languages have adopted them. Also I liked askanydifference website’s article on abstract class and interface difference.

Show more replies