Click here to Skip to main content
15,353,432 members
Please Sign up or sign in to vote.
3.83/5 (3 votes)
See more:
Dear sir,

Please explain why to use interface (leave the answer multiple inheritance).

Please brief the answer for interface.

i know it is used for multiple inheritance, please send me another important benefits.

Thank's
Rohit singh
Posted
Updated 24-Jan-11 23:06pm
v3
Comments
GPUToaster™ 25-Jan-11 4:17am
   
you should consider reading basics of OOPS related to .NET.

Let's examine an example:
Consider, for instance, the IEnumerable[^] interface. It specifies the contract your
custom collection must fulfil in in order to support the foreach semantics.
Now, foreach knows nothing about your custom collection (but the fact that it fulfils the IEnumerable interface). Moreover, you're free to implement all the details of your collection (probably, it wouldn't make sense inheriting a 'piece of code' from IEnumerable).
:)
   
It is useful if you are working in a team.
You may be one of several developers working on different parts of a project that need to connect.
Use the interfaces to connect them.
So long as you are all writing to the same standard they will link easily.

Also, you can use a class without having to define it beforehand.
So the rest of the team can use the interface with getting stuck because you haven't finished writing some labrynthine and intricate class definition.
   
Lots of reasons!!

1) Make code more testable (specifically Unit Tests) by separation of concerns that allow easy mocking of dependecies of the code you are testing.

e.g. Separate out a Data Access code from a component/class (could be a database, xml file or network stream) into an interface called say IDataProvider. Then create a mock say DataProviderMock and pass that to your class you want to test (within your unit test framework) instead of the real version.

2) Separation of Concerns. Programming to interfaces isolates code to just the concern you are dealing with and enables 1).

3) One interface but many concrete versions that are not similar. A good example is the Data Provider in 1). In an n-tier system you may configure it to run on 1, two or 3 physical tiers. This may cause the IDataProvider to be implemented as Database access code (for single tier) or usings a middle tier data access assembly directly for 2 tiers or calls WCF services for 3 tiers (in fact in WCF you can do 2 and 3 with the same code!)

4) Dependency Injection. I could write an essay about this but please just google
"Unity" or "MEF". This uses interfaces to resolve which concrete type to assign to an interface at run-time.

5) I would not actually call using an interface while also inheriting from an astract class as "multiple inheritence", it is not. Interfaces do not implement the "is-a" paradigm, they actually implement what I call "like-a". An interface say ssomething is "like" say a file, a database etc but may not actually be any of those.

I am sure there are more!
   
The interface is allow to use same structure of class for multiple purpose.

i.e. It allow to use same methods with different definitions.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900