Reputation: 228
I am not getting proper answer about Interface, I'm really so confused about interface, because we are directly use the class and methods using of object. than why we unnecessary create Interface and Inherit in Class?
Upvotes: 3
Views: 17230
Reputation: 1794
Interfaces are a powerful programming tool because they let you separate the definition of objects from their implementation. Here some points from msdn:
1)Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
2)Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
3)Interfaces are better in situations in which you do not have to inherit implementation from a base class.
4)Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.
for example: lets say you have a program which need to collect data from different parts of it. Every part is not connected to the other, like: Screen, Printer, Beer Machine, and so on. Each class will impelment IDataCollector
and it's functions and eventually you'll be able to do something like that in some manager class:
foreach (ProgramFeature feature in ProgramFeatures)
{
if (feature is IDataCollector)
{
feature.CollectData();
}
}
Upvotes: 3