Interface

Posted by Learner under C# on 1/12/2010 | Views : 1605 | Status : [Member] | Replies : 4
Why do we need Interfaces ?? Any typical solution which can be solved by Interface only .. I know about multiple inheritance & other stuff but need typical example...




Responses

Posted by: Danishgr8 on: 1/27/2010 [Member] Starter

Up
0
Down
The Main Advantage of using Interfaces is that we can Make Common Functionality in one MainPage.
Suppose we have a interface of Several Methods "Save,Delete,Add" with this we can make only a Single SAVE,ADD,Delete Button in Parent Window and Implement it functionality for particular form.

Learner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 1/12/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
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

Posted by: Nishithraj on: 1/13/2010 [Member] Bronze

Up
0
Down
Through interfaces we can force all the classes to implement some abstract methods.

I think it would be clear if I explain you through an example.

If you are the architect of a project and you have to make sure that a particular functionality should be very uniform and all the methods in that need to be implemented every where, you can use interface.

If the requirement is like that something in your design changes frequently then go for interfaces instead of classes, in this case you just have to modify the interface to make the design changes.





Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Learner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 1/18/2010 [Member] [MVP] Bronze

Up
0
Down
In addition to the other comments in this thread I just wanted to point out that c# doesn't support multiple inheritence.

Although you can chain several classes together, each class can only inherit from one other class above it.

Learner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response