Interfaces is actually allows multiple inheritance.
Say you have a class A, and Class B
Say you have a common part of both, now you need a generalized code which might work with both A and B.
In such a scenario, you can do like : create a "switch case " which will detect each type and do the job for you.
If this number of classes are 500, it would be difficult to have such a long switch case construct.
There we need Interfaces.
For example :
If you see IDbConnection.
Both SqlConnection and OleDbConnection (also others) has the same function Open() and close()
Now if you want your application to work for both type of connection, you can just pass in your connection object which is inherited from the interface IDbConnection and you can easily assign to the Interface type and call its Open.
public void OpenConnection(IDbConnection con)
{
con.Open();
}
Now you can pass :
this.OpenConnection(oracleCon);
this.OpenConnection(sqlCon);
this.OpenConnection(oledbcon);
If you want to do this without Interfaces, you need to write quite a longer code than this to call specific function Open.
I hope you got the point properly now.
Cheers.
www.abhisheksur.com
Learner, if this helps please login to Mark As Answer. | Alert Moderator