Possible Duplicates:
Abstract classes vs Interfaces
Interface vs Base class
Hi,
In C# when should we use interfaces and when should we use abstract classes? Please explain with example
Possible Duplicates:
Abstract classes vs Interfaces
Interface vs Base class
Hi,
In C# when should we use interfaces and when should we use abstract classes? Please explain with example
Abstract class, if you want to implement basic functionality and some properties/methods that must be overwritten by the implementer:
public abstract class Entity
{
public int Id { get; set;}
public bool IsNew { get { return Id == 0; } }
public abstract DoSomething(int id); // must be implemented by concrete class
}
Interface, if you want to define, which properties/methods a class must contain, without implementing any functionality:
public interface IRepository
{
object Get(int id);
IEnumerable<object> GetAll();
}
use interfaces when you don't when your describing a contract and abstract classes when your implmenting functionality that will be shared among deriving classes.
To examples of usage could be template pattern and Strategy pattern