04/01/2016
When to use Abstract and when to use Interface
1. If you are creating something that provides common functionality to unrelated classes, use an interface
2. Abstract classes allow you to provide default functionality for the subclasses.
3. If you are creating something for objects that are closely related in a hierarchy, use an abstract class
4. If the base class will be changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken. Now if it’s just you working on the project, that’s no big deal. However, once your interface is published to the client, that interface needs to be locked down. At that point, you will be breaking the clients code
5. Compared to interfaces abstract classes can have implementation. Without implementation an abstract class does the same as an interface but C # allows you to inherit from / implement multiple interfaces but inherit from one base class only.
6. An interface once deployed is "frozen" - you must not change a deployed interface. If you change an interface you break binary compatibility but you can extend an abstract class as long as you don't change method signatures.
Use an abstract class
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.)
Use an abstract class to define a common base class for a family of types.
Use an abstract class to provide default behavior.
Subclass only a base class in a hierarchy to which the class logically belongs.
Use an interface
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.
Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
Use an interface to design a polymorphic hierarchy for value types.
Use an interface
when an immutable contract is really intended.
A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.
Main difference between abstract classes and interface is that an abstract class provides default implementation for which subclasses does not need any implementation and can be used as it is. While interfaces force all subclasses to implement its all methods. Abstract class method can have different access modifier like private and public while interface will always have public. Abstract can have member variables however interface cannot have.
Example:
If you are building car of the same model with different engine types -Petrol, Diesel, CNG and Hybrid..all other parts chasis,Body,Wheels, dashboard remain same and these are implemented in default implemenattion of the Car abstract class because these parts do not change while the engine abstract method in the abstract Car class lets subclasses like PetrolCar, DieselCar,LPGCar and HybridCar to provide its own implementation.This is an example of abstract class.