What is different between an abstract and an Interface class in C#?
- 2Interface is not a class,,i guess ..Sangram Nandkhile– Sangram Nandkhile2011-03-14 07:58:58 +00:00Commented Mar 14, 2011 at 7:58
- might be useful stackoverflow.com/questions/10443344/…Balaswamy Vaddeman– Balaswamy Vaddeman2012-05-15 12:13:23 +00:00Commented May 15, 2012 at 12:13
- possible duplicate of Why do both the abstract class and interface exist in C#?nawfal– nawfal2014-07-07 09:55:47 +00:00Commented Jul 7, 2014 at 9: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.
2 Comments
Rather than writing whole thing here..
try http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
Comments
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
Comments
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.