What is different between an abstract and an Interface class in C#?
-
2Interface is not a class,,i guess .. – Sangram Nandkhile Mar 14 '11 at 07:58
-
might be useful http://stackoverflow.com/questions/10443344/when-to-use-abstract-classes-and-interfaces/10443530#10443530 – Balaswamy Vaddeman May 15 '12 at 12:13
-
possible duplicate of [Why do both the abstract class and interface exist in C#?](http://stackoverflow.com/questions/1028285/why-do-both-the-abstract-class-and-interface-exist-in-c) – nawfal Jul 07 '14 at 09:55
8 Answers
An interface is not a class, it is just a contract that defines the public members that a class must implement.
An abstract class is just a class from which you cannot create an instance. Normally you would use it to define a base class that defines some virtual methods for derived classes to implement.

- 5,221
- 2
- 27
- 29
-
Be careful while using the word 'contract', because a contract is often much stronger than a 'definition'. A contract would give guaranties, such as that the implementation never returns null, or will never throw an exception. Still + for this. – Steven Mar 14 '11 at 10:15
-
It does provide guarantees. It provides the guarantee that certain methods will exist, no matter what the concrete type (so long as the concrete type implements the interface). A type system is basically a bunch of specific, codified contracts. Indeed, you can encode non-nullity and non-exception throwing in a type system - they would still be contracts. – ICR Mar 14 '11 at 10:49
Rather than writing whole thing here..
try http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

- 31,557
- 24
- 104
- 139

- 17,634
- 19
- 82
- 116
A class can implement multiple interfaces but can only inherit from one abstract class.
An abstract class can provide implementation for it's methods. An interface cannot provide implementations.

- 31,254
- 3
- 43
- 68
the level of interface is higher than abstract.
when u're design the strcuture, draw the uml, u should use interface.
when u're implement, then u should use abstract to extract repeat things.
anyway, the different is not only a syntax problem..
hope it helps.

- 517
- 4
- 14
The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes. When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

- 51
- 1
- 1
- 7
Google "abstract class vs interface" and you'll get lots of explanatory articles...

- 31,557
- 24
- 104
- 139
A class can implement multiple interfaces but can only inherit from one abstract class.
Also, abstract classes may have some functions defined but interfaces will not have any function definition and the deriving class must define all of them.

- 21,454
- 43
- 116
- 176
I would explain this through the usage. Abstract class can be used when there is only one hierarchy, additionally without default implementation; while interface can be used across hierarchies (horizontally), often referred to as a behavior.
Interface is also an abstraction and in c# substitutes multiple class inheritance, so this may be confusing, but you have to distinguish when to use what.
Hope this helps, Robert

- 3,276
- 1
- 17
- 26