The general guideline: Use abstract clas for "is A" relationship, while use interfaces for "Can do" relationship.
For example:
An Aircraft "is a" Vehicle and it "can" fly.
So it makes sense to make Vehicle an abstract class and make an interface IFlyable that class Aircraft implements and Aircraft derives from Vehicle class.
The choice of whether to design your functionality as an interface or an abstract class (a MustInherit class in Visual Basic) can sometimes be a difficult one. 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. For details, see Abstract Classes.
An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.
Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument. For example:
// C#
public void Spin (IWidget widget)
{}
This method could accept any object that implemented IWidget as the widget argument, even though the implementations of IWidget might be quite different. Abstract classes also allow for this kind of polymorphism, but with a few caveats:
Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, they must all inherit from that class.
Abstract classes may also provide members that have already been implemented. Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
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.
If you need shared behavior to derived classes then use abstract class or if you need plug gable element structure use interfaces because interface is only alternative of multiple inheritance in term of reusable component design not for shared functionality, it’s just define a contract to derived classes .
Differences:
Abstract class may or may not have abstract method other than interfaces only declarative.
Abstract class can contain variable other than interface can’t.
Interfaces members are implicitly public; so you can’t use access modifiers to restrict access other than abstract class follows the general class rules.
We can’t use static and constant members in interface other than abstract class we can use.
A Class or Struct can be inherited from multiple interfaces but only from single abstract class.
Interface provides loose coupling other than concrete class provide strong coupling.
Interfaces support events, indexers, methods, and properties.
Coupling: In Brief when one class has direct knowledge of other class refer as coupling. If dependent class has direct reference to concrete class then it‘s called strong coupling else when dependent class contains pointer to interface it’s called loose coupling.
Partial Interfaces: Interfaces can be used with partial keyword like partial classes as we know compiler combine the partial elements to create unified interface definition.
partial interface IProcess
{ void DataProcess(); }
partial interface IProcess
{ string ProcessType { get; } }
class Process : IProcess
{
public void DataProcess()
{ //Logic }
public string ProcessType { get { return "Data"; } }
}
Explicit Interface Implementation: Multiple inheritances by using interfaces are quite easy until members are identical .Suppose two interfaces have same member like below:
interface IExportProcess { void Save(); }
interface IImportProcess { void Save(); }
Problem occur when XMLDocument object up cast to IExportProcess ,calls to the Save Method expecting different result then if the same method was called when the object up cast to IImportProcess .
To sort out this problem you can use explicit interface implementation as below;
class XMLDocument :IExportProcess,IImportProcess
{
public void IExportProcess.Save()
{ //Logic }
public void IImportProcess.Save()
{ //Logic }
}