At a recent interview I was asked about the difference between an interface vs and an abstract class and when to use which one.

I answered that that interfaces do not have any implementation whereas an abstract class does. So, an abstract class can have one or more be abstract members (i.e. without implementation) with other members implemented. What I did not answer really answer is when which one is preferred.

From the body language of the interviewer I felt he was not very satisfied, but I could not think of an example of when to use an interface over an abstract class or the other way around. Can someone tell me how I did and/or what I was missing?

1 Answers

These are some important differences.

Abstract classes are templates to other classes with basic functionality implemented. Derived classes only need implement the abstract members. Only single inheritance is supported, in other words, a derived class can only inherit from one abstract class.

Interfaces are 'skeletons' with only members defined without implementation. In other words a class that inherits from an interface must implement all members (methods and properties). Furthermore, a derived class can implement multiple interfaces. Finally interfaces are typically smaller than abstract classes. Take IDisposable, which has only one member.