In my project I have all virtual methods.so I can declare it in abstract class or in interface.But Why we user interface as same we can do with abstract?
-
2Use the right tool for the right job: http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx – Chris Sinclair Jul 13 '13 at 10:48
-
Virtual methods isn't good enough, they need to be *abstract* methods. Burning up the one-and-only base class that a .NET class can inherit from isn't usually that desirable. – Hans Passant Jul 13 '13 at 11:03
-
@Polity: "Specious" means "this looks correct but actually contains a subtle error", as in, "your argument is specious". I think you meant "species". – Eric Lippert Jul 13 '13 at 15:55
-
@EricLippert A subtle but fundamental error, I stand corrected! – Polity Jul 14 '13 at 02:40
4 Answers
An interface looks like a class, but has no implementation.
-The only thing it contains are declarations of events, indexers, methods and/or properties.
-The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.
Interfaces in C# are provided as a replacement of multiple inheritance.
-Because C# does not support multiple inheritance, it was necessary to incorporate some other method so that the class can inherit the behavior of more than one class, avoiding the problem of name ambiguity that is found in C++.
-With name ambiguity, the object of a class does not know which method to call if the two base classes of that class object contain the same named method.
Purposes of Interfaces
-create loosely coupled software
-support design by contract (an implementor must provide the entire interface)
-allow for pluggable software
-allow different objects to interact easily
-hide implementation details of classes from each other
-facilitate reuse of software

- 3,374
- 3
- 29
- 49
-
All of the above listed advantages are also provided- or could be provided with abstract classes. A good argument for offering interfaces can be found in the link provided by Chris Sinclair. Interfaces simply have a different purpose then abstract classes. Although possible, you wouldnt ram a screw with a hammer – Polity Jul 13 '13 at 11:06
-
-
1Interface defines a contract. An Abstract base class defines a behavior. Essentially, you can provide a single class that implements multiple interfaces, which then in turn can be injected into multiple classes, but you will only have a single abstract base class (at least in C#). Consider the point of registering a type at the container (the composition root at best) and consider the point where you resolve the dependency (the constructor or a property). – Deep Sharma Jul 13 '13 at 11:46
Abstract classes allow some implementation to serve as a basis. Interfaces also allow multiple inheritance.

- 43,651
- 22
- 107
- 170
If text isn't helping you then let me give you an example, recently I created a collection of controls that all inhertited from other controls that shared a common function UnitChanged()
(which did a different thing for each).
public MyTextBox : TextBox
public MyComboBox : ComboBox
public MyLabel : Label
When I needed to call this function, it needed to change all labels, textboxes, and comboboxes now if I didnt use an interface I would have to do
foreach(MyTextBox tb in this.Controls.OfType<MyTextBox>())
tb.UnitChanged();
foreach(MyComboBox cb in this.Controls.OfType<MyComboBox >())
cb.UnitChanged();
foreach(MyLabel lab in this.Controls.OfType<MyLabel>())
lab.UnitChanged();
now with an interface
foreach(IMyInterface control in this.Controls.OfType<IMyInterface >())
control.UnitChanged();

- 42,633
- 14
- 77
- 146
driven classes can implements multi interface, but only each driven class can inherits from one classs

- 3,671
- 1
- 22
- 21