Interfaces enable you to separate the definition of objects from
their implementation
Definition of objects in this case refers to the publicly exposed part of an object.
In the example below, it refers to the public properties Name / Age
and the Public methods Insert
and Update
.
In the below class, the definition of Person and its implementation (code used to implement the properties and methods) is the same i.e the class Person.
Class Person
{
private string _name; //private fields
private string _age; //private fields
public Name {get {....} set {.....}} //public properties
public Age {get {....} set {.....}} //public properties
public void Insert() {.......} //public methods
public void Update() {.......} //public methods
}
NOTE: "........" represents actual code which will be called when these properties / methods are invoked i.e their implementation.
Now, if i were to do create an interface for this called
Interface IPerson
{
public Name get; set; //public properties
public Age get; set; //public properties
public void Insert(); //public methods
public void Update(); //public methods
}
and then do Class Person : IPerson
then what I have done is that i have now separated the DEFINITION i.e. IPerson
from the Implementation i.e Person
.
NOTE : Why this is useful is already covered by Aamir and Gravell in their answers above so i wont repeat the same